1.使用case实现成绩优良差的判断
[root@localhost test]# vim score.sh
#!/bin/bash
read -p "please enter your score (0-100):" grade
case "$grade" in
8[0-9]|9[0-9]|100)
echo "优"
;;
6[0-9]|7[0-9])
echo "良"
;;
*)
echo "差"
esac
[root@localhost test]# chmod a+x score.sh
[root@localhost test]# ./score.sh
please enter your score (0-100):99
优
[root@localhost test]# ./score.sh
please enter your score (0-100):10
差
[root@localhost test]# ./score.sh
please enter your score (0-100):67
良
2.for创建20用户
用户前缀由用户输入
用户初始密码由用户输入
例如:test01,test10
[root@localhost test]# vim user20.sh
#!/bin/bash
read -p "输入用户前缀:" u
read -p "输入用户初始密码:" n
for i in {1..20}
do
useradd $u$i
echo "$u$i"
echo "$u$i:$n" | chpasswd
echo "Initial password changed successfully."
done
[root@localhost test]# chmod a+x user20.sh
[root@localhost test]# ./user20.sh
输入用户前缀:user
输入用户初始密码:123
user1
Initial password changed successfully.
user2
Initial password changed successfully.
user3
Initial password changed successfully.
user4
Initial password changed successfully.
user5
Initial password changed successfully.
user6
Initial password changed successfully.
user7
Initial password changed successfully.
user8
Initial password changed successfully.
user9
Initial password changed successfully.
user10
Initial password changed successfully.
user11
Initial password changed successfully.
user12
Initial password changed successfully.
user13
Initial password changed successfully.
user14
Initial password changed successfully.
user15
Initial password changed successfully.
user16
Initial password changed successfully.
user17
Initial password changed successfully.
user18
Initial password changed successfully.
user19
Initial password changed successfully.
user20
Initial password changed successfully.
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
[root@localhost test]# vim 10.sh
#!/bin/bash
read -p "请输入网段:" n
for i in {130..140}
do
if ping -c 3 $n.$i &> /dev/null
then
echo "$n.$i is up" >> /tmp/host_up.txt
else
echo "$n.$i is down" >> /tmp/host_down.txt
fi
done
[root@localhost test]# chmod a+x ping2.sh
[root@localhost test]# ./10.sh
请输入网段:192.168.223
[root@localhost test]# cat /tmp/host_down.txt
192.168.223.130 is down
192.168.223.131 is down
192.168.223.133 is down
192.168.223.134 is down
192.168.223.135 is down
192.168.223.136 is down
192.168.223.137 is down
192.168.223.138 is down
192.168.223.139 is down
192.168.223.140 is down
[root@localhost test]# cat /tmp/host_up.txt
192.168.223.132 is up
4.使用for实现批量主机root密码的修改
成功或失败都必须记录
提示:主机IP存放在一个文件中
SSH:实现公钥认证,执行远程中主机命令
实现公钥认证
# ssh-keygen 在用于管理的主上生成密钥对
# ssh-copy-id -i 192.168.2.3
[root@localhost test]# vim iplist
192.168.168.132 22
192.168.168.21 2000
[root@localhost test]# vim 4.sh
#!/bin/bash
num=`cat iplist | wc -l`
for((i=1;i<=$num;i++))
do
ip=`head -$i iplist | tail -l | awk '{print $1}'`
port=`head -$i iplist | tail -l | awk '{print $2}'`
ssh -p $port root@$ip echo 123 | passwd --stdin root
done
[root@localhost test]# chmod a+x 4.sh
[root@localhost test]# ./4.sh