自动化运维脚本语言shell练习(2)

1、编写脚本for1.sh,使用for循环创建20账户,账户名前缀由用户从键盘输入,账户初始密码由用户输入,例如: test1、test2、test3、.....、 test10
2、编写脚本for2.sh,使用for循环,通过ping命令测试网段的主机连通性,IP前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文件中
3、使用for循环实现批量主机root密码的修改
(1) 打开多台主机
(2) 使用ssh-keygen命令建立密钥对
(3) 多台主机间通过ssh-copy-id进行免密登录
(4) 编写脚本for3.sh,通过for循环登录主机修改对方root账户密码

目录

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

1.1编写脚本

1.2执行,并查看

 2、编写脚本for2.sh,使用for循环,通过ping命令测试网段的主机连通性,IP前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文件中

2.1编写脚本

2.2执行脚本,并查看

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

3.1将要连接的IP记录到一个文件中

3.2建立免密sshd登录

3.3编写脚本

3.4执行脚本,并查看结果


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

1.1编写脚本

[root@quantou ~]# 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    

1.2执行,并查看

[root@quantou ~]# bash for1.sh 
请输入用户账户名前缀: dianzandezuishuai
请输入中户的密码: 123456
[root@quantou ~]# cat  /etc/passwd  |  tail  -20 

 2、编写脚本for2.sh,使用for循环,通过ping命令测试网段的主机连通性,IP前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文件中

2.1编写脚本

[root@quantou ~]# vim for2.sh

#!/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

if  ping  -c 2 -w 3 $IP  &> /dev/null   ## -c后跟ping执行次数     -w后跟执行时间(时间不宜太短)

2.2执行脚本,并查看

[root@quantou ~]# bash for2.sh 
[root@quantou ~]# cat /tmp/host_up.txt
[root@quantou ~]# cat /tmp/host_down.txt

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

3.1将要连接的IP记录到一个文件中

[root@quantou ~]# vim ip.txt

192.168.50.131
192.168.50.133
##将需要连接的虚拟机IP写入名为ip.txt文件中

3.2建立免密sshd登录

[root@quantou ~]# ssh-keygen -t rsa   # 一路回车
[root@quantou ~]# ssh-copy-id  root@192.168.48.131   # 输入yes及密码
[root@quantou ~]# ssh-copy-id  root@192.168.48.132   # 输入yes及密码

3.3编写脚本

[root@quantou ~]# vim for3.sh

#!/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    

3.4执行脚本,并查看结果

[root@quantou ~]# bash for3.sh
请输入密码:123456
更改用户 root 的密码 。
passwd:所有的身份验证令牌已经成功更新。
更改用户 root 的密码 。
passwd:所有的身份验证令牌已经成功更新。
[root@quantou ~]# ssh root@192.168.50.131
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 23 14:15:39 2023 from 192.168.50.1

 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

捞起月亮的渔民QAQ

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值