目录
1.编写脚本for1.sh,使用for循环创建20账户,账户名前缀由用户从键盘输入,账户初始密码由用户输入,例如: test1、test2、test3、.....、 test10
(1)编写脚本
(2)运行脚本
(3)查看是否创建成功
2.编写脚本for2.sh,使用for循环,通过ping命令测试网段的主机连通性,IP前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)编写脚本
(2)运行脚本
(3)查看结果
3.使用for循环实现批量主机root密码的修改
(1)建立密钥
(2)将产生的密钥发送给目标主机
(3)测试是否能连接上
(4)创建一个目的主机IP所在文件
(5)编写脚本
(6)执行
1.编写脚本for1.sh,使用for循环创建20账户,账户名前缀由用户从键盘输入,账户初始密码由用户输入,例如: test1、test2、test3、.....、 test10
(1)编写脚本
[root@server ~]# vim for1.sh
#!/bin/bash
read -p "请输入账户名前缀:" user
read -p "请输入密码" newpasswd
for ((i=1;i<=20;i++))
do
if ! id -u $user$i &> /dev/null
then
useradd $user$i
echo "$newpassswd" | passwd --stdin $user$i &> /dev/null
else
echo "$user$i is exists....."
fi
done
(2)运行脚本
[root@server ~]# sh for1.sh
请输入账户名前缀:test
亲输入密码123456
[root@server ~]#
[root@server ~]# sh for1.sh
请输入账户名前缀:test
亲输入密码123456
test1 is exists.....
test2 is exists.....
test3 is exists.....
test4 is exists.....
test5 is exists.....
test6 is exists.....
test7 is exists.....
test8 is exists.....
test9 is exists.....
test10 is exists.....
test11 is exists.....
test12 is exists.....
test13 is exists.....
test14 is exists.....
test15 is exists.....
test16 is exists.....
test17 is exists.....
test18 is exists.....
test19 is exists.....
test20 is exists.....
(3)查看是否创建成功
[root@server ~]# cat /etc/passwd
2.编写脚本for2.sh,使用for循环,通过ping命令测试网段的主机连通性,IP前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)编写脚本
[root@server ~]# vim for2.sh
#!/bin/bash
read -p " 请输入IP的前三个网段:" IP
for ip in {125..135}
do
ping -c 2 -w 3 $IP.$ip &> /dev/null
num=$?
if [ $num -eq 0 ]
then
echo "$IP.$ip" >> /tmp/host_up.txt
else
echo "$IP.$ip" >> /tmp/host_down.txt
fi
done
(2)运行脚本
[root@server ~]# sh for2.sh
请输入IP的前三个网段:192.168.183
(3)查看结果
[root@server ~]# cat /tmp/host_up.txt
192.168.183.128
[root@server ~]# cat /tmp/host_down.txt
192.168.183.125
192.168.183.126
192.168.183.127
192.168.183.129
192.168.183.130
192.168.183.131
192.168.183.132
192.168.183.133
192.168.183.134
192.168.183.135
3.使用for循环实现批量主机root密码的修改
(1)建立密钥
[root@server ~]# ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Created directory '/root/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa
Your public key has been saved in /root/.ssh/id_rsa.pub
The key fingerprint is:
SHA256:W5FeCG66egoOYXOuKjSwTIxB21UwaSPWz3FhWckQ5I0 root@server
The key's randomart image is:
+---[RSA 3072]----+
|.. .++.o*B.. |
|. oo.=.oo+o= |
|oo..o + =E+.. |
|oo = . o |
|=+ . . S o |
|o=+ . o |
|o o. . . |
|.o.. .. |
|+.. oo |
+----[SHA256]-----+
[root@server ~]#
(2)将产生的密钥发送给目标主机
[root@server ~]# ssh-copy-id root@192.168.183.128
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
The authenticity of host '192.168.183.128 (192.168.183.128)' can't be established.
ED25519 key fingerprint is SHA256:MK3vrSRPaKvJHtkQrrGnUqHK2sKzyWj6PCwnZwC6GkY.
This key is not known by any other names
Are you sure you want to continue connecting (yes/no/[fingerprint])? y
Please type 'yes', 'no' or the fingerprint: yws^H^H
Please type 'yes', 'no' or the fingerprint: yes
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@192.168.183.128's password:
Number of key(s) added: 1
Now try logging into the machine, with: "ssh 'root@192.168.183.128'"
and check to make sure that only the key(s) you wanted were added.
[root@server ~]# ssh root@192.168.183.128
Activate the web console with: systemctl enable --now cockpit.socket
Register this system with Red Hat Insights: insights-client --register
Create an account or view all your systems at https://red.ht/insights-dashboard
Last login: Sat Apr 22 00:30:02 2023 from 192.168.183.1
[root@server ~]#
(3)测试是否能连接上
[root@server ~]# ssh root@192.168.183.129
The authenticity of host '192.168.183.129 (192.168.183.129)' can't be established.
ED25519 key fingerprint is SHA256:MK3vrSRPaKvJHtkQrrGnUqHK2sKzyWj6PCwnZwC6GkY.
This host key is known by the following other names/addresses:
~/.ssh/known_hosts:1: 192.168.183.128
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added '192.168.183.129' (ED25519) to the list of known hosts.
root@192.168.183.129's password:
Activate the web console with: systemctl enable --now cockpit.socket
Register this system with Red Hat Insights: insights-client --register
Create an account or view all your systems at https://red.ht/insights-dashboard
Last login: Sat Apr 22 01:05:08 2023 from 192.168.183.1
[root@node1 ~]#
(4)创建一个目的主机IP所在文件
[root@server ~]# vim ipfile
(5)编写脚本
[root@server ~]# vim for3.sh
#!/bin/bash
for ip in `cat ipfile`
do
echo $ip
ssh root@$ip "echo rhce | passwd --stdin root" &>/dev/null
if [ "$?" -eq 0 ]
then
echo "host $ip successfully update passwd"
else
echo "host $ip error update passwd"
fi
done
(6)执行
[root@node1 ~]# sh for3.sh
192.168.183.129
The authenticity of host '192.168.183.129 (192.168.183.129)' can't be established.
ED25519 key fingerprint is SHA256:MK3vrSRPaKvJHtkQrrGnUqHK2sKzyWj6PCwnZwC6GkY.
This key is not known by any other names
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
root@192.168.183.129's password:
host 192.168.183.129 successfully update passwd