shell脚本--for循环

[root@server ~]# vim for1.sh

#!/bin/bash


read -p "请输入用户名:" username
read -p "请输入用户密码:" password

for ((i=1,i<=20;i++))
do      
        user=$username$i
        if id $user  &>  /dev/null
        then    
                echo "$user用户已存在"
        else    
                useradd $user
                if [ $? -eq 0 ]
                then    
                        echo "$password"  | passwd --stdin $user &> /dev/null
                else    
                        echo "用户创建失败"
                        exit
                fi      
        fi
done    
[root@server ~]# bash for1.sh
请输入用户名:liming
请输入用户密码:123456
[root@server ~]# tail -20 /etc/passwd
liming1:x:1001:1001::/home/liming1:/bin/bash
liming2:x:1002:1002::/home/liming2:/bin/bash
liming3:x:1003:1003::/home/liming3:/bin/bash
liming4:x:1004:1004::/home/liming4:/bin/bash
liming5:x:1005:1005::/home/liming5:/bin/bash
liming6:x:1006:1006::/home/liming6:/bin/bash
liming7:x:1007:1007::/home/liming7:/bin/bash
liming8:x:1008:1008::/home/liming8:/bin/bash
liming9:x:1009:1009::/home/liming9:/bin/bash
liming10:x:1010:1010::/home/liming10:/bin/bash
liming11:x:1011:1011::/home/liming11:/bin/bash
liming12:x:1012:1012::/home/liming12:/bin/bash
liming13:x:1013:1013::/home/liming13:/bin/bash
liming14:x:1014:1014::/home/liming14:/bin/bash
liming15:x:1015:1015::/home/liming15:/bin/bash
liming16:x:1016:1016::/home/liming16:/bin/bash
liming17:x:1017:1017::/home/liming17:/bin/bash
liming18:x:1018:1018::/home/liming18:/bin/bash
liming19:x:1019:1019::/home/liming19:/bin/bash
liming20:x:1020:1020::/home/liming20:/bin/bash

[root@server ~]# vim for2.sh
#!/bin/bash
read -p "请输入IP地址前三段:" IP

for ((i=128;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 down" >> /tmp/host_down.txt
        fi
done

echo "在线的主机:"
cat /tmp/host_up.txt

echo "不在线的主机:"
cat /tmp/host_down.txt
[root@server ~]# bash for2.sh
请输入IP地址前三段:192.168.253
在线的主机:
192.168.253.128 is up
192.168.253.129 is up
不在线的主机:
192.168.253.130 is down
192.168.253.131 is down
192.168.253.132 is down
192.168.253.133 is down
192.168.253.134 is down
192.168.253.135 is down

准备工作

(1)打开三台主机

(2)创建密钥对

[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:yiDwQ8GplsWfItHqou7kPXkXlwMl/SOfKPACSXcv5fQ root@server
The key's randomart image is:
+---[RSA 3072]----+
| .+.    .        |
| .o* . o =       |
|..B + o B o      |
|.O + + o + E     |
|o = + o S = o    |
|.. o + = = o     |
|o.  . + + .      |
|+ .o . .         |
|o+ .o .          |
+----[SHA256]-----+

(3)创建地址文件

[root@server ~]# vim ip.txt

192.168.253.129
192.168.253.129
192.168.253.130

(4)将公钥上传至目标主机

[root@server ~]# ssh-copy-id root@192.168.253.129
[root@server ~]# ssh-copy-id root@192.168.253.130

(5)编写脚本for3.sh来修改root密码

[root@server ~]# vim for3.sh
#!/bin/bash
read -p "请输入密码:" password

for i in $(cat ip.txt)
do
        ssh $i "echo '$password'  | passwd --stdin root "
done
[root@server ~]# bash for3.sh 
请输入密码:1234567
更改用户 root 的密码 。
passwd:所有的身份验证令牌已经成功更新。
更改用户 root 的密码 。
passwd:所有的身份验证令牌已经成功更新。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值