ip.txt
192.168.0.105 123455 192.168.0.106 123456 192.168.0.107 123457 192.168.0.108 123458
passwd.sh
#!/bin/bash for ip in `awk ‘{print $1}‘ ip.txt` do pass=`grep $ip ip.txt |awk ‘{print $2}‘` echo $ip $pass expect action.exp $ip $pass done
action.exp
#!/bin/expect set ip [lindex $argv 0] set passwd [lindex $argv 1] set timeout 30 spawn ssh root@$ip expect { "yes/no" {send "yes\r";exp_continue} "password" {send "$passwd\r"} } expect "#" send "echo 408950 |passwd --stdin root\r" send "exit\r" expect eof
使用
[root@localhost ~]# sh passwd.sh 192.168.0.105 123456 spawn ssh root@192.168.0.105 root@192.168.0.105‘s password: Last login: Tue Jul 7 22:37:38 2020 from 192.168.0.104 [root@localhost ~]# echo 123456 |passwd --stdin root Changing password for user root. passwd: all authentication tokens updated successfully. [root@localhost ~]# exit logout Connection to 192.168.0.105 closed.