在 bash 中监听返回的 UDP 数据包
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24247811/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
listen for returning UDP packets in bash
提问by Martin
I played around with bash(4.0.33) network support for learning purposes and tried to create a port scanner in bash. For TCP I opened a TCP/IP socket with
为了学习目的,我尝试了 bash(4.0.33) 网络支持,并尝试在 bash 中创建端口扫描器。对于 TCP,我打开了一个 TCP/IP 套接字
exec 3<>/dev/tcp/192.0.2.1/80
exec 3<>/dev/tcp/192.0.2.1/80
and appropriate action was taken if the connection was refused or connectsystem call timed out. However, with UDP I'm easily able to send a packet with
如果连接被拒绝或连接系统调用超时,则采取适当的措施。但是,使用 UDP,我可以轻松发送数据包
echo > /dev/udp/192.0.2.1/53
echo > /dev/udp/192.0.2.1/53
but how to read returning packets from correct socket? I mean UDP datagram sent to 192.0.2.1 has source port from ephemeral port range and thus I do not know which socket in /dev/udp/192.0.2.1/
directory should I read. Or isn't this doable without external utilities like tcpdump?
但是如何从正确的套接字读取返回的数据包?我的意思是发送到 192.0.2.1 的 UDP 数据报具有来自临时端口范围的源端口,因此我不知道/dev/udp/192.0.2.1/
应该读取目录中的哪个套接字。或者,如果没有像tcpdump这样的外部实用程序,这是否可行?
回答by that other guy
Bash's UDP support isn't great, and is compiled out on many distros (Debian/Ubuntu and derivatives especially). The recommended tool is netcat:
Bash 的 UDP 支持不是很好,并且在许多发行版(尤其是 Debian/Ubuntu 和衍生版)上编译出来。推荐的工具是netcat:
nc -u 192.0.2.1 53
Note: Use coproc or named pipes to read and write from the same netcat process. Don't send a packet first and try to catch the reply netcat.
注意:使用 coproc 或命名管道从同一个 netcat 进程读取和写入。不要先发送数据包并尝试捕获回复 netcat。
Bash is really the wrong language for this though. Consider using Python, which handles both UDP and binary data way better, with just a couple of more lines of code.
尽管如此,Bash 确实是错误的语言。考虑使用 Python,它可以更好地处理 UDP 和二进制数据,只需多几行代码。