Test Pypcap Code (Environment: Windows, Python2.7)

Source: Internet
Author: User
Tags ord unpack

import pcapimport sysimport stringimport timeimport socketimport struct# Protocols={socket. IPPROTO_TCP: ' TCP ',            #socket. IPPROTO_UDP: ' UDP ',            #socket. IPPROTO_ICMP: ' ICMP '}protocols={    0x00: ' hopopt ',     0x01: ' ICMP ',     0X02: "IGMP",     0x03: "GGP",     0x04: "IP-in-IP" ,     0x05: "ST",     0x06: "TCP",     0x07: "CBT",     0x08: "EGP",     0x09: "IGP",     0x0a: " Bbn-rcc-mon ",     0x0b:" Nvp-ii ",     0x0c:" PUP ",     0x0D: "ARGUS",     0x0e: "Emcon",     0x0f: "XNET",     0x10: "CHAOS",     0x11: "UDP",  &Nbsp;  0x12: "MUX",     0x13: "Dcn-meas",     0x14: "HMP",     0x15: "PRM",     0x16: "XNS-IDP",     0x17: "TRUNK-1",     0x18: "TRUNK-2",     0x19: "LEAF-1",     0x1a: " LEAF-2 ",     0x1b:" RDP ",     0x1c:" IRTP ",     0x1d:" ISO-TP4 ",     0x1e:" Netblt ",     0x1f:" MFE-NSP ",     0x20: "Merit-inp",     0x21: "DCCP",     0x22: "3PC",     0x23: "IDPR",     0x24: "XTP",     0x25: "DDP",     0x26: "Idpr-cmtp",     0x27: "tp++",     0x28: "IL",     0x29: "IPv6",     0x2a: "Sdrp",     0x2b: "IPv6-Route",     0X2C: "Ipv6-frag",     0x2d: "Idrp",     0x2e: "RSVP",     0x2f: "GRE",     0x30: "Mhrp",     0x31: "BNA",     0x32: "ESP",     0X33: "AH",     0x34: "I-nlsp",     0x35: "SWIPE",     0x36: "Narp",     0x37: "MOBILE",     0x38: "TLSP",     0x39: "SKIP",     0x3a: "ipv6-icmp",     0x3b: " Ipv6-nonxt ",     0x3c:" Ipv6-opts ",     0x3d:" host internal  Protocol ",  #any     0x3e:" Cftp ",     0x3f:" Local network ",   #any      0x40: "Sat-expak",     0x41: "Kryptolan",     0x42: "RVD",     0x43: "IPPC",     0x44: "Distributed file  system ",  #any &NBsp;    0x45: "Sat-mon",      0x46: "VISA",      0x47: "IPCU",      0x48: "Cpnx",      0x49: "CPHB",      0X4A: "WSN",      0x4b: "PVP",      0x4c : "Br-sat-mon",      0x4d: "Sun-nd",      0x4e: "WB-MON",      0x4f: "Wb-expak",      0x50: "Iso-ip",      0x51: "Vmtp",      0x52: "Secure-vmtp",      0x53: "VINES",      0x54: "TTP",      0x54: "Iptm",      0x55: "NSFNET-IGP",      0x56: "DGP",      0x57: "TCF",      0x58: "EIGRP",      0x59: "OSPF",      0X5A: "Sprite-rpc",      0X5B: "LARP",      0x5c: "MTP",      0X5D: "ax.25",      0x5e: "Ipip",      0x5f: "MICP",      0x60: "Scc-sp",      0x61: "Etherip",      0x62: "EnCap",      0x63: "",      0x64: "GMTP",      0X65: "IFMP",      0x66: "PNNI",      0x67: "PIM",      0x68: "ARIS",      0x69: "SCPs",      0X6A: "QNX",      0x6b: "a/n",      0x6c: " Ipcomp ",      0x6d:" SNP ",      0x6e:" Compaq-peer ",      0x6f: "Ipx-in-ip",      0x70: "VRRP",      0x71: "PGM",      0x72: "",      0x73: "L2TP",      0x74: "DDX",      0X75: "IATP",      0x76: "STP",      0x77: "SRP ",      0x78:" UTI ",      0x79:" SMP ",      0X7A: "SM",      0x7b: "PTP",      0x7c: "IS-IS over  ipv4 ",      0x7d:" Fire ",      0x7e:" CRTP ",      0x7f: "Crudp",      0x80: "Sscopmce",      0X81: "Iplt",      0x82: "SPS",      0x83: "PIPE",      0x84: "SCTP",      0x85: "FC",      0x86: " Rsvp-e2e-ignore ",      0x87:" Mobility header ",      0x88: "Udplite",     0x89: "Mpls-in-ip",      0x8a: "Manet",      0x8b: " HIP ",      0x8c:" Shim6 ",      0x8d:" Wesp ",      0x8e: "ROHC",  }import socketsocket.inet_ntoadef decode_ip_packet (s):     d={}    d[' Version ']= (Ord (s[0))  & 0xf0)  >> 4     d[' Header_len ']=ord (s[0])  & 0x0f    d[' tos ']=ord (s[1 ])     d[' Total_len ']=socket.ntohs (struct.unpack (' H ', S[2:4]) [0])     d[' Id ']=socket.ntohs (struct.unpack (' H ', S[4:6]) [0])     d[' flags ']= (ORD (s[6])  &  0XE0)  >> 5    d[' Fragment_offset ']=socket.ntohs (struct.unpack (' H ', S[6:8]) [ 0] & 0x1f)     d[' ttl ']=ord (s[8])     d[' protocol ']=ord (s[ 9])     d[' checksum ']=socket.ntohs (struct.unpack (' H ', S[10:12]) [0])     d[' source_address ']= Socket.inet_ntoa (s[12:16])      d[' destination_address ']=socket.inet_ntoa (s[16:20])     if d[' Header_len ']>5:        d[' Options ']=s[20:4* (d[' Header_len ']-5)]    else:         d[' options ']=none    d[' data ']=s[4*d[' Header_len ']:]    return  ddef dumphex (s):     bytes = map (lambda x:  '%.2x '  %  x, map (Ord, s))     for i in xrange (0,len (bytes)/16):         print  '     %s '  % string.join ( BYTES[I*16: (i+1) *16], '   ')         print  '      %s '  % string.join (bytes[(i+1) *16:], '   ') Def print_packet ( data, timestamp):     if not  data:        return    if data[12:14]== ' \x08\x00 ':  #IP   Pack         decoded=decode_ip_packet (data[14:])         print  ' \n%s.%f %s > %s '  %  ( Time.strftime ('%h:%m ',                                                    Time.localtime (timestamp)),                                     timestamp % 60,                                     decoded[' source_address '],                                     decoded[' destination_address ')         for  key in [' version ',  ' Header_len ',  ' tos ',  ' total_len ',  ' id ',                      ' Flags ',  ' Fragment_offset ',  ' ttl ']:             print  '   %s: %d '  %  (Key, decoded[key])          print  '   protocol: %s '  % protocols[decoded[' protocol ']]         print  '   header checksum: %d '  % decoded[' checksum ']          #print   '   data: '           #dumphex (decoded[' data ') if __name__== ' __main__ ':    print  (  Pcap.findalldevs ()  )     for dev in pcap.findalldevs ():         net, mask = pcap.lookupnet (Dev)          PRINT DEV        PRINT NET.__REPR__ ( ), mask.__repr__ ()     p = pcap.pcap ()     net, mask  = pcap.lookupnet (Dev)     try:        for  timestamp, data&Nbsp;in p:            print_packet ( data ,  timestamp)              #print  timestamp ,  len (data)     except KeyboardInterrupt:         print  ('%s '  % sys.exc_type)         print   (' Shutting down ')


Test Pypcap Code (Environment: Windows, Python2.7)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.