1

based on this answer, I am trying to figure out a way to exit from telnet and it return 0

these wont work:

(echo -e "repl.quit()";sleep 1) |telnet localhost 4242;echo $?
(echo -e "\029";sleep 1;echo "q";sleep 1) |telnet localhost 4242;echo $?
(echo -e "\c]";sleep 1;echo "q";sleep 1) |telnet localhost 4242;echo $?
(echo -e "\e]";sleep 1;echo "q";sleep 1) |telnet localhost 4242;echo $?
(echo -e "\E]";sleep 1;echo "q";sleep 1) |telnet localhost 4242;echo $?

The problem is: I am UNABLE to differentiate from a successful exit and a failed one...

I think this question can be linked.

Community
  • 1
  • 1
Aquarius Power
  • 3,729
  • 5
  • 32
  • 67

1 Answers1

4

You need to send ^] character, that is an unprintable group separator character before your telnet client terminates your connection after executing all commands you gave it via pipe. Most versions of echo program can produce unprintable characters using -e option. Group separator is 035 in octal (you can see the entire ASCII table with man 7 ascii on *nix systems.). So, the entire command should look like this:

$ (echo "content.location.href = 'http://v4.ident.me/'"; sleep 2; echo -e '\035'; sleep 2) | telnet localhost 4242 > /dev/null
$ echo $?
$ 0
Arkadiusz Drabczyk
  • 11,227
  • 2
  • 25
  • 38
  • `\035` !!!, I only got a tip to `\029` which did not work, thx!! btw, look at [this question](http://stackoverflow.com/questions/11681683/cant-close-a-scpitelnet-session-with-echo-when-i-use-it-in-a-script) I think you may be able to answer it by just linking to your answer here, you can grab its link on the 'share' that is just below the text! – Aquarius Power Jun 21 '14 at 18:00
  • it required another delay between url and echo here to work; the `man -7 ascii` is very useful too thx! – Aquarius Power Jun 21 '14 at 19:07
  • I just found (quick looking on the result) that `man ascii` == `man -7 ascii` == `man 7 ascii`, it seems to ignore the 7 part :) – Aquarius Power Jun 21 '14 at 19:35
  • 1
    It is not ignored. It just happened that you have only one manpage titled `ascii` on your machine - in this case `7` is redundant because always manpage from the 7th section of the manual will be opened. However, I got used to `man 7 ascii` command when I want to see an ASCII table because I remember I used some systems with multiple manpages titled `ascii` installed in different sections of the manual on which `man ascii` would open manpage from an incorrect section. – Arkadiusz Drabczyk Jun 21 '14 at 19:48