Auto exit Telnet command back to prompt without human intervention ^] quit close exit code 1

21.4k Views Asked by At

I'm running telnet command on a host for a given port (which is open), it returns 0 (success).

For trying telnet manually, I type the following command, then I press control+bracket i.e. ^], then press Enter key, then I get to telnet> prompt, where if I type close or quit, it comes back to the $ prompt and seeing exit status of last command shows 0 for (success) as port 2878 is open (as per the telnet command's output).

[vagrant@myvagrant /tmp] $ telnet `hostname` 2878
Trying 127.0.0.1...
Connected to myvagrant.
Escape character is '^]'.
^]

telnet> close
Connection closed.
[vagrant@myvagrant /tmp] $ echo $?
0

Now, I wanted to run the same operation without any human intervention i.e. I don't want to manually give ^] and press Enter key to come to the telnet> prompt, then enter close (or quit) telnet command to finally come back to the $ prompt.

For this, I tried using echo -e command's option and giving ^], \n (for new line character i.e. Enter key) and close command (for telnet> prompt so that I come back to $ prompt). Doing this, kind of worked as expected but for the exit status of last command echo $?, I'm getting 1 (instead of 0). Why?

[vagrant@myvagrant /tmp] $ echo -e "^]\nclose" | telnet `hostname` 2878
Trying 127.0.0.1...
Connected to myvagrant.
Escape character is '^]'.
Connection closed by foreign host.
[vagrant@myvagrant /tmp] $ 
[vagrant@myvagrant /tmp] $ echo $?
1
[vagrant@myvagrant /tmp] $ 

or tried the here-doc method as well, but not sure why it's returning 1 (as exit code) for a valid port which is open.

[vagrant@myvagrant /tmp] $ telnet `hostname` 2878 <<GIGA
> echo ^]
> echo close
> GIGA
Trying 127.0.0.1...
Connected to myvagrant.
Escape character is '^]'.
Connection closed by foreign host.
[vagrant@myvagrant ~/aks/always-latest-ws-sunny] $ echo $?
1
[vagrant@myvagrant ~/aks/always-latest-ws-sunny] $

How can I automatically exit from telnet if the port is open and get 0 as exit code? If there's a way to capture the output of the previous command, may be I can grep the 'Connection closed by foreign host.' string to mark it successful (0).

6

There are 6 best solutions below

3
On BEST ANSWER

A slight improvement to the answer provided by Matthew above which allows you to use it inside shell scripts:

$ echo -e '\x1dclose\x0d' | telnet google.com 80
Connected to google.com.
Escape character is '^]'.

telnet> close
Connection closed.
$ echo $?
0
1
On

Without using Expect, I guess using the HERE document (<

[vagrant@myvagrant /tmp] $ telnet_result=$(telnet `hostname` 2878 <<GIGA
echo ^]
echo close
GIGA
); echo $telnet_result | grep "Escape character is '^]'"; echo $?
Connection closed by foreign host.
 Escape character is '^]'.
0
[vagrant@myvagrant /tmp] $ 

and for a bad/invalid/non-open port, the same gives 1 (as expected).

[vagrant@myvagrant /tmp] $ telnet_result=$(telnet `hostname` 287811111 <<GIGA
echo ^]
echo close
GIGA
); echo $telnet_result | grep "Escape character is '^]'"; echo $?
telnet: connect to address 127.0.0.1: Connection refused
1
[vagrant@myvagrant /tmp] $ 
1
On

You may need other linux package like expect to achieve what you want.

0
On

The approach that I used for this :

for i in list do for j in list1 do sleep 2|timeout --signal=9 3 telnet $i $j done done

0
On
╭─ ~
╰» echo "^]close^M" | telnet localhost 22; echo $?
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.

telnet> close
Connection closed.
0

To enter the ^] and ^M characters, press ctrl+v ctrt+] and ctrl+v ctrl+m respectively.

0
On

Here is the clean way to do it

echo -n | telnet `hostname` 2878

or,

telnet `hostname` 2878 </dev/null

For more info on this behavior see this link