15

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).

AKS
  • 16,482
  • 43
  • 166
  • 258

6 Answers6

32

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
Maxim Akhmedov
  • 679
  • 5
  • 10
5

Here is the clean way to do it

echo -n | telnet `hostname` 2878

For more info on this behavior see this link

k_vishwanath
  • 1,326
  • 2
  • 20
  • 28
1
╭─ ~
╰» 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.

Matthew
  • 6,351
  • 8
  • 40
  • 53
0

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

Chiu
  • 374
  • 2
  • 10
  • Yes, expect is prob. the way to go. "Expect is a tool for automating interactive applications according to a script. Following the script, Expect knows what can be expected from a program and what the correct response should be." –  Jan 05 '17 at 02:11
0

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] $ 
AKS
  • 16,482
  • 43
  • 166
  • 258
  • Nah, I don't like this solution that much, but it's working as I used 2-3 operations at least. – AKS Jan 06 '17 at 00:46
0

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