I connect to a Cisco router using telnet.

The connection times out every 3 minutes.

How do stop my telnet session from getting disconnected from the router due to a timeout.

I understand that putty and SecureCRT can send a null or escape character periodically to stop the session (telnet or SSH) from timing out.

How do I do this on Linux without a 3rd party program ? I use the following script as a startup script when starting my terminal:

#!/usr/bin/expect -f

spawn telnet <Router IP Address>
expect -re "ogin: "
send "*******\n"
expect -re "assword:"
send "***********\n"
sleep 2
expect "Router>"
  • 2
    Do you have an ssh session or a telnet session? Your title, tags, and body are confused. – Jeff Schaller Feb 17 '18 at 0:32
  • 1
    I'm using Telnet. The only place I mentioned SSH in my post was when referencing the feature for sending periodic characters to the session in SecureCRT and Putty. Thanks. – Khaled Abuelenain Feb 17 '18 at 1:45

The interact statement of expect can take pairs of patterns and actions somewhat like the expect statement. In particular, you can add a timeout pattern and an action of sending. For example,

interact timeout 10 { send "date\r" }

would send what you type as usual, but if you do not type for 10 seconds, it will then send the string date and carriage-return. If you are using telnet, in char mode, you might be able to keep the connection alive by simply sending a space followed by a backspace, which would not disrupt any partial line you had already typed:

interact timeout 150 { send " \b" }
  • Exactly what I was looking for. Worked like a charm. Thank you very much ! – Khaled Abuelenain Feb 23 '18 at 6:40
  • cool thing. starting day here, will revisit later on my answer – Rui F Ribeiro Feb 23 '18 at 10:03

You have got the default timeout on your telnet sessions in the Cisco side, and not on the Linux side as a security measure.

I advise not confusing those timeouts with TCP keepalives. They are layer 7 timeouts for console/human operator inactivity.

I would say you should setup ssh sessions and not telnet to Cisco routers due to security concerns.

One of the configurations you can do on cisco side if you go for ssh is:

ip ssh timeout 300

As for telnet, you can change the default timeout as:

r1# configure terminal
r1(config)# line vty 
r1(config-line)# exec-timeout 300

You can also use exec-timeout 0 or ip ssh timeout 0 for not having timeouts in telnet or ssh, however it is not considered a good security pratice. .

I would also advise changing other default configurations of the router, namely the default hostname.

  • 1
    I understand the security issues with telnet, but I'm not in a position to change the access method to SSH as the routers are owned by a client. Ideally, I am looking for a way to send periodic TCP keepalives from my terminal while I'm inside a telnet session by adding something to the script in my original post. – Khaled Abuelenain Feb 17 '18 at 1:43
  • Or some way to emulate pressing any character on my keyboard, while I'm inside a telnet session by adding something to the script in my original post - to stop the session from timing out. Thanks. – Khaled Abuelenain Feb 17 '18 at 1:49
  • @KhaledAbuelenain, are you saying the exec-timeout setting in this answer wasn't sufficient? – BowlOfRed Feb 17 '18 at 6:48
  • @KhaledAbuelenain Either leaving a telnet open or changing exec-timeout is the same thing. The solution is not the script, as soon you hit interact, you cannot do much more with expect. What you need for now is bigger exec-timeouts – Rui F Ribeiro Feb 17 '18 at 8:07
  • In an ideal world, what you would need would be bigger exec-timeouts, and if you dream on inject commands, it would be to be a modified telnet program to be aware how for long have you not done commands. It would be an ugly hack. The timeout is done at the Cisco side, and it is too low to not be change at Cisco side. – Rui F Ribeiro Feb 17 '18 at 8:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.