- 使用case实现成绩优良差的判断
vim 1.sh
#!/bin/bash
read -p "please input your score:" score
case $score in
[0-9]|[1-5][0-9])
echo "Grade is C"
;;
[6-8][0-9])
echo "Grade is B"
;;
9[0-9]|100)
echo "Grade is A"
;;
esac
bash 1.sh
- for创建20用户
用户前缀由用户输入
用户初始密码由用户输入
例如:test01,test10
vim 2.sh
#!/bin/bash
read -p "pleace input user prefix:" a
read -p "pleace input user postfix:" b
for i in {01..20}
do
user=$a$i
password=$b$i
if ! id -u $user &> /dev/null
then
useradd $user
echo $password | passwd --stdin $user &>/dev/null
else
echo "user is exist"
fi
done
bash 2.sh
-
for ping测试指网段的主机
网段由用户输入,例如用户输入192.168.2 ,则ping 192.168.2.10 — 192.168.2.20
UP: /tmp/host_up.txt
Down: /tmp/host_down.txt
vim 3.sh #!/bini/bash read -p "pleace input ip network segment" a for i in {2..4} do ip="$a.$i" echo $ip if ping -c 2 $ip &>/dev/null then echo "$ip is up" >> /tmp/host_up.txt else echo "$ip is down" >> /tmp/host_down.txt fi done bash 3.sh
-
使用for实现批量主机root密码的修改
成功或失败都必须记录
提示:主机IP存放在一个文件中 SSH:
实现公钥认证,执行远程中主机命令
实现公钥认证 # ssh-keygen 在用于管理的主上生成密钥对
ssh-copy-id -i 192.168.2.3
vim 4.sh
#!/bin/bash
ssh-keygen -f /root/.ssh/id_rsa -P '' -q &>/dev/null
for ip in `cat ipfile`
do
echo $ip
ssh-copy-copy-id root@$ip
done
for ip in `cat ipfile`
do
echo $ip
ssh root@ip "echo redhat | passwd --stdin root" &>/dev/null
if [ "$?" -eq 0 ];then
echo "host $ip successfully update passwd"
else
echo "host $ip error update passwd"
fi
done
bash 4.sh