一.编写脚本for1.sh,使用for循环创建20账户,账户名前缀由用户从键盘输入,账户初始密码由用户输入,例如: test1、test2、test3、.....、 test10
1.创建脚本for1.sh
[root@server ~]# vim for1.sh
2.编写脚本for1.sh
3.执行脚本for1.sh
[root@server ~]# bash for1.sh
请输入用户账户名前缀 andy
请输入用户的密码 123456
4.测试是否创建成功
二.编写脚本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.编写脚本for2.sh
2.执行脚本for2.sh
3.查看结果
root@server ~]# cat /tmp/host_up.txt
192.168.111.128
[root@server ~]# cat /tmp/host_down.txt
192.168.111.125
192.168.111.126
192.168.111.127
192.168.111.129
192.168.111.130
192.168.111.131
192.168.111.132
192.168.111.133
192.168.111.134
192.168.111.135
三.使用for循环实现批量主机root密码的修改
(1)打开多台主机
(2)使用ssh-keygen命令建立密钥对
(3)多台主机间通过ssh-copy-id进行免密登录
(4)编写脚本for3.sh,通过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): 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:9ndQyS81xfxmf1Pp5QBYbYnQtIB6lOpnFYn8zz9hbrs root@server
The key's randomart image is:
+---[RSA 3072]----+
| . +oBoo +.|
| * +.+o+.+|
| + . ..o+.+|
| o . o ..+B|
| . .S. o. o==|
| ..o. o.o++|
| o . .+..o|
| . .= |
| .E+ |
+----[SHA256]-----+
2.将产生的密钥发送给目标主机
[root@server ~]# ssh-copy-id root@192.168.111.130
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
The authenticity of host '192.168.111.130 (192.168.111.130)' can't be established.
ED25519 key fingerprint is SHA256:CqaJjGT+w+4B9rnNqQA2s8IzvInLUFRXO2+cGwsLUKU.
This key is not known by any other names
Are you sure you want to continue connecting (yes/no/[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.111.130's password:
Number of key(s) added: 1
Now try logging into the machine, with: "ssh 'root@192.168.111.130'"
and check to make sure that only the key(s) you wanted were added.
3.创建一个目的主机IP所在文件
[root@server ~]# vim ip.txt
4.编写脚本
5.测试
完成