0

任何帮助,将不胜感激。我需要将继承的期望脚本从 Telnet 转换为仅 SSH 登录。首先,我是一个路由器专家,继承了我们所有的预期脚本模板,这是不久前写的。到目前为止,只需稍加修改,它们就能够顺利运行。我们的客户想要摆脱 Telnet,因此几个月前我们准备好所有 Cisco 路由器和交换机以支持 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
4

1 回答 1

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}
                }
        }
于 2012-07-26T14:11:23.677 回答