TCL/Expect:: How to automate the router boot loade

2019-06-10 05:53发布

This question already has an answer here:

This Question is the continuation of My old question "TCL/Expect:: How to automate the router booting scenario?". Since I am not getting any response for that I am creating a new question.

My requirement : I want to automate router boot prompt scenario. That involves following steps:

  1. Login into a router
  2. Give reload
  3. Press Esp kep from keyboard continuously (You will get boot prompt by this)

Is there any way to automate this inbetween process (reload... to ... boot prompt)

I tried to use "plink.exe" as suggested by donal in my previous question. But when router reloads plink is also coming out.

Please suggest me any tool to automate this.

标签: tcl expect
1条回答
何必那么认真
2楼-- · 2019-06-10 06:39

You can use send and expect to do this with code given below.

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"

After giving reload command, to get to the router's boot prompt, you are pressing the Esc Key and I believe you will prefer to wait for some random text which will appear on the reboot logs of router.

That text has to be added in the code segment

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

Make sure, you are replacing the "your ideal string here" with your preferable text.

You can refer the following links for the ASCII characters of printable and non-printable characters.

ASCII Table

ASCII Non-Printable

查看更多
登录 后发表回答