批量更改用户口令

雨燕双飞 提交于 2019-12-05 17:34:29

--------prochapawd.sh---------

#!/usr/bin/expect

set timeout 5

set ipaddress      [lindex $argv 0]
set username       [lindex $argv 1]
set newpassword    [lindex $argv 2]

spawn ssh -l root $ipaddress

expect "*#*"                {send "hostname\r"}
expect "*#*"                {send "passwd $username\r"}
expect "*assword: "         {send "$newpassword\r"}
#AIX,this
#expect "again:"             {send "$newpassword\r"}
#Linux,UNIX,this
expect "*assword: "         {send "$newpassword\r"} 
expect "*#*"                {send "exit\r"}

expect eof

--------prochapawdexecute.sh---------

#!/bin/bash

echo -n "Enter the user whose password you want to change: "
read user

echo -n "Enter the user's new password: "
read newpassword

if [ -n "$newpassword" ] && [ -n "$user" ]; then
    echo "Passwd $user will be executed."
else
    echo "The password you enter is null."
    exit 1
fi

for ip in $(cat iplist);
do {
    ssh ${ip} "id $user"
    if [ $? -eq 0 ]; then
        /usr/bin/expect prochapawd.sh $ip $user $newpassword
    else
        continue
    fi
}
done

exit 0
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!