[英]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.