--------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 "} expect "*#*" {send "passwd $username "} expect "*assword: " {send "$newpassword "} #AIX,this #expect "again:" {send "$newpassword "} #Linux,UNIX,this expect "*assword: " {send "$newpassword "} expect "*#*" {send "exit "} 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