SHELL中for循环和if判断的使用

   1.编写脚本使用for创建20用户,账户名前缀由键盘输入,账户密码用用户输入。例如text1.text2......text10.

创建脚本text1.

read -p "请输入账户名前缀:" user
read -p "亲输入密码" newpasswd
for ((i=1;i<=20;i++))
do
        if ! id -u $user$i &> /dev/null
        then
                useradd $user$i
                echo "$newpassswd" | passwd --stdin $user$i &> /dev/null
        else
                echo "$user$i is exists....."
        fi
done

执行脚本

[root@bogon cb123456]# sh for 1.sh
请输入账户名前缀:text
请输入密码123456
text1 is exists.....
text2 is exists.....
text3 is exists.....
text4 is exists.....
text5 is exists.....
text6 is exists.....
text7 is exists.....
text8 is exists.....
text9 is exists.....
text10 is exists.....
text11 is exists.....
text12 is exists.....
text13 is exists.....
text14 is exists.....
text15 is exists.....
text16 is exists.....
text17 is exists.....
text18 is exists.....
text19 is exists.....
text20 is exists.....

查看是否存在

cat /etc/passwd

 编写for2.sh使用for命令。使用ping命令测试网段主机连接性,网段前三段由用户输入

1编写脚本

vim for2.sh
#!/bin/bash
 
read -p " 请输入IP的前三个网段:" IP

for ip in {125..135}
do
        ping -c 2 -w 3 $IP.$ip  &> /dev/null
        num=$?
        if [  $num -eq 0 ]
        then
                echo "$IP.$ip" >> /tmp/host_up.txt
        else
                echo "$IP.$ip" >> /tmp/host_down.txt
        fi
done

执行

[root@bogon cb123456]# sh for2.sh
 请输入IP的前三个网段:192.168.42

查看

[root@bogon cb123456]# cat /tmp/host_up.txt
192.168.42.128
[root@bogon cb123456]# cat /tmp/host_down.txt
192.168.42.125
192.168.42.126
192.168.42.127
192.168.42.128
192.168.42.129
192.168.42.130
192.168.42.131
192.168.42.132
192.168.42.133
192.168.42.134
192.168.42.135
192.168.42.125
192.168.42.126
192.168.42.127
192.168.42.129
192.168.42.130
192.168.42.131
192.168.42.132
192.168.42.133
192.168.42.134
192.168.42.135

3实现用for循环实现对主机密码的修改

1打开多台主机

2使用ssh-keygen命令建立密钥配对

3多台主机通过ssh-copy-id进行免密登录

4编写脚本for3.sh通过for循环更改用户密码

首先建立密钥配对

[root@bogon cb123456]# ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): cb123456
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in cb123456
Your public key has been saved in cb123456.pub
The key fingerprint is:
SHA256:Z1tDGSiuzdY3mcpVl5XsI5OAdsbbvbAw/gpClb28Xus root@bogon
The key's randomart image is:
+---[RSA 3072]----+
|          o.. . .|
|        .o+= o o.|
|       ..+o.* + o|
|        o .=.*.=.|
|       =S.+o=+=.o|
|      o +o.+Bo . |
|       o o.=.o   |
|        . = ...  |
|           ooE   |
+----[SHA256]-----+
[root@bogon cb123456]# 

将产生的密钥发送给主机

[root@localhost cb123456]# ssh-copy-id root@192.168.42.128
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
/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.42.128's password: 

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh 'root@192.168.42.128'"
and check to make sure that only the key(s) you wanted were added.

测试

[root@localhost cb123456]# ssh root@192.168.42.128
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: Sat Apr 22 12:38:00 2023 from 192.168.42.132

创建一个主机ip所在文件

vim ipfile
192.168.42.128
192.168.42.132

 编写脚本

[root@bogon cb123456]# vim for3.sh
#!/bin/bash
 
for ip in `cat ipfile`
do
        echo $ip
        ssh root@$ip "echo rhce | passwd --stdin root" &>/dev/null
        if [ "$?" -eq 0 ]
        then
                echo "host $ip successfully update passwd"
        else
                echo "host $ip error update passwd"
        fi
done

执行

[root@bogon cb123456]# sh for3.sh
192.168.42.128
host 192.168.42.132 successfully update passwd
[root@localhost cb123456]# sh for3.sh
192.168.42.128
host 192.168.42.132 error update passwd

128无主机报错

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值