1.使用case实现成绩优良差的判断
#!/bin/bash
read -p "please you input your grades:" grades
case $grades in
8[5-9]|9[0-9]|100)
echo "the grades is A"
;;
7[5-9]|8[0-4])
echo "the grades is B"
;;
6[0-9]|7[0-4])
echo "the grades is C"
;;
*)
echo "the grades is D"
esac
2.for创建20用户,用户前缀由用户输入,用户初始密码由用户输入
#! /bin/bash
for((i=0;i<20;i++))
do
if [ $i -lt 10 ];then
user=test0$i
else
user=test$i
fi
if ! id -u $user &> /dev/null
then
useradd $user
echo "123456" | passwd --stdin $user &> /dev/null
else
echo "$user is exists"
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
#!/bin/bash
while true
do
echo "1:ping"
echo "2:quit"
read -p "plese input you chosoe 1~2:" choose
case $choose in
1)
read -p "please input ip address:" host
ping -c 2 -w 2 $host &> /dev/null
if [ $? -eq 0 ];then
echo $host >> /tmp/host_up.txt
else
echo $host >> /tmp/host_down.txt
fi
;;
2)
exit 0
;;
*)
echo "input error"
esac
done
4.使用for实现批量主机root密码的修改,成功或失败都必须记录
提示:主机ip存放在一个文件中,ssh:实现公钥认证,执行远程主机中命令,实现公钥认证
#ssh-keygen 在用于管理的主上生成秘钥对 #ssh-copy-id -i 192.168.2.3
#!/bin/bash
read -p "请输如新密码:" pass
for i in 'cat /homework2/host.txt'
do
ssh $i "echo $pass | passwd --stdin root "
done