dpkt包官方文档解析汉化----ICMP Example(IP层)

dpkt包官方文档解析汉化----ICMP Example(IP层)

Print ICMP Example #打印ICMP的例子

This example expands on the print_packets example. It checks for ICMP packets and displays the ICMP contents.(这个例子扩展了print_packages的例子。它检查ICMP包并显示ICMP内容。)

Code Excerpt(代码摘录)

# For each packet in the pcap process the contents
#对pcap中的每个包处理内容
for timestamp, buf in pcap:

    # Unpack the Ethernet frame (mac src/dst, ethertype)
    #解压以太网帧(mac src/dst, ethertype)
    eth = dpkt.ethernet.Ethernet(buf)

    # Make sure the Ethernet data contains an IP packet
    #确保以太网数据包含一个IP包
    if not isinstance(eth.data, dpkt.ip.IP):
        print 'Non IP Packet type not supported %s\n' % eth.data.__class__.__name__
        continue

    # Now grab the data within the Ethernet frame (the IP packet)
    #现在获取以太网帧(IP包)中的数据
    ip = eth.data

    # Now check if this is an ICMP packet
    #现在检查这是否是一个ICMP包
    if isinstance(ip.data, dpkt.icmp.ICMP):
        icmp = ip.data

        # Pull out fragment information (flags and offset all packed into off field, so use bitmasks)
        #提取片段信息(标记和偏移量都打包到off字段中,所以使用位掩码)
        do_not_fragment = bool(ip.off & dpkt.ip.IP_DF)
        more_fragments = bool(ip.off & dpkt.ip.IP_MF)
        fragment_offset = ip.off & dpkt.ip.IP_OFFMASK

        # Print out the info
        #Print out the info
        print 'Timestamp: ', str(datetime.datetime.utcfromtimestamp(timestamp))
        print 'Ethernet Frame: ', mac_addr(eth.src), mac_addr(eth.dst), eth.type
        print 'IP: %s -> %s   (len=%d ttl=%d DF=%d MF=%d offset=%d)' % \
              (inet_to_str(ip.src), inet_to_str(ip.dst), ip.len, ip.ttl, do_not_fragment, more_fragments, fragment_offset)
        print 'ICMP: type:%d code:%d checksum:%d data: %s\n' % (icmp.type, icmp.code, icmp.sum, repr(icmp.data))

Example Output (输出)

Timestamp:  2013-05-30 22:45:17.283187
Ethernet Frame:  60:33:4b:13:c5:58 02:1a:11:f0:c8:3b 2048
IP: 192.168.43.9 -> 8.8.8.8   (len=84 ttl=64 DF=0 MF=0 offset=0)
ICMP: type:8 code:0 checksum:48051 data: Echo(id=55099, data='Q\xa7\xd6}\x00\x04Q\xe4\x08\t\n\x0b\x0c\r\x0e\x0f\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f !"#$%&\'()*+,-./01234567')

Timestamp:  2013-05-30 22:45:17.775391
Ethernet Frame:  02:1a:11:f0:c8:3b 60:33:4b:13:c5:58 2048
IP: 8.8.8.8 -> 192.168.43.9   (len=84 ttl=40 DF=0 MF=0 offset=0)
ICMP: type:0 code:0 checksum:50099 data: Echo(id=55099, data='Q\xa7\xd6}\x00\x04Q\xe4\x08\t\n\x0b\x0c\r\x0e\x0f\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f !"#$%&\'()*+,-./01234567')

...

以上参数与上篇物理层相似,不解释

dpkt/examples/print_icmp.py (dpkt / /打印icmp.py例子)

This example expands on the print_packets example. It checks for ICMP packets and displays the ICMP contents. (这个例子扩展了print_packages的例子。它检查ICMP包并显示ICMP内容。)

examples.print_icmp.mac_addr(address)
Convert a MAC address to a readable/printable string(将MAC地址转换为可读/可打印的字符串)

examples.print_icmp.inet_to_str(inet)
Convert inet object to a string (将inet对象转换为字符串)

examples.print_icmp.print_icmp(pcap)
Print out information about each packet in a pcap (在pcap中打印出每个数据包的信息)

examples.print_icmp.test()
Open up a test pcap file and print out the packets (打开一个测试pcap文件并打印出数据包)

icmp方法源文档相应位置有案列链接

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值