Expect 批量修改1000台机器密码
要修改的机器列表:host_lists
文件格式如下:
host_lists 文件格式( 机器名 旧密码 新密码 )
linux001 11111111 BOQOHVB5
linux002 22222222 BOQOHVB5
linux003 33333333 BOQOHVB5
linux004 12323233 BOQOHVB5
代码如下:
- #!/usr/bin/expect -f
- set file [ open "host_lists" r ]
- set log [open sshlog a ]
- proc do_command {new_pwd} {
- #expect "*#"
- #send "df -hT\r"
- expect "*#"
- send "echo $new_pwd|passwd --stdin root\r"
- expect "*#"
- send "exit\n"
- }
- while { [gets $file line] != -1 } {
- set host [lindex $line 0]
- set new_pwd [lindex $line 2]
- set old_pwd [lindex $line 1]
- # spawn ssh root@$host.sandai.net
- spawn ssh root@$host
- expect {
- "*(yes/no)*" {send "yes\r";exp_continue}
- "*password:*" {send "$old_pwd\r"}
- "*Connection refused" { puts $log "$host is can not connect";continue;}
- "closed by remote host" { puts $log "$host is can not connect";continue;}
- }
- do_command $new_pwd
- interact
- }
- host_lists 文件格式( 机器名 旧密码 新密码 )
- linux001 11111111 BOQOHVB5
- linux002 22222222 BOQOHVB5
- linux003 33333333 BOQOHVB5
- linux004 12323233 BOQOHVB5
转载于:https://blog.51cto.com/netspace/1123081