Escape from Telnet
Posted by raetsel on August 12, 2007
Not the title of a 50s sci-fi novel but something I find I need to do at work now and again. Telnet is something of a deprecated method of connectivity because of its inherent lack of security. ( Anything transmitted over a telnet session is transmitted in the clear ). The preferred method of remote connectivity is usually ssh that will encrypt the traffic. However many devices like jet direct print server boxes only support telnet and indeed many of the Unix servers at work don’t have ssh installed. (Sshhh don’t tell the crackers).
So now and again I find I am telnetted (new verb) on to a system and then telnet to a device only for it to hang and I need to escape from the telnet session. Now as standard Ctrl + ] (^]) will get you to the telnet “command prompt” and from here you can type quit to end the hung session. However there is a snag when you have telnetted to one server and then to another server or device. The Ctrl + ] is actually interpreted by the first telnet connection and you end up escaping that one and severing your initial connection not the secondary one.
The reason for this is fairly obvious telnet inspects your keystrokes and passes them through to the remote connection, if it sees the escape sequence then it responds. Now when you telnet from a telnet session that first telnet inspects all your keystrokes before passing them on to the remote connection which is itself running telnet. (See diagram below).
So how do I get the second telnet session to see the Ctrl+] ? Well the trick is to press Ctrl+] to get to the command prompt of the first session and change the escape sequence of that session using the command “set escape” you need to choose a control sequence you won’t type for any other reason say Ctrl+[ or Ctrl+B then hit "enter" to return back to your connection.
At this point you can type Ctrl+] and the first telnet session will see this as a normal character sequence and pass it on to the remote connection (our second telnet session). The second telnet session will then to the escape sequence and give you the command prompt where you can type quit.
So now you are back are the first connection and you can do whatever work you want to do. You just have to remember if you need to escape from this session for some reason use your newly set escape sequence Ctrl+B or whatever.