问题
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).
回答1:
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
回答2:
You may need other linux package like expect
to achieve what you want.
回答3:
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] $
回答4:
╭─ ~
╰» 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.
来源:https://stackoverflow.com/questions/41476089/auto-exit-telnet-command-back-to-prompt-without-human-intervention-quit-close