【问题标题】:Transitioning Expect script from Telnet to SSH only仅将 Expect 脚本从 Telnet 转换为 SSH
【发布时间】:2012-07-26 22:11:23
【问题描述】:

任何帮助将不胜感激。我需要将继承的期望脚本从 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

【问题讨论】:

    标签: login ssh expect cisco


    【解决方案1】:

    代替:

    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}
                    }
            }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-10-19
      • 2013-07-07
      • 2017-07-22
      • 2016-09-09
      • 2018-11-15
      • 2014-07-02
      • 1970-01-01
      相关资源
      最近更新 更多