为大家提供一例expect脚本,expect脚本语言是tcl语言的扩展,可用来解决一些工具无法自动交互的问题。
例子,使用expect管理多台服务器。
复制代码代码示例:
#!/usr/bin/expect
#purpose:auto run command on multiple servers #how to: mms <user> <cmd> #site: www.it.net.cn # if {$argc < 2} { puts "usage: mms <user> <cmd>" exit 1 } #set servers set SERVERS {"192.168.0.100" "192.168.0.101" "192.168.0.102"} #set password set PASSWORDS(user1) "passwd1" set PASSWORDS(user2) "passwd2" #get virables set USER [lindex $argv 0] set CMD [lrange $argv 1 end] set passwd $PASSWORDS($USER) foreach x $SERVERS { eval spawn ssh -l $USER $x $CMD expect { "password" { send "$passwd\r" } "yes/no" { send "yes\r";exp_continue; } } expect eof }
代码说明: |