1.编写脚本for1.sh,使用for循环创建20账户,账户名前缀由用户从键盘输入,账户初始密码由用广输入,例如: test1、test2、test3、.....、 test10
2.编写脚本for2.sh,使用for循环,通过ping命令测试网段的主机连通性,IP前3段由用户输入,如: 输入192.168.48 ,则pin 192.168.48.125 - 192.168.48.135,将可以ping通的主机IP地址写入到 /tmp/host_up.txt文件中,不能ping通的主机IP地址写入到: /tmp/host_down.txt文件中
3.使用for循环实现批量主机root密码的修改
(1) 打开多台主机
(2) 使用ssh-keygen命令建立密钥对
(3) 多台主机间通过ssh-copy-id进行免密登录(4) 编写脚本for3.sh,通过for循环登录主机修改对方root账户密码
1.
[root@node2 ~]# vim for1.sh
[root@node2 ~]# bash for1.sh
请输入账户的前缀:test
请输入账户密码:123456
id: “test1”:无此用户
id: “test2”:无此用户
id: “test3”:无此用户
id: “test4”:无此用户
id: “test5”:无此用户
id: “test6”:无此用户
.........省略
2.
[root@node2 ~]# vim for2.sh
[root@node2 ~]# bash for2.sh
请输入一个网段:192.168.150 //(主机位)
up is :
cat: /tmp/host_up.txt: 没有那个文件或目录
down is :
192.168.150.131 未开机
192.168.150.132 未开机
192.168.150.133 未开机
192.168.150.134 未开机
3.
[root@node2 ~]# vim ip.txt //(往里面写两个ip想要实现免密登录的主机server,node1的ip)
192.168.150.128
192.168.150.129
[root@node2 ~]# 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:ge7k2jTN62vCyZkh3s83VSMZ2PtMh0mB1Aa5iL/kXbY root@node2
The key's randomart image is:
+---[RSA 3072]----+
| +o=.. |
| . . = + |
| . .. . B o |
| . ... = * .|
| o S. * o |
| .+.o o . = |
| . ===oo + o . |
| .+Oo..= . E |
| . .=*o . |
+----[SHA256]-----+
ssh-copy-id root@192.168.150.129 // 和(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.150.129 (192.168.150.129)' can't be established.
ED25519 key fingerprint is SHA256:JOkWin3VS/TLsMGCcvycf2iFOLuHk/gdfXfac7UZ0Dw.
This host key is known by the following other names/addresses:
~/.ssh/known_hosts:1: 192.168.150.128
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.150.129's password: //设置密码654321
Number of key(s) added: 1
Now try logging into the machine, with: "ssh 'root@192.168.150.129'"
and check to make sure that only the key(s) you wanted were added.
[root@node2 ~]# vim for3.sh
[root@node2 ~]# bash for3.sh
请输入你的密码:654321
更改用户 root 的密码 。
passwd:所有的身份验证令牌已经成功更新。
更改用户 root 的密码 。
passwd:所有的身份验证令牌已经成功更新。