Closing telnet connection gracefully from session mode itself without going to telnet prompt

16

4

a normal telnet connection is like this:

telnet localhost 22
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
SSH-2.0-OpenSSH_4.2
^]
telnet> close
Connection closed.

I want to close it from telnet session itself without coming to telnet prompt by pressing. My requirement is that if i press some control character from telnet session itself like CTRL+A so it will come out of session and close it automatically. something like this:

$ telnet localhost 22
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
SSH-2.0-OpenSSH_4.2
^A
Connection closed.
$

I tried all the options given at the man page and tried to do some $HOME/.telnetrc file tests but couldn't achieve it, as telnetrc will execute all the commands written in it with the given host whenever a telnet to that host is done.

Can anyone help me in this, like how it can be achieved.

Kumar Alok

Posted 2010-06-28T08:37:56.153

Reputation: 1 153

it's so complicated..why you not use "exit"???its...more easy.. – None – 2010-06-28T06:58:43.190

for using exit also i need to come to telnet prompt first. right. while my requirement is to close the connection pr exit it from session mode without coming to telnet prompt(i.e. for coming to telnet prompt i need to first send escape character and then i can do close or exit). – Kumar Alok – 2010-06-28T08:04:24.983

@ Paul : I don't get you. can you please tell me where can i find the solution. – Kumar Alok – 2010-06-28T08:33:04.743

3Can you explain why one would want this? Telnet is just a client program to connect to some arbitrary service on some machine. If you use port 23 you connect to the telnet service and can terminate your login with exit, but this is a shell command, not a telnet command. You have to go to the telnet prompt to terminate a connection if the service you connected to doesn't offer a command to terminate the connection.

If you want to login to a remote machine via ssh use the ssh command, not telnet (unless you want to do manual encryption). – Benjamin Bannier – 2010-06-29T02:44:38.590

Answers

33

No, the telnet client (I'm guessing you are asking about the Linux one) only supports one escape character, Ctrl] (^]).

If you are just using telnet to make arbitrary TCP connections, consider using netcat or socat instead; these can be interrupted by simply pressing CtrlC.

user1686

Posted 2010-06-28T08:37:56.153

Reputation: 283 655

9For followers, you hold down ctrl, then hit the "right bracket" key, then hit enter. It will respond with a prompt like telnet> and you can type in quit and hit enter to exit your session. Phew! – rogerdpack – 2015-06-02T23:38:44.090

@rogerdpack Thanks. That's way to exit on OSX. – chhantyal – 2015-12-28T11:21:36.263

What if I'm using a locale where they keyboard layout has the right bracket under alt-<some other key>? ctrl-alt-<the other key> isn't working. – user628418 – 2019-04-29T09:11:19.067

Then you switch the keyboard layout, or use an on-screen keyboard... – user1686 – 2019-04-29T10:07:19.257

-4

What about the good old CTRL+D, aka EOF?

kdo

Posted 2010-06-28T08:37:56.153

Reputation: 367

1it would work only if it is handled on the server side of session. Which is not true in general (and also not true in mentioned 22 port ssh connection). – Andrey Regentov – 2014-05-13T03:16:32.920