任何帮助将不胜感激。我需要将继承的expect脚本从Telnet转换为仅SSH登录。首先,我是一个路由器人,继承了我们所有的期望脚本模板,写了一段时间。到目前为止,经过很少的修改,他们已经能够顺利运行。我们的客户希望远离Telnet,因此几个月前我们准备好所有思科路由器和交换机,以支持Telnet和SSH。到现在为止,我们的脚本一直很好。但是,Telnet支持和服务器很快就会消失,我需要弄清楚如何重新配置所有脚本模板才能在仅限SSH的环境中工作。
所以,这是一个简单的模板来获得sh ver输出的例子:
#!/usr/local/bin/expect -f
#
set force_conservative 0 ;# set to 1 to force conservative mode even if
;# script wasn't run conservatively originally
if {$force_conservative} {
set send_slow {1 .1}
proc send {ignore arg} {
sleep .1
exp_send -s -- $arg
}
}
####################################################################
# Info for command line arguments
set argv [ concat "script" $argv ]
set router [ lindex $argv 1 ]
####################################################################
set timeout 15
set send_slow {1 .05}
spawn telnet $router
match_max 100000
expect Username:
sleep .1
send -s -- "user\r"
expect Password:
sleep .1
send -s -- "pass\r"
expect *
send -s -- "\r"
expect *
sleep .2
send -s -- "sh ver\r"
expect *
sleep .2
send -s -- "end\r"
expect *
sleep .2
send -s -- "wr\r"
expect *
sleep .2
send -s -- "exit\r"
expect *
sleep .2
expect eof
答案 0 :(得分:0)
而不是:
spawn telnet $router
match_max 100000
expect Username:
sleep .1
send -s -- "user\r"
expect Password:
sleep .1
send -s -- "pass\r"
尝试使用:
spawn ssh -M username@$router
while 1 {
expect {
"no)?" {send "yes\r"}
"sername:" {send "username\r"}
"assword:" {send "password\r"}
">" {break}
"denied" {send_user "Can't login\r"; exit 1}
"refused" {send_user "Connection refused\r"; exit 2}
"failed" {send_user "Host exists. Check ssh_hosts file\r"; exit 3}
timeout {send_user "Timeout problem\r"; exit 4}
}
}