树莓派用tcpdump与socat抓包重传时遇Permission denied错误求助
解决socat发送UDP广播时的Permission Denied问题
你在树莓派上通过tcpdump捕获多播包、再用socat转发为广播包时,遇到了如下权限错误:
tcpdump: listening on eth0, link-type EN10MB (Ethernet), snapshot length 262144 bytes
2023/09/26 20:17:17 socat[2400] E sendto(5, 0x3fb000, 24, 0, AF=2 192.168.7.255:14555, 16): Permission denied
使用的脚本如下:
#!/bin/bash # Define the multicast group and port to capture multicast_group="224.1.1.1" # Replace with your specific multicast group address multicast_port=12345 # Replace with the multicast port number # Define the broadcast address and port to retransmit broadcast_address="192.168.xxx.xxx" # Broadcast address for the target network forward_port=54321 # Replace with the port number you want to use for forwarding while true; do # Start tcpdump to capture UDP multicast packets sudo tcpdump -i eth0 -n -s0 -U -w - "udp and port $multicast_port" | \ sudo socat -u - UDP4-DATAGRAM:$broadcast_address:$forward_port done
问题原因
这个错误并非因为缺少sudo权限,而是socat默认禁止发送UDP广播包,即使以root身份运行也需要显式开启广播允许选项。
解决步骤
- 修改socat的参数,在
UDP4-DATAGRAM目标后添加,broadcast选项,允许发送广播包:sudo tcpdump -i eth0 -n -s0 -U -w - "udp and port $multicast_port" | \ sudo socat -u - UDP4-DATAGRAM:$broadcast_address:$forward_port,broadcast - 确保
broadcast_address是当前子网的有效广播地址(比如子网掩码为255.255.255.0时,广播地址通常是192.168.x.255)。 - 重新运行脚本(可直接用
sudo ./your_script.sh执行,或确保脚本有可执行权限后再用sudo运行)。
内容的提问来源于stack exchange,提问作者dgholmes
相关产品推荐
相关产品推荐

