RECH网络服务第六次作业

RECH网络服务第六次作业文章展示了如何使用 Linuxshell 脚本自动化执行任务 包括通过 for 循环创建带有特定前缀的 20 个用户账户并设置密码 测试指定网段的主机连通性并将结果分别写入文件 以及使用 SSH 和密钥对实现多台主机间的免密登录并批量修改 root 密码

大家好,欢迎来到IT知识分享网。

RECH网络服务第六次作业

 1、编写脚本for1.sh,使用for循环创建20账户,账户名前缀由用户从键盘输入,账户初始密码由用户输入,例如: test1、test2、test3、…..、 test10

[root@server ~]# vim for1.sh 

RECH网络服务第六次作业

 

[root@server ~]# bash for1.sh Please enter the account name prefix:test Please enter your PIN: [root@server ~]# cat /etc/passwd test1:x:1001:1001::/home/test1:/bin/bash test2:x:1002:1002::/home/test2:/bin/bash test3:x:1003:1003::/home/test3:/bin/bash test4:x:1004:1004::/home/test4:/bin/bash test5:x:1005:1005::/home/test5:/bin/bash test6:x:1006:1006::/home/test6:/bin/bash test7:x:1007:1007::/home/test7:/bin/bash test8:x:1008:1008::/home/test8:/bin/bash test9:x:1009:1009::/home/test9:/bin/bash test10:x:1010:1010::/home/test10:/bin/bash test11:x:1011:1011::/home/test11:/bin/bash test12:x:1012:1012::/home/test12:/bin/bash test13:x:1013:1013::/home/test13:/bin/bash test14:x:1014:1014::/home/test14:/bin/bash test15:x:1015:1015::/home/test15:/bin/bash test16:x:1016:1016::/home/test16:/bin/bash test17:x:1017:1017::/home/test17:/bin/bash test18:x:1018:1018::/home/test18:/bin/bash test19:x:1019:1019::/home/test19:/bin/bash test20:x:1020:1020::/home/test20:/bin/bash 

 2、编写脚本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文件中

 RECH网络服务第六次作业

[root@server ~]# bash for2.sh 请输入IP的前三个网段:192.168.121 [root@server ~]# cat /tmp/host_up.txt 192.168.121.128 [root@server ~]# cat /tmp/host_down.txt 192.168.121.125 192.168.121.126 192.168.121.127 192.168.121.129 192.168.121.130 192.168.121.131 192.168.121.132 192.168.121.133 192.168.121.134 192.168.121.135 

3、使用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): 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:51mVwUitAPCcVovCm9+urd+EzOoi1r/glNiwdALkSMs root@server The key's randomart image is: +---[RSA 3072]----+ | .. .......+. | |o+. . o +... oo | |.Eo o * .. .o | | . = .. | | + + S . . | | . B o * + | | o.= . B . | | ooo. + o | | . ..+B=+ . | +----[SHA256]-----+ 

2)将产生的密钥发送到目标主机并且测试

[root@server ~]# ssh-copy-id root@192.168.121.129 The authenticity of host '192.168.121.129 (192.168.121.129)' can't be established. ED25519 key fingerprint is SHA256:RE4azvFRBh/5D/XDeSN421FTZRKLvoJLPwvQY/ZwFt8. 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.121.129's password: Number of key(s) added: 1 Now try logging into the machine, with: "ssh 'root@192.168.121.129'" and check to make sure that only the key(s) you wanted were added. [root@server ~]# ssh root@192.168.121.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: Fri Apr 21 23:33:06 2023 [root@Server-NFS-DNS ~]# exit 注销 Connection to 192.168.121.129 closed. 

3)编写脚本

RECH网络服务第六次作业

4) 测试

[root@server ~]# bash for3.sh cat: ipfile: 没有那个文件或目录 [root@server ~]# vim ipfile [root@server ~]# vim for3.sh [root@server ~]# bash for3.sh 192.168.121.128 The authenticity of host '192.168.121.128 (192.168.121.128)' can't be established. ED25519 key fingerprint is SHA256:RE4azvFRBh/5D/XDeSN421FTZRKLvoJLPwvQY/ZwFt8. This host key is known by the following other names/addresses: ~/.ssh/known_hosts:1: 192.168.121.129 Are you sure you want to continue connecting (yes/no/[fingerprint])? yes root@192.168.121.128's password: host 192.168.121.128 successfully update passwd 192.168.121.129 host 192.168.121.129 successfully update passwd [root@server ~]# 

 [root@server ~]# vim ipfile    #内容如下

RECH网络服务第六次作业

免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://yundeesoft.com/131447.html

(0)
上一篇 2024-11-23 11:00
下一篇 2024-11-23 11:15

相关推荐

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注微信