记一次Shell脚本练习_批量主机root密码修改

北城以北 提交于 2020-03-08 23:03:00

操作环境:CentOS 7

[root@localhost ~]# cat /etc/redhat-release 
CentOS Linux release 7.7.1908 (Core)
[root@localhost ~]# 

条件:
1、可以ssh免密登陆
2、把需要修改root密码的主机ip地址,写进当前目录下的ip.txt

#!/usr/bin/bash
#create by liujun at 2020-03-08

#enter the change password
while :
do
read -s -p "Enter a New Password:" pass1
echo ""
read -s -p "Enter Your Password Agine:" pass2
if [ ! $pass1 == $pass2 ];then
echo ""
echo "The New password do not match,please re-enter"
else
        break
fi
done
echo ""
echo "Entered successfully!"

#make sure the host is powered on and change password 
for ip in $(cat ip.txt)
do
        {
                ping -c1 -W1 $ip &> /dev/null
                if [ $? -eq 0 ];then
                        ssh $ip "echo $pass1 | passwd --stdin root"
                        if [ $? -eq 0 ];then
                                echo "$ip" >> ok_`date +%F`.txt
                        else
                                echo "$ip" >> fail_`date +%F`.txt
                        fi
                else
                        echo "$ip" >>unonline_`date +%F`.txt
                fi
}&
done
wait
echo "finish......"
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!