简体   繁体   English

收到数据后如何关闭netcat连接?

[英]How do you close netcat connection after receipt of data?

A very simple use of netcat is below: netcat一个非常简单的使用如下:

#!/bin/sh

echo "hello world" > original.txt
base64 original.txt > encoded.txt
cat encoded.txt | nc -l -p 1234 -q 0
#!/bin/sh

nc localhost 1234 -q 0 > received.txt
base64 -d received.txt > decoded.txt
rm received.txt encoded.txt
echo "Sent:"
md5sum original.txt
echo "Received:"
md5sum decoded.txt
rm decoded.txt original.txt

This creates a file, encodes it into base64, sends it over netcat , and then decodes it, finally comparing whether what was sent and what was received is identical using a checksum comparison.这将创建一个文件,将其编码为 base64,通过netcat发送,然后对其进行解码,最后使用校验和比较来比较发送的内容和接收的内容是否相同。

On a Kali Linux machine I was using earlier, the netcat connection closes upon execution of the second script, but trying on Ubuntu at home, this is not the case.在我之前使用的 Kali Linux 机器上, netcat连接在执行第二个脚本时关闭,但在家里尝试 Ubuntu,情况并非如此。 I need to manually close the connection with Ctrl+D .我需要使用Ctrl+D手动关闭连接。

How can I make it such that the connection closes after receiving this data?如何在收到此数据后关闭连接?

I think including the -c flag for nc should do it.我认为包括nc-c标志应该这样做。 Also, are you sure you want to use Bourne shell, and not Bash?另外,您确定要使用 Bourne shell 而不是 Bash 吗? I'd suggest changing your shebang to #!/bin/bash我建议将您的shebang更改为#!/bin/bash

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM