Python icmp socket server (not tcp\udp)

后端 未结 2 1807
渐次进展
渐次进展 2021-02-04 22:08

I\'m trying to write a socket server in Python that can receive ICMP packets.

Here\'s my code:

s = socket.socket(socket.AF_INET,socket.SOCK_RAW, socket.I         


        
2条回答
  •  梦毁少年i
    2021-02-04 23:00

    Building on the accepted answer, this code unpacks the received ICMP header and displays its data (ICMP type, code, etc)

        s = socket.socket(socket.AF_INET,socket.SOCK_RAW,socket.IPPROTO_ICMP)
        s.setsockopt(socket.SOL_IP, socket.IP_HDRINCL, 1)
        while 1:
            recPacket, addr = s.recvfrom(1024)
            icmp_header = recPacket[20:28]
            type, code, checksum, p_id, sequence = struct.unpack('bbHHh', icmp_header)
            print "type: [" + str(type) + "] code: [" + str(code) + "] checksum: [" + str(checksum) + "] p_id: [" + str(p_id) + "] sequence: [" + str(sequence) + "]"
    

提交回复
热议问题