[root@runstone ssh_lianxi]# cat expect.sh
#!/usr/bin/env expect
if { $argc < 3 } { #判断命令行参数的个数
send_user "usage: expect expect.ssh ip user passwd
" #send_user 表示输出到标准输出
exit #退出程序
}
set timeout 5
#下面[]的意思是执行中括号里面的命令,然后把值赋值给其他变量,中括号里面的意思表示获取argv变量的第一个参数的值赋值给server。
set server [lindex $argv 0]
set user [lindex $argv 1]
set passwd [lindex $argv 2]
spawn ssh -l $user $server
expect {
#-timeout 3 #在expect里面可以这样设置超时时间
"(yes/no)?" { send "yes
" }
"password:" { send "$passwd
" }
"#" { send "vmstat
" }
timeout { puts "expect was timeout by fblinux.";return } #当超时发生的时候,就激活这个timeout动作,执行后面的表达式
}
#expect "#" { send "vmstat
" }
expect eof
exit
借鉴:https://www.cnblogs.com/liujiacai/p/9046849.html