如何在响应中返回特定字符后关闭 netcat 连接?
我们有一个非常简单的 TCP 消息传递脚本,它将一些文本发送到服务器端口,该端口返回并显示响应。
我们关心的脚本部分看起来像这样:
cat someFile | netcat somehost 1234
一旦我们得到返回的特定字符代码(特别是 &001C
),服务器返回的响应就是“完整”的。
当我收到这个特殊字符时如何关闭连接?
(注意:服务器不会为我关闭连接。而我目前只是CTRL+当我知道脚本已经完成时,我希望能够发送许多这样的消息,一个接一个。)
(注意:netcat -w x
不是'还不够好,因为我希望尽快推送这些消息)
We have a very simple tcp messaging script that cats some text to a server port which returns and displays a response.
The part of the script we care about looks something like this:
cat someFile | netcat somehost 1234
The response the server returns is 'complete' once we get a certain character code (specifically &001C
) returned.
How can I close the connection when I receive this special character?
(Note: The server won't close the connection for me. While I currently just CTRL+C the script when I can tell it's done, I wish to be able to send many of these messages, one after the other.)
(Note: netcat -w x
isn't good enough because I wish to push these messages through as fast as possible)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这个怎么样?
客户端:
测试:
客户端产生:
出现“J”后,服务器端关闭& 打印“已关闭...”,确保连接确实已关闭。
How about this?
Client side:
Testing:
Client side produces:
After 'J' appears, server side closes & prints 'closed...', ensuring that the connection has indeed closed.
创建一个名为 client.sh 的 bash 脚本:
然后从主脚本中调用 netcat,如下所示:(
您需要 bash 版本 3 来进行正则表达式匹配)。
这假设服务器正在按行发送数据 - 如果不是,您将不得不调整 client.sh 以便它一次读取并回显一个字符。
Create a bash script called client.sh:
Then invoke netcat from your main script like so:
(You'll need bash version 3 for the regexp matching).
This assumes that the server is sending data in lines - if not you'll have to tweak client.sh so that it reads and echoes a character at a time.
这对我来说效果最好。 只需使用 while 循环读取输出,然后使用 if 语句检查“0x1c”。
This worked best for me. Just read the output with a while loop and then check for "0x1c" using an if statement.
也许也看看 Ncat:
“Ncat 是各种 Netcat 版本(例如 Netcat 1.x、Netcat6、SOcat、Cryptcat、GNU Netcat 等)的许多关键功能的巅峰。Ncat 具有许多新功能,例如“连接代理”、TCP/UDP 重定向、SOCKS4 客户端和服务器支持、“链接”Ncat 进程的能力、HTTP CONNECT 代理(和代理链接)、SSL 连接/侦听支持、IP 地址/连接过滤等等。
http://nmap-ncat.sourceforge.net
Maybe have a look at Ncat as well:
"Ncat is the culmination of many key features from various Netcat incarnations such as Netcat 1.x, Netcat6, SOcat, Cryptcat, GNU Netcat, etc. Ncat has a host of new features such as "Connection Brokering", TCP/UDP Redirection, SOCKS4 client and server supprt, ability to "Chain" Ncat processes, HTTP CONNECT proxying (and proxy chaining), SSL connect/listen support, IP address/connection filtering, plus much more."
http://nmap-ncat.sourceforge.net
尝试:
这需要 GNU sed。
它是如何工作的:
请注意,这假设 \x01 之后会有一个换行符 - 否则 sed 将看不到它,因为 sed 是逐行运行的。
Try:
This requires GNU sed.
How it works:
Note that this assumes there'll be a newline after \x01 - sed won't see it otherwise, as sed operates line-by-line.