shell第四天练习

题目:

1.for创建20用户,用户前缀由用户输入,用户初始密码由用户输入。
2.for ping测试指网段的主机,网段由用户输入。
3.使用for实现批量主机root密码的修改,成功或失败都必须记录。

1.for创建20用户,用户前缀由用户输入,用户初始密码由用户输入。

例如:test01,test10
1、编写脚本

[root@localhost day4]# vim add20user.sh
#!/bin/bash
read -p "please input username's prefix:" namepre
read -p "please input initial password:" passwd
count=20
for name in ${namepre}{01..20}
do
	useradd $name &> /dev/null
	if [ $? -eq 0 ];then
		echo $passwd | passwd $name --stdin &> /dev/null
	else
		echo "user $name already exists."
		let count--
	fi
done
echo "created $count users successfully"

2、设置权限

[root@localhost day4]# chmod a+rx add20user.sh

3、运行测试

[root@localhost day4]# ./add20user.sh
please input username's prefix:ccc
please input initial password:ccc
created 20 users successfully
[root@localhost day4]# tail -20 /etc/passwd
ccc01:x:1255:1255::/home/ccc01:/bin/bash
ccc02:x:1256:1256::/home/ccc02:/bin/bash
ccc03:x:1257:1257::/home/ccc03:/bin/bash
ccc04:x:1258:1258::/home/ccc04:/bin/bash
ccc05:x:1259:1259::/home/ccc05:/bin/bash
ccc06:x:1260:1260::/home/ccc06:/bin/bash
ccc07:x:1261:1261::/home/ccc07:/bin/bash
ccc08:x:1262:1262::/home/ccc08:/bin/bash
ccc09:x:1263:1263::/home/ccc09:/bin/bash
ccc10:x:1264:1264::/home/ccc10:/bin/bash
ccc11:x:1265:1265::/home/ccc11:/bin/bash
ccc12:x:1266:1266::/home/ccc12:/bin/bash
ccc13:x:1267:1267::/home/ccc13:/bin/bash
ccc14:x:1268:1268::/home/ccc14:/bin/bash
ccc15:x:1269:1269::/home/ccc15:/bin/bash
ccc16:x:1270:1270::/home/ccc16:/bin/bash
ccc17:x:1271:1271::/home/ccc17:/bin/bash
ccc18:x:1272:1272::/home/ccc18:/bin/bash
ccc19:x:1273:1273::/home/ccc19:/bin/bash
ccc20:x:1274:1274::/home/ccc20:/bin/bash
[root@localhost day4]# ./add20user.sh
please input username's prefix:ccc
please input initial password:ccc
user ccc01 already exists.
user ccc02 already exists.
user ccc03 already exists.
user ccc04 already exists.
user ccc05 already exists.
user ccc06 already exists.
user ccc07 already exists.
user ccc08 already exists.
user ccc09 already exists.
user ccc10 already exists.
user ccc11 already exists.
user ccc12 already exists.
user ccc13 already exists.
user ccc14 already exists.
user ccc15 already exists.
user ccc16 already exists.
user ccc17 already exists.
user ccc18 already exists.
user ccc19 already exists.
user ccc20 already exists.
created 0 users successfully

2.for ping测试指网段的主机,网段由用户输入。

例如用户输入192.168.2 ,则ping 192.168.2.10 — 192.168.2.20
UP: /tmp/host_up.txt
Down: /tmp/host_down.txt

准备工作:
创建相关的文件

[root@localhost day4]# touch /tmp/host_up.txt
[root@localhost day4]# touch /tmp/host_down.txt

1、编写脚本

[root@localhost day4]# vim forPing.sh

#!/bin/bash
read -p "please input network segment:" seg
for ip in ${seg}.{1..10}
do
	if ping -c2 $ip &> /dev/null ;then
		date >> /tmp/host_up.txt
		echo "$ip is up" >> /tmp/host_up.txt
	else
		date >> /tmp/host_down.txt
		echo "$ip is down" >> /tmp/host_down.txt
	fi
done

2、设置权限

[root@localhost day4]# chmod a+rx forPing.sh

3、运行测试

