收到数据后如何关闭 netcat 连接?
How do you close netcat connection after receipt of data?
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
这将创建一个文件,将其编码为 base64,通过 netcat
发送,然后对其进行解码,最后使用校验和比较来比较发送的内容和接收的内容是否相同。
在我之前使用的 Kali Linux 机器上,netcat
连接在执行第二个脚本时关闭,但在家里尝试 Ubuntu 时,情况并非如此.我需要手动关闭与 Ctrl+D
.
的连接
如何才能在收到此数据后关闭连接?
我认为包括 nc
的 -c
标志应该可以。另外,您确定要使用 Bourne shell 而不是 Bash 吗?我建议将您的 shebang 更改为 #!/bin/bash
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
这将创建一个文件,将其编码为 base64,通过 netcat
发送,然后对其进行解码,最后使用校验和比较来比较发送的内容和接收的内容是否相同。
在我之前使用的 Kali Linux 机器上,netcat
连接在执行第二个脚本时关闭,但在家里尝试 Ubuntu 时,情况并非如此.我需要手动关闭与 Ctrl+D
.
如何才能在收到此数据后关闭连接?
我认为包括 nc
的 -c
标志应该可以。另外,您确定要使用 Bourne shell 而不是 Bash 吗?我建议将您的 shebang 更改为 #!/bin/bash