目录
1.编写脚本for1.sh,使用for循环创建20账户,账户名前缀由用户从键盘输入,账户初始密码由用户输入,例如:test1、test2、test3、、test10
a.编辑脚本
b.运行脚本
c.进行检验
2.编写脚本for2.sh,使用for循环,通过pig命令测试网段的主机连通性,网段前3段由用户输入,如:输入192.168.48,则ping192.168.48.125-192.168.48.135,将可以ping通的主机IP地址写入到/tmp/host_up.txt文件中,不能ping通的主机IP地址写入到:/tmp/host_down.txt文件中
a.编写脚本
b.运行脚本
3.使用for循环实现批量主机root密码的修改
(1)打开多台主机,创建一个文件存放主机IP
(2)使用ssh-keygen命令建立密钥对
(3)多台主机间通过ssh-copy-id进行免密登录
(4)编写脚本for3.sh,通过for循环登录主机修改对方root账户密码
1.编写脚本for1.sh,使用for循环创建20账户,账户名前缀由用户从键盘输入,账户初始密码由用户
输入,例如:test1、test2、test3、、test10
a.编辑脚本
[root@srever ~]# vim for1.sh
b.运行脚本
[root@srever ~]# bash for1.sh
请输入用户账户名前缀: lisa
请输入用户的密码: 123456
c.进行检验
[root@srever ~]# cat /etc/passwd | tail -20
lisa1:x:1001:1001::/home/lisa1:/bin/bash
lisa2:x:1002:1002::/home/lisa2:/bin/bash
lisa3:x:1003:1003::/home/lisa3:/bin/bash
lisa4:x:1004:1004::/home/lisa4:/bin/bash
lisa5:x:1005:1005::/home/lisa5:/bin/bash
lisa6:x:1006:1006::/home/lisa6:/bin/bash
lisa7:x:1007:1007::/home/lisa7:/bin/bash
lisa8:x:1008:1008::/home/lisa8:/bin/bash
lisa9:x:1009:1009::/home/lisa9:/bin/bash
lisa10:x:1010:1010::/home/lisa10:/bin/bash
lisa11:x:1011:1011::/home/lisa11:/bin/bash
lisa12:x:1012:1012::/home/lisa12:/bin/bash
lisa13:x:1013:1013::/home/lisa13:/bin/bash
lisa14:x:1014:1014::/home/lisa14:/bin/bash
lisa15:x:1015:1015::/home/lisa15:/bin/bash
lisa16:x:1016:1016::/home/lisa16:/bin/bash
lisa17:x:1017:1017::/home/lisa17:/bin/bash
lisa18:x:1018:1018::/home/lisa18:/bin/bash
lisa19:x:1019:1019::/home/lisa19:/bin/bash
lisa20:x:1020:1020::/home/lisa20:/bin/bash
2.编写脚本for2.sh,使用for循环,通过pig命令测试网段的主机连通性,网段前3段由用户输入,
如:输入192.168.48,则ping192.168.48.125-192.168.48.135,将可以ping通的主机IP
地址写入到/tmp/host_up.txt文件中,不能ping通的主机IP地址写入到:/tmp/host_down.txt
文件中
a.编写脚本
[root@srever ~]# vim for2.sh
b.运行脚本
[root@srever ~]# bash for2.sh
请输入网段:192.168.154
up ip:
192.168.154.128 is up
192.168.154.129 is up
down ip :
192.168.154.125 is up
192.168.154.126 is up
192.168.154.127 is up
192.168.154.130 is up
192.168.154.131 is up
192.168.154.132 is up
192.168.154.133 is up
192.168.154.134 is up
192.168.154.135 is up
3.使用for循环实现批量主机root密码的修改
(1)打开多台主机,创建一个文件存放主机IP
[root@srever ~]# vim ip.txt
192.168.154.129
(2)使用ssh-keygen命令建立密钥对
[root@srever ~]# 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:aOWjqiz9+jFpFUnSSl+C1SQ3XlUY6vjVl5hRCtgDCyU root@srever
The key's randomart image is:
+---[RSA 3072]----+
| .++E=o=oo+.. |
| ooo==+.+o o |
| . oooo.. .o |
| . .= o .+ .|
| + S . .o...|
| + . o . . |
| . = . . |
|.... + |
| .+=+ |
+----[SHA256]-----+
(3)多台主机间通过ssh-copy-id进行免密登录
[root@srever ~]# ssh-copy-id root@192.168.154.129
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
The authenticity of host '192.168.154.129 (192.168.154.129)' can't be established.
ED25519 key fingerprint is SHA256:uQorZCoLEhNo95SxCTX7DZJFlIBsQrE6RauI4AMvLWk.
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: 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.154.129's password:
Number of key(s) added: 1
Now try logging into the machine, with: "ssh 'root@192.168.154.129'"
and check to make sure that only the key(s) you wanted were added.
进行验证
[root@srever ~]# ssh 192.168.154.129
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: Sun Apr 16 13:52:21 2023 from 192.168.154.1
[root@srever ~]#
(4)编写脚本for3.sh,通过for循环登录主机修改对方root账户密码
[root@srever ~]# vim for3.sh
运行脚本
[root@srever ~]# bash for3.sh
请输入密码:123456
更改用户 root 的密码 。
passwd:所有的身份验证令牌已经成功更新。