我试图编写一个脚本,它会检查给定地址的所有端口,并确定听取UDP中的哪一个。
为此,我向每个端口发送一个空数据包,然后侦听ICMP Port Unreachable响应,这意味着相应端口上没有UDP。
在这样做时,我遇到了一个问题:在收到恰好279个(每次)ICMP数据包后,我无法再接收它们。甚至在localhost上。
我已将其谷歌了,并且只了解有时此类数据包的数量有限。但有没有办法消除这种限制?
我已经简化了我的代码以显示它,这里是:
import socket
import struct
with socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.IPPROTO_ICMP) as icmpSocket:
UDPPorts = set()
for port in range(1, 2 ** 16):
with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as s:
s.sendto(b'', ('localhost', port))
UDPPorts.add(port)
while True:
data, addr = icmpSocket.recvfrom(1024)
(destPort,) = struct.unpack(">H", data[50:52])
print(destPort)
UDPPorts.remove(destPort)
我的操作系统是Ubuntu 14.04(64位)。 Python 3.4.3。 我很乐意听到任何有用的信息:一些关键字可以更好地搜索问题,精确解决方案,只需解释幻数279.一切都会很有趣。