问题
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:
- Login into a router
- Give reload
- 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.
回答1:
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
来源:https://stackoverflow.com/questions/24436915/tcl-expect-how-to-automate-the-router-boot-loader-function