自动退出Telnet命令返回提示,无需人工干预^]退出关闭退出代码1 [英] Auto exit Telnet command back to prompt without human intervention ^] quit close exit code 1

查看:1321
本文介绍了自动退出Telnet命令返回提示,无需人工干预^]退出关闭退出代码1的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在主机上为给定端口(已打开)运行telnet命令,它返回0(成功).

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

要手动尝试telnet,请键入以下命令,然后按Control +括号,即^],然后按Enter键,然后进入telnet>提示符,如果键入closequit,它返回到$提示符,并看到最后一个命令的退出状态在端口2878打开时(成功)显示0(根据telnet命令的输出).

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

现在,我想在没有任何人工干预的情况下运行相同的操作,即我不想手动给出^]并按Enter键进入telnet>提示符,然后输入close(或quit)telnet命令最终返回到$提示符.

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.

为此,我尝试使用echo -e命令的选项,并给出^]\n(用于换行符,即Enter键)和close命令(用于telnet>提示符),以便我返回到$提示).这样做可以按预期工作,但是对于最后一个命令echo $?的退出状态,我得到的是1(而不是 0 ). 为什么?

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] $ 

也尝试了here-doc方法,但不确定为什么它为有效的开放端口返回1(作为退出代码).

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] $

如果端口打开,如何自动退出telnet并获得0作为退出代码?如果有办法捕获上一条命令的输出,可能是我可以grep'被外部主机关闭的连接.'字符串以将其标记为成功(0).

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

推荐答案

对Matthew上面提供的答案进行了一点改进,使您可以在shell脚本中使用它:

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

这篇关于自动退出Telnet命令返回提示,无需人工干预^]退出关闭退出代码1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