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

7

17

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

Salto answered 5/1, 2017 at 1:45 Comment(0)
P
33

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
Plasmo answered 25/1, 2019 at 12:1 Comment(4)
Matthew's answer doesn't work for me. This one works. I am still surprised why.Unravel
Work for me beautifully!Roxi
@Maxim Akhmedov Nice!!Salto
I think echo -e '\x1d' | telnet google.com 80 on its own is enough, at least it works for me.Glycoside
G
7

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

Galliot answered 28/3, 2020 at 5:29 Comment(0)
H
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.

Hulsey answered 11/8, 2018 at 6:7 Comment(0)
H
0

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

Hardi answered 5/1, 2017 at 2:5 Comment(1)
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."Compressed
S
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] $ 
Salto answered 6/1, 2017 at 0:35 Comment(1)
Nah, I don't like this solution that much, but it's working as I used 2-3 operations at least.Salto
E
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

Embrace answered 11/4, 2022 at 12:59 Comment(0)
J
0

You can use the following script to exit a telnet session when it's done:

telnet [ip-address] || true

The output will be like:

Trying [ip-address]...
Connected to [ip-address].
Escape character is '^]'.
Connection closed by foreign host.

Or

Trying [ip-address]...
telnet: Unable to connect to remote host: Connection refused

Notes:

  • In both cases, a zero status code will be returned.
Jacal answered 25/7 at 11:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.