如何在响应中返回特定字符后关闭 netcat 连接?

发布于 2024-08-01 22:28:00 字数 426 浏览 3 评论 0原文

我们有一个非常简单的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

蓝眼睛不忧郁 2024-08-08 22:28:00

这个怎么样?

客户端:

awk -v RS=

测试:

# server side test script
while true; do ascii -hd; done | { netcat -l 12345; echo closed...;}
# Generate 'some' data for testing & pipe to netcat.
# After netcat connection closes, echo will print 'closed...'

# Client side:
awk -v RS=J 'NR==1; {exit;}' < /dev/tcp/localhost/12345
# Changed end character to 'J' for testing.
# Didn't wish to write a server side script to generate 0x1C.

客户端产生:

    0 NUL    16 DLE    32      48 0    64 @    80 P    96 `   112 p
    1 SOH    17 DC1    33 !    49 1    65 A    81 Q    97 a   113 q
    2 STX    18 DC2    34 "    50 2    66 B    82 R    98 b   114 r
    3 ETX    19 DC3    35 #    51 3    67 C    83 S    99 c   115 s
    4 EOT    20 DC4    36 $    52 4    68 D    84 T   100 d   116 t
    5 ENQ    21 NAK    37 %    53 5    69 E    85 U   101 e   117 u
    6 ACK    22 SYN    38 &    54 6    70 F    86 V   102 f   118 v
    7 BEL    23 ETB    39 '    55 7    71 G    87 W   103 g   119 w
    8 BS     24 CAN    40 (    56 8    72 H    88 X   104 h   120 x
    9 HT     25 EM     41 )    57 9    73 I    89 Y   105 i   121 y
   10 LF     26 SUB    42 *    58 :    74

出现“J”后,服务器端关闭& 打印“已关闭...”,确保连接确实已关闭。

\x1c' 'NR==1;{exit 0;}' < /dev/tcp/host-ip/port

测试:

客户端产生:

出现“J”后,服务器端关闭& 打印“已关闭...”,确保连接确实已关闭。

How about this?

Client side:

awk -v RS=

Testing:

# server side test script
while true; do ascii -hd; done | { netcat -l 12345; echo closed...;}
# Generate 'some' data for testing & pipe to netcat.
# After netcat connection closes, echo will print 'closed...'

# Client side:
awk -v RS=J 'NR==1; {exit;}' < /dev/tcp/localhost/12345
# Changed end character to 'J' for testing.
# Didn't wish to write a server side script to generate 0x1C.

Client side produces:

    0 NUL    16 DLE    32      48 0    64 @    80 P    96 `   112 p
    1 SOH    17 DC1    33 !    49 1    65 A    81 Q    97 a   113 q
    2 STX    18 DC2    34 "    50 2    66 B    82 R    98 b   114 r
    3 ETX    19 DC3    35 #    51 3    67 C    83 S    99 c   115 s
    4 EOT    20 DC4    36 $    52 4    68 D    84 T   100 d   116 t
    5 ENQ    21 NAK    37 %    53 5    69 E    85 U   101 e   117 u
    6 ACK    22 SYN    38 &    54 6    70 F    86 V   102 f   118 v
    7 BEL    23 ETB    39 '    55 7    71 G    87 W   103 g   119 w
    8 BS     24 CAN    40 (    56 8    72 H    88 X   104 h   120 x
    9 HT     25 EM     41 )    57 9    73 I    89 Y   105 i   121 y
   10 LF     26 SUB    42 *    58 :    74

After 'J' appears, server side closes & prints 'closed...', ensuring that the connection has indeed closed.

\x1c' 'NR==1;{exit 0;}' < /dev/tcp/host-ip/port

Testing:

Client side produces:

After 'J' appears, server side closes & prints 'closed...', ensuring that the connection has indeed closed.

萌酱 2024-08-08 22:28:00

创建一个名为 client.sh 的 bash 脚本:

#!/bin/bash

cat someFile

while read FOO; do
        echo $FOO >&3
        if [[ $FOO =~ `printf ".*\x00\x1c.*"` ]]; then
                break
        fi
done

然后从主脚本中调用 netcat,如下所示:(

3>&1 nc -c ./client.sh somehost 1234

您需要 bash 版本 3 来进行正则表达式匹配)。

这假设服务器正在按行发送数据 - 如果不是,您将不得不调整 client.sh 以便它一次读取并回显一个字符。

Create a bash script called client.sh:

#!/bin/bash

cat someFile

while read FOO; do
        echo $FOO >&3
        if [[ $FOO =~ `printf ".*\x00\x1c.*"` ]]; then
                break
        fi
done

Then invoke netcat from your main script like so:

3>&1 nc -c ./client.sh somehost 1234

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

遮云壑 2024-08-08 22:28:00

这对我来说效果最好。 只需使用 while 循环读取输出,然后使用 if 语句检查“0x1c”。

while read i; do 
    if [ "$i" = "0x1c" ] ; then # Read until "0x1c". Then exit
        break
    fi
    echo $i; 
done < <(cat someFile | netcat somehost 1234)

This worked best for me. Just read the output with a while loop and then check for "0x1c" using an if statement.

while read i; do 
    if [ "$i" = "0x1c" ] ; then # Read until "0x1c". Then exit
        break
    fi
    echo $i; 
done < <(cat someFile | netcat somehost 1234)
寻找我们的幸福 2024-08-08 22:28:00

也许也看看 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

谜泪 2024-08-08 22:28:00

尝试:

(cat somefile; sleep $timeout) | nc somehost 1234 | sed -e '{s/\x01.*//;T skip;q;:skip}'

这需要 GNU sed。

它是如何工作的:

{
    s/\x01.*//; # search for \x01, if we find it, kill it and the rest of the line
    T skip;     # goto label skip if the last s/// failed
    q;          # quit, printing current pattern buffer
    :skip       # label skip
}

请注意,这假设 \x01 之后会有一个换行符 - 否则 sed 将看不到它,因为 sed 是逐行运行的。

Try:

(cat somefile; sleep $timeout) | nc somehost 1234 | sed -e '{s/\x01.*//;T skip;q;:skip}'

This requires GNU sed.

How it works:

{
    s/\x01.*//; # search for \x01, if we find it, kill it and the rest of the line
    T skip;     # goto label skip if the last s/// failed
    q;          # quit, printing current pattern buffer
    :skip       # label skip
}

Note that this assumes there'll be a newline after \x01 - sed won't see it otherwise, as sed operates line-by-line.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文