Source code for examples.print_icmp

#!/usr/bin/env python
"""
This example expands on the print_packets example. It checks for ICMP packets and displays the ICMP contents.
"""
import dpkt
import datetime
import socket
from dpkt.compat import compat_ord


[docs]def mac_addr(address): """Convert a MAC address to a readable/printable string Args: address (str): a MAC address in hex form (e.g. '\x01\x02\x03\x04\x05\x06') Returns: str: Printable/readable MAC address """ return ':'.join('%02x' % compat_ord(b) for b in address)
[docs]def inet_to_str(inet): """Convert inet object to a string Args: inet (inet struct): inet network address Returns: str: Printable/readable IP address """ # First try ipv4 and then ipv6 try: return socket.inet_ntop(socket.AF_INET, inet) except ValueError: return socket.inet_ntop(socket.AF_INET6, inet)
[docs]def test(): """Open up a test pcap file and print out the packets""" with open('data/dns_icmp.pcap', 'rb') as f: pcap = dpkt.pcap.Reader(f) print_icmp(pcap)
if __name__ == '__main__': test()