TCL / Expect ::如何自动化路由器引导加载程序功能

时间:2014-06-26 17:39:14

标签: tcl expect

这个问题是我的旧问题“TCL / Expect ::如何自动化路由器启动方案?”的延续。由于我没有得到任何回应,我正在创建一个新问题。

我的要求:我想自动化路由器启动提示场景。这涉及以下步骤:

  1. 登录路由器
  2. 重新加载
  3. 连续按键盘上的Esp kep(你会得到启动提示)
  4. 有没有办法让这个inbetween进程自动化(重新加载...到...引导提示符)

    我尝试在前一个问题中使用donal建议的“plink.exe”。但是当路由器重新加载plink时也会出现。

    请建议我使用任何自动化工具。

1 个答案:

答案 0 :(得分:1)

您可以使用sendexpect使用下面的代码执行此操作。

set username "admin"
set password "lab"
set router_ip 10.189.11.27

spawn telnet $router_ip
puts "Telnet to router device"

expect "Login"
send "$username\r"
expect "Password"
send "$password\r"
#Assuming the router will come up a prompt as 'Router>' 
#and I am expecting for character '>'
expect ">"
send "enable\r"
expect "#"
#Sending reload command here
send "reload\r"
#Assuming it will prompt the user for confirmation. 
#If your router won't prompt before reload, you can remove this
expect "confirm"
#Sending 'Enter' key to confirm
send "\r"
#Waiting for the portion of text to come when we have to give
#Esc key from keyboard
expect {
        #Appearance of some text after which you prefer send Esc key
        "your ideal string here" {puts "Going to send Esc key now"}
        timeout {puts "I am still waiting to press the Esc key"}
    }

#I hope by saying escape key you meant the 'Esc' key. 
#In some routers, to get boot prompt, you have to press 'Ctrl+C'

#This will send 'Escape' key from keyboard.
send "\x1b\r"
#Assuming your boot prompt is 'boot>'
expect "boot>"

#Whatever configuration you want to change here, do it

#This will send 'Ctrl+]' to close the telnet connection gracefully
send "\x1d"
expect "telnet>"
send "quit\r"
expect "Connection"

在给出重新加载命令后,为了进入路由器的启动提示,你按下了Esc键,我相信你宁愿等待一些随机文本出现在路由器的重启日志上。

该文字必须添加到代码段

expect {
            #Appearance of some text after which you prefer send Esc key
            "your ideal string here" {puts "Going to send Esc key now"}
            timeout {puts "I am still waiting to press the Esc key"}
        }

请确保您在这里更换了#34;理想的字符串"用你喜欢的文字。

您可以参考以下链接获取可打印和不可打印字符的ASCII字符。

ASCII Table

ASCII Non-Printable