没有手动干预需要获得完整的telnet输出

时间:2018-04-22 05:32:52

标签: linux shell tcp telnet

我需要在没有任何人工干预的情况下获得准确的telnet输出

-bash-4.2# echo exit | telnet localhost 22
Trying ::1…
Connected to localhost.
Escape character is ‘^]’.
Connection closed by foreign host.

-bash-4.2# telnet localhost 22
Trying ::1…
Connected to localhost.
Escape character is ‘^]’.
SSH-2.0-OpenSSH_7.4

这里使用telnet localhost 22" SSH-2 OpenSSH_7.4"正在显示,但在echo exit | telnet localhost 22中它没有显示

输出:

bash-4.2# echo exit | telnet localhost 22
Trying ::1…
Connected to localhost.
Escape character is ‘^]’.
Connection closed by foreign host.
telnet> quit
Connection closed.
-bash-4.2#

我也尝试过以下评论 "连接已关闭。"

-bash-4.2# cat < /dev/tcp/localhost/22
SSH-2.0-OpenSSH_7.4
^C
-bash-4.2# echo exit | cat < /dev/tcp/localhost/22
SSH-2.0-OpenSSH_7.4  (I want to execute and close automatically)

2 个答案:

答案 0 :(得分:0)

你可以试试;

read a < /dev/tcp/localhost/22
echo "Output is $a"

$ a变量将包含文本“SSH-2.0-OpenSSH_7.4”。

答案 1 :(得分:0)

Telnet sill在一些初始步骤(连接?)后开始读取输入 在结束输入之前,您必须先给telnet一些时间。

(sleep 1 ; echo quit) | telnet localhost 22

不需要echo

sleep 1 |telnet localhost 22

在输出中避免一些垃圾

sleep 1 |netcat localhost 22