[root@localhost day4]# ./forPing.sh
please input network segment:192.168.210

[root@localhost day4]# cat /tmp/host_up.txt
20230106日 星期五 00:03:28 CST
192.168.210.2 is up

[root@localhost day4]# cat  /tmp/host_down.txt
20230106日 星期五 00:03:27 CST
192.168.210.1 is down
20230106日 星期五 00:03:31 CST
192.168.210.3 is down
20230106日 星期五 00:03:34 CST
192.168.210.4 is down
20230106日 星期五 00:03:38 CST
192.168.210.5 is down
20230106日 星期五 00:03:41 CST
192.168.210.6 is down
20230106日 星期五 00:03:44 CST
192.168.210.7 is down
20230106日 星期五 00:03:47 CST
192.168.210.8 is down
20230106日 星期五 00:03:50 CST
192.168.210.9 is down
20230106日 星期五 00:03:53 CST
192.168.210.10 is down

3.使用for实现批量主机root密码的修改,成功或失败都必须记录。

提示:主机IP存放在一个文件中
SSH:实现公钥认证,执行远程中主机命令
实现公钥认证
ssh-keygen 在用于管理的主上生成密钥对
ssh-copy-id -i 192.168.2.3

准备工作:
创建主机IP文件

[root@localhost day4]# vim host_ip.txt
192.168.210.5
192.168.210.6
192.168.210.7

1、编写脚本

[root@localhost day4]# vim chpwd_ssh.sh
#!/bin/bash
host_ip="/root/day4/host_ip.txt"
chpwd_log="/root/day4/chpwd_log.txt"
echo "checking key pair..."
if [ -f /root/.ssh/id_rsa -a -f /root/.ssh/id_rsa.pub ];then
	echo "key pair is ok"
else
	ssh-keygen -q -P "" -f /root/.ssh/id_rsa &> /dev/null && echo "key pair is created successfully"
fi
read -p "please input new password for hosts:" passwd
for line in `cat "$host_ip"`
do
	echo "changing $line's root password..."
	ssh-copy-id root@$line &> /dev/null && ssh root@$line "echo $passwd | passwd root --stdin" &> /dev/null
	if [ $? -eq 0 ];then
		echo "$line change password successfully"
		date >> "$chpwd_log"
		echo "$line change password successfully" >> "$chpwd_log"
	else
		echo "$line change password failed"
		date >> "$chpwd_log"
        echo "$line change password failed" >> "$chpwd_log"
	fi
done

2、设置权限

[root@localhost day4]# chmod a+rx chpwd_ssh.sh

3、运行测试
第一次修改密码(无免密):

[root@localhost day4]# ./chpwd_ssh.sh
checking key pair...
key pair is ok
please input new password for hosts:test
changing 192.168.210.5's root password...
192.168.210.5 change password failed
changing 192.168.210.6's root password...
192.168.210.6 change password failed
changing 192.168.210.128's root password...
The authenticity of host '192.168.210.128 (192.168.210.128)' can't be established.
ECDSA key fingerprint is SHA256:fUIFDTKDWHnEY9Pdl2go991nL40RiDHB+IXON7mTaPQ.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
root@192.168.210.128's password: 
192.168.210.128 change password successfully

第二次修改密码(免密):

[root@localhost day4]# ./chpwd_ssh.sh
checking key pair...
key pair is ok
please input new password for hosts:123456
changing 192.168.210.5's root password...
192.168.210.5 change password failed
changing 192.168.210.6's root password...
192.168.210.6 change password failed
changing 192.168.210.128's root password...
192.168.210.128 change password successfully

查看日志:

[root@localhost day4]# cat chpwd_log.txt
20230106日 星期五 00:19:40 CST
192.168.210.5 change password failed
20230106日 星期五 00:19:46 CST
192.168.210.6 change password failed
20230106日 星期五 00:20:45 CST
192.168.210.128 change password successfully
20230106日 星期五 00:21:38 CST
192.168.210.5 change password failed
20230106日 星期五 00:21:44 CST
192.168.210.6 change password failed
20230106日 星期五 00:21:45 CST
192.168.210.128 change password successfully
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值