一、作业要求
二、作业内容
1、(1)编写脚本
[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
测试
[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
2、(1)编写脚本
[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
测试
[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
3、(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)编写脚本
[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
测试
[root@server ~]# bash for3.sh
请输入密码:654321
更改用户 root 的密码 。
passwd:所有的身份验证令牌已经成功更新。
更改用户 root 的密码 。
passwd:所有的身份验证令牌已经成功更新。