Expect 批量修改1000台机器密码

要修改的机器列表:host_lists  

文件格式如下:

host_lists  文件格式(  机器名  旧密码 新密码 )

linux001 11111111 BOQOHVB5

linux002 22222222 BOQOHVB5

linux003 33333333 BOQOHVB5

linux004 12323233 BOQOHVB5

代码如下:
  1. #!/usr/bin/expect -f 
  2. set file [ open "host_lists" r ] 
  3. set log [open sshlog a ] 
  4. proc do_command {new_pwd} {     
  5. #expect "*#"         
  6. #send "df -hT\r" 
  7. expect "*#" 
  8. send "echo $new_pwd|passwd --stdin root\r" 
  9. expect "*#" 
  10. send "exit\n" 
  11.  
  12. while { [gets $file line] != -1 } { 
  13.     set host [lindex $line 0] 
  14.     set new_pwd [lindex $line 2] 
  15.     set old_pwd [lindex $line 1] 
  16. #    spawn ssh root@$host.sandai.net 
  17.      spawn ssh root@$host 
  18.     expect { 
  19.     "*(yes/no)*"  {send "yes\r";exp_continue} 
  20.     "*password:*" {send "$old_pwd\r"} 
  21.     "*Connection refused" { puts $log "$host is can not connect";continue;} 
  22.     "closed by remote host" { puts $log "$host is can not connect";continue;} 
  23.   } 
  24.  do_command $new_pwd 
  25.  interact 
  26.  
  27. host_lists  文件格式(  机器名  旧密码 新密码 ) 
  28. linux001 11111111 BOQOHVB5 
  29. linux002 22222222 BOQOHVB5 
  30. linux003 33333333 BOQOHVB5 
  31. linux004 12323233 BOQOHVB5