shell

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

编写脚本

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

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

for ((i=1;i<=20;i++))
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 ~]# bash for1.sh 
请输入用户账户名前缀:test
请输入用户的密码:123456
[root@server ~]# cat /etc/passwd | tail -20
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 命令测试网段的主机连通性,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文件中

编写脚本

[root@server ~]# 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 down" >> /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 
请输入网段:192.168.62
up ip:
192.168.62.129 is up
192.168.62.129 is up

down ip:
192.168.62.125 is down
192.168.62.126 is down
192.168.62.127 is down
192.168.62.128 is down
192.168.62.130 is down
192.168.62.131 is down
192.168.62.132 is down
192.168.62.133 is down
192.168.62.134 is down
192.168.62.135 is down
192.168.62.125 is down
192.168.62.126 is down
192.168.62.127 is down
192.168.62.128 is down
192.168.62.130 is down
192.168.62.131 is down
192.168.62.132 is down
192.168.62.133 is down
192.168.62.134 is down
192.168.62.135 is down

3.使用for循环实现批量主机root密码的修改

(1)打开多台主机

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

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

(4)编写脚本for3.sh,通过for循环登录主机修改对方root账户密码

编写文本文件,输入ip地址

[root@server ~]# vim ip.txt
192.168.62.130
192.168.62.131

建立ssh免密登录

[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:mBGb2bJHayI+j/JqB/N9d79fSeUhTSmyjqNEI4EGyeE root@server
The key's randomart image is:
+---[RSA 3072]----+
|.o+ . .        ..|
|.o o . *   . .o. |
| E.   B o   o..o.|
|     . X . .  ..o|
|    . B S o    ..|
|  o. . = o .  . .|
|   +o.. . .    ..|
|  o o+... . .   .|
| ..=o .. . . .oo.|
+----[SHA256]-----+
#上传公钥
[root@server ~]# ssh-copy-id root@192.168.62.130
[root@server ~]# ssh-copy-id root@192.168.62.131

编写脚本

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

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

for i in `cat ip.txt`
do
        ssh $i "echo '$passwd' | passwd --stdin root"
done

测试,root的密码的修改

[root@server ~]# bash for3.sh 
请输入密码:123456
更改用户 root 的密码 。
passwd:所有的身份验证令牌已经成功更新。
更改用户 root 的密码 。
passwd:所有的身份验证令牌已经成功更新。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值