☆注意要执行的操作放在注释那行的下面 用send包起来☆
本例中是进入指定目录,开始一个程序。
#!/usr/bin/expect ##author:stevenrao##打开 ssh 终端,省去密码交互脚本 if { $argc==0 } {send "usage: $argv0 ip \[port\]\n";exit}#要打开的远程机器ip 端口set ip [lindex $argv 0]set port 22if { $argc==2 } {set port [lindex $argv 1]} #日志文件,一ip为文件名log_file $iplog_user 1 #用户名密码set user rootset password cwvbu3knna #启动ssh登录进程spawn /usr/bin/ssh -p $port $user@${ip}set myid $spawn_idset timeout 10 while { [string length $myid] } { #提示符set prompt "(%|#|\\$|>|\\])"catch {set prompt $env(EXPECT_PROMPT)} expect { "*tcsetattr failed*" { send_log "\n${ip},sshsyscallerror\n"; break } "*Authentication failed*" { break } "*yes/no*" { send "yes\n" } "*password: " { send "$password\n" } timeout { send_log "\n${ip},logintimeout\n" exit } -re $prompt { set timeout 1 send_log "\n${ip},loginsuccess\n" ##登录成功,这里放你想要执行的交互脚本 send "cd /usr/local/court/jusspider-1.0-RELEASE/bin\n" send "sh start.sh\n" send "exit\n" interact;exit expect eof { send_log "eof\n" exit } } }}
就这样,祝大家套用成功^_^