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