shell第二次作业
1. 使用case实现成绩优良差的判断
read -p "请输入你的成绩:" score
if ! [[ "$score" =~ ^[0-9]+$ ]];then
echo "请输入数字"
exit 1
fiif [ "$score" -lt 0 ] || [ "$score" -gt 100 ];then
echo "输入的成绩无效"
exit 1
ficase "$score" in
9[0-9]|100)
echo "优秀"
;;
6[0-9]|7[0-9]|8[0-9])
echo "合格"
;;
*)
echo "未及格"
;;
esac
2. for创建20用户
用户前缀由用户输入
用户初始密码由用户输入
例如:test01,test10
read -p "用户前缀和密码:" usrname s
for usrname in $(seq -f "$usrname%02g" 1 10)
do
id $usrname &> /dev/null
if [ "$?" -ne 0 ];then
useradd $usrname &>/dev/null
echo "$usrname is create"
echo "$s" | passwd --stdin $username &> /dev/null
echo "$usrname passwd is create"
else
echo "$usrname is already"
fi
done
3. for ping测试指网段的主机
网段由用户输入,例如用户输入192.168.2 ,则ping 192.168.2.10 --- 192.168.2.20
UP: /tmp/host_up.txt
Down: /tmp/host_down.txt
read -p "请输入网段:" ip
>/tmp/host_up.txt
>/tmp/host_down.txt
for ip in $( seq -f "$ip.%02g" 10 20)
do
ping -c 4 $ip
if [ "$?" -eq 0 ]; then
echo $ip >>/tmp/host_up.txt
else
echo $ip >>/tmp/host_down.txt
fi
done
4. 使用for实现批量主机root密码的修改
成功或失败都必须记录
提示:主机IP存放在一个文件中
SSH:实现公钥认证,执行远程中主机命令
实现公钥认证
# ssh-keygen 在用于管理的主上生成密钥对
# ssh-copy-id -i 192.168.2.3
read -p "输入修改之后的root密码:" passwdsuccess_log="/shells/T_file"
failure_log="/shells/F_file">"$success_log"
>"$failure_log"host_ip="/shells/ip"
if [ ! -f "$host_ip" ];then
echo "主机文件列表不存在"
exit 1
fifor ip in $(cat "$host_ip")
do
if ssh root@"$ip" "echo $passwd | passwd --stdin root";then
echo "密码修改成功:$ip" >> "$success_log"
else
echo "密码修改失败:$ip" >> "$failure_log"
fi
done