如果我在WireShark 捕获运行(捕获过滤器 = )期间在命令下方运行这些NetCat(根本没有错误消息):udp dst port 4000
luis@Zarzamoro:~$ echo "Hello" | nc -w1 -4u 255.255.255.255 4000
luis@Zarzamoro:~$ echo "Hello" | nc -w1 -4u 255.255.255.1 4000
luis@Zarzamoro:~$ echo "Hello" | nc -w1 -4u 192.168.11.255 4000
luis@Zarzamoro:~$ echo "Hello" | nc -w1 -4u 192.168.11.100 4000
Run Code Online (Sandbox Code Playgroud)
这是捕获的数据(注意缺少的两个 netcat命令):
正如我总结的那样,NetCat会忽略发送到广播(xxx255) 网络地址的任何数据(没有错误 :-P )。
为什么会这样?
附加数据:
这是我的完整内容ifconfig:
luis@Zarzamoro:~$ sudo ifconfig
eth0 Link encap:Ethernet HWaddr b8:27:eb:ef:bb:aa
inet addr:192.168.11.140 Bcast:192.168.11.255 Mask:255.255.255.0
inet6 addr: fe80::ba27:ebff:feef:bbaa/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:9672 errors:0 dropped:0 overruns:0 frame:0
TX packets:8567 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:503027 (503.0 KB) TX bytes:5993557 (5.9 MB)
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:65536 Metric:1
RX packets:153 errors:0 dropped:0 overruns:0 frame:0
TX packets:153 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:10926 (10.9 KB) TX bytes:10926 (10.9 KB)
Run Code Online (Sandbox Code Playgroud)
运行Ubuntu 14.04。
作为目前的解决方法,我正在使用socat:
echo "Hello" | socat - UDP-DATAGRAM:255.255.255.255:4000,broadcast
Run Code Online (Sandbox Code Playgroud)
但无论如何我想了解为什么 NetCat 不能做同样的事情。
我假设您使用的是netcat-openbsd,因为您指定了-4. 它必须-b启用广播地址,但众所周知,即使使用-b. Debian Bug#702204建议一个补丁来解决这个问题。
您可以安装似乎正确支持UDP的替代软件包netcat-traditional-b。请注意,当两个软件包都安装在您的系统中时,可以/bin/nc使用运行来调用哪个 netcat 进行配置update-alternatives。
$ sudo update-alternatives --config nc
There are 2 choices for the alternative nc (providing /bin/nc).
Selection Path Priority Status
------------------------------------------------------------
* 0 /bin/nc.openbsd 50 auto mode
1 /bin/nc.openbsd 50 manual mode
2 /bin/nc.traditional 10 manual mode
Press enter to keep the current choice[*], or type selection number: 2
update-alternatives: using /bin/nc.traditional to provide /bin/nc (nc) in manual mode
Run Code Online (Sandbox Code Playgroud)
最后,您可能想要添加-v以从nc.