Reputation: 33
I have a device as a telnet server but drops the connection if no packet is received in 60 seconds, for some reason this behavior cannot be changed. Putty has a feature to send null packets to the server periodically to keep the session alive, which works fine for me. But some times I have to telnet to the device from linux terminal, where putty is not available. So I wonder if it's possible to periodically send null packets with the linux telnet client, or any other way to keep the session alive, or other command line telnet client that could do this.
Upvotes: 3
Views: 8125
Reputation: 1952
apt install libkeepalive0 # Only needed once
export KEEPIDLE=1 KEEPINTVL=10
LD_PRELOAD=/usr/lib/libkeepalive.so telnet gaia.local
Telnet uses TCP ("Transmission Control Protocol") to communicate with the server. TCP implementations on most UNIX systems allow programs to request that a connection be kept alive. While BSD has a nice way of handling this if a program does not enable keepalive on its own, it appears Linux requires a little bit of a kludge: wrap the program in libkeepalive
using LD_PRELOAD so that the TCP socket calls are altered before going to the kernel.
You can compile libkeepalive from https://libkeepalive.sourceforge.net/. You do not need to be root to use it. However, it is easier on most GNU/Linux distributions to install it from the package manager. For example, on Debian and Ubuntu, one could do apt install libkeepalive0
.
There are three environment variables which control how libkeepalive works. The variables are documented in the Linux manpage for tcp(7).
Variable name | /proc/sys/net/ipv4/ | Meaning | Default | Units |
---|---|---|---|---|
KEEPIDLE |
tcp_keepalive_time | Interval between last data packet sent and first keepalive probe | 7200 | seconds |
KEEPINTVL |
tcp_keepalive_intvl | Interval between subsequent keepalive probes | 75 | seconds |
KEEPCNT |
tcp_keepalive_probes | Consider connection dead if this many probes are sent to the server without receiving a response | 9 | count |
There can be multiple causes for a server to drop an idle connection. This keepalive solution only operates at the level of TCP packets. It would not work if the telnetd on your server is looking for user keystrokes or telnet packets (SEND NOP
).
Upvotes: 1
Reputation: 12255
This is not a perfect solution, but might suffice. Wrap the telnet within an expect script that detects lack of input. It sends the escape sequence control-] to the telnet client to get the command prompt telnet>
, and issues the command send nop
(no-operation). I assume this is enough to keep the connection alive. If not there are other commands to try.
#!/usr/bin/expect
spawn telnet localhost
expect "ogin: "
send "username\r"
expect "assword:"
send "mypw\r"
expect {$ }
interact timeout 10 {
log_user 0; send "\x1d"; expect "telnet>" { send "send nop\r" };
expect "send nop\r\n"; log_user 1 }
Obviously, you need to edit this with the desired hostname, username, password, and expected command prompt. See man expect
for the syntax.
Upvotes: 1
Reputation: 4770
With the command line telnet client that's not possible. You can enable frequent TCP keepalives globally: TCP Keepalive HOWTO. I don't know if this has any side-effects, but it seems like a viable workaround.
By the way, putty is available on Linux. You just need to have an X server on your machine to run it on a remote host (I assume you're ssh-ing into the Linux box where you do your telnet work). If you're on Windows, the Linux Subsystem for Windows together with an X server might do the trick. Cygwin also works for this.
Upvotes: 1