一.编写脚本for1.sh,使用for循环创建20账户,账户名前缀由用户从键盘输入,账户初始密码由用户输入,例如: test1、test2、test3、.....、 test10
1.编写shell脚本
[root@server ~]#vim for1.sh
#!/bin/bash
read -p "请输入用户账户名前缀:" prefix
read -p "请输入用户密码:" passwd
for((i=1;i<=20;i++))
do
user=$prefix$i
if id $user &> /dev/null
then
echo "$user 已存在"
else
useradd $user
if [ $? -eq 0 ]
then
echo "$passwd" | passwd --stdin $user &> /dev/null
else
echo "用户创建失败"
exit
fi
fi
done
2.进行测试
[root@server ~]# bash for1.sh
请输入用户账户名前缀:abc
请输入用户密码:123456
[root@server ~]# cat /etc/passwd | tail -20
abc1:x:1001:1001::/home/abc1:/bin/bash
abc2:x:1002:1002::/home/abc2:/bin/bash
abc3:x:1003:1003::/home/abc3:/bin/bash
abc4:x:1004:1004::/home/abc4:/bin/bash
abc5:x:1005:1005::/home/abc5:/bin/bash
abc6:x:1006:1006::/home/abc6:/bin/bash
abc7:x:1007:1007::/home/abc7:/bin/bash
abc8:x:1008:1008::/home/abc8:/bin/bash
abc9:x:1009:1009::/home/abc9:/bin/bash
abc10:x:1010:1010::/home/abc10:/bin/bash
abc11:x:1011:1011::/home/abc11:/bin/bash
abc12:x:1012:1012::/home/abc12:/bin/bash
abc13:x:1013:1013::/home/abc13:/bin/bash
abc14:x:1014:1014::/home/abc14:/bin/bash
abc15:x:1015:1015::/home/abc15:/bin/bash
abc16:x:1016:1016::/home/abc16:/bin/bash
abc17:x:1017:1017::/home/abc17:/bin/bash
abc18:x:1018:1018::/home/abc18:/bin/bash
abc19:x:1019:1019::/home/abc19:/bin/bash
abc20:x:1020:1020::/home/abc20:/bin/bash
二.编写脚本for2.sh,使用for循环,通过ping命令测试网段的主机连通性,网段前3段由用户输入,如: 输入192.168.48 则ping 192.168.48.125 - 192.168.48.135,将可以ping通的主机IP地址写入到 /tmp/host_up.txt文件中,不能ping通的主机IP地址写入到: /tmp/host_down.txt文件中
1.编写shell脚本
[root@server ~]# vim for2.sh
#!/bin/bash
read -p "请输入网段:" ip
for ((i=125;i<=135;i++))
do
IP="$ip"."$i"
if ping -c 2 -w 3 $IP &> /dev/null
then
echo "$IP is up" >> /tmp/host_up.txt
else
echo "$IP is up" >> /tmp/host_down.txt
fi
done
echo "up ip:"
cat /tmp/host_up.txt
echo
echo "down ip:"
cat /tmp/host_down.txt
2.进行测试
[root@server ~]# bash for2.sh
请输入网段:192.168.190
up ip:
192.168.190.134 is up
192.168.190.135 is up
down ip:
192.168.190.125 is up
192.168.190.126 is up
192.168.190.127 is up
192.168.190.128 is up
192.168.190.129 is up
192.168.190.130 is up
192.168.190.131 is up
192.168.190.132 is up
192.168.190.133 is up
192.168.190.134 is up
192.168.190.135 is up
192.168.190.125 is up
192.168.190.126 is up
192.168.190.127 is up
192.168.190.128 is up
192.168.190.129 is up
192.168.190.130 is up
192.168.190.131 is up
192.168.190.132 is up
192.168.190.133 is up
三.使用for循环实现批量主机root密码的修改
(1) 打开多台主机
(2)使用ssh-keygen命令建立密钥对
(3)多台主机间通过ssh-copy-id进行免密登录
(4)编写脚本for3.sh,通过for循环登录主机修改对方root账户密码
1.新建ip.txt用于存储远程连接的主机ip
[root@server ~]# vim ip.txt
192.168.190.135
192.168.190.136
2.建立免密登录
[root@server ~]# ssh-keygen -t rsa #一路回车
[root@server ~]# ssh-copy-id root@192.168.190.135
[root@server ~]# ssh-copy-id root@192.168.190.136
3.编写shell脚本
[root@server ~]# vim for3.sh
#!/bin/bash
read -p "请输入密码:" passwd
for i in `cat ip.txt`
do
ssh $i "echo '$passwd' | passwd --stdin root"
done
4.进行测试
[root@server ~]# bash for3.sh
请输入密码:654321
更改用户 root 的密码 。
passwd:所有的身份验证令牌已经成功更新。
更改用户 root 的密码 。
passwd:所有的身份验证令牌已经成功更新。