Linux脚本解析telnet消息并退出
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10172860/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Linux script to parse telnet message and exit
提问by user1336117
I am connecting to a telnet listener. Telnet server sends "1234" for every second. I want to read the message "1234" and close the telnet session. Here below is my code but it does not work.
我正在连接到 telnet 侦听器。Telnet 服务器每秒发送“1234”。我想阅读消息“1234”并关闭 telnet 会话。下面是我的代码,但它不起作用。
#!/bin/bash
telnet 192.168.10.24 1234
read $RESPONSE
echo "Response is"$RESPONSE
echo "quit"
How can i automatically read the telnet message?
如何自动读取 telnet 消息?
采纳答案by dAm2K
You could use internal TCP mechanism:
您可以使用内部 TCP 机制:
#!/bin/bash
exec 3<>/dev/tcp/127.0.0.1/80
# echo -en "eventualy send something to the server\n" >&3
RESPONSE="`cat <&3`"
echo "Response is: $RESPONSE"
Or you could use nc (netcat), but please don't use telnet!
或者您可以使用 nc (netcat),但请不要使用 telnet!
回答by siva
Redirect the output to a file and read from the file
将输出重定向到文件并从文件中读取
telnet [ip-address] > /tmp/tempfile.txt
回答by Seff
The simplest and easiest method is given below.
下面给出了最简单和最简单的方法。
sleep <n> | telnet <server> <port>
n- The wait time in seconds before auto exit. It could be fractional like 0.5. Note that some required output may not be returned in the specified wait time. So we may need to increase accordingly.
n- 自动退出前的等待时间(以秒为单位)。它可能是像 0.5 这样的小数。请注意,在指定的等待时间内可能无法返回某些必需的输出。所以我们可能需要相应增加。
server- The target server IP or hostname.
server- 目标服务器 IP 或主机名。
port- Target service port number.
端口- 目标服务端口号。
You can also redirect the output to file like this,
您还可以像这样将输出重定向到文件,
sleep 1 | telnet <server> <port> > output.log
回答by realpclaudio
Already answered, but here's another point of view using curl, useful for quick checks (ie service active or not). I had to struggle a bit avoiding "script" and "expect" solutions.
已经回答了,但这是使用 curl 的另一个观点,可用于快速检查(即服务是否处于活动状态)。我不得不努力避免“脚本”和“期望”解决方案。
Just a stub for possible POP3 check:
只是可能的 POP3 检查的存根:
echo "quit" | curl telnet://localhost:110 > /tmp/telnet_session.txt
if grep "POP3 ready" /tmp/telnet_session.txt; then
echo "POP3 OK"
else
echo "POP3 KO"
fi