shell

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

[root@server ~]# vim  for1.sh
#!/bin/bash

read -p "请输入用户账户名前缀: "  prefix
read -p "请输入中户的密码: "  passwd

for ((i=1;i<=20;i++))     # 循环20次
do      
        user=$prefix$i    # 存储完整账户名
        if id $user  &>  /dev/null    # 检查账户是否存在
        then    
                echo  "$user 已存在"
        else    
                useradd  $user        # 创建账户
                if [ $? -eq 0 ]       # 若账户创建成功则设置密码
                then    
                        echo  "$passwd" | passwd  --stdin  $user  &> /dev/null
                else    
                        echo  "用户创建失败"
                        exit
                fi      
        fi      
done    
[root@server ~]# cat  /etc/passwd  |  tail  -20   # 查看结果

结果:

[root@server ~]# bash for1.sh
Please enter the account name prefix:test
Please enter your PIN:123456
[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 ,则pina 192.168.48.125 - 192.168.48.135,将可以ping通的主机工P
地址写入到 /tmp/host_up.txt文件中,不能ping通的主机工P地址写入到:/tmp/host_down .txt
文件中

#!/bin/bash

read -p "请输入网段:"  ip

for ((i=125;i<=135;i++))
do      
        IP="$ip"."$i"
        if  ping  -c 2 -w 3 $IP  &> /dev/null
        then    
                echo  "$IP is up"  >>  /tmp/host_up.txt
        else    
                echo  "$IP is up"  >>  /tmp/host_down.txt
        fi      
done    

echo  "up ip: "
cat  /tmp/host_up.txt

echo

echo "down ip :"
cat  /tmp/host_down.txt

测试

[root@server ~]# bash for2.sh
 请输入IP的前三个网段:192.168.60
[root@server ~]# cat /tmp/host_up.txt
192.168.60.128
[root@server ~]# cat /tmp/host_down.txt
192.168.60.125
192.168.60.126
192.168.60.127
192.168.60.129
192.168.60.130
192.168.60.131
192.168.60.132
192.168.60.133
192.168.60.134
192.168.60.135


3.使用for循环实现批量主机root密码的修改
(1)打开多台主机
(2)使用ssh-keygen命令建立密钥对
(3) 名台主机间通过ssh-coov-id进行免密沓录
(4)编写脚本for3.sh,通过for循坏登录主机修改对方root账户密码

 建立密钥

[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]-----+

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

[root@server ~]# ssh-copy-id root@192.168.60.129
The authenticity of host '192.168.60.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.60.129'"
and check to make sure that only the key(s) you wanted were added.
 
[root@server ~]# ssh root@192.168.60.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 22 12:33:06 2023
[root@Server-NFS-DNS ~]# exit
注销
Connection to 192.168.60.129 closed.

编写脚本

#!/bin/bash
[root@server ~]# vim  for3.sh
#!/bin/bash

read -p  "请输入密码:"  passwd

for  i in  `cat ip.txt`   # 循环读取文本中的IP地址
do      
        ssh $i  "echo '$passwd' | passwd --stdin  root"  # 远程登录修改密码
done    

测试

[root@server ~]# ssh-copy-id root@192.168.60.129
The authenticity of host '192.168.60.129 (192.168.60.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.60.129's password: 
 
Number of key(s) added: 1
 
Now try logging into the machine, with:   "ssh 'root@192.168.60.129'"
and check to make sure that only the key(s) you wanted were added.
 
[root@server ~]# ssh root@192.168.60.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 22 12:33:06 2023
[root@Server-NFS-DNS ~]# exit
注销
Connection to 192.168.60.129 closed.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值