Skip to content

Instantly share code, notes, and snippets.

@tbhaxor
Last active Dec 3, 2019
Embed
What would you like to do?
Simple ICMP Packet Sniffer
import os
import socket
from argparse import ArgumentParser, RawDescriptionHelpFormatter
# configuring the argument
parser = ArgumentParser(description="host discovery tool",
formatter_class=RawDescriptionHelpFormatter)
parser.add_argument("--host",
help="hostname to bind",
metavar="HOSTNAME/IP",
required=True)
# actually parsing the args
args = parser.parse_args()
# instancing the sniffer
# setting the protocol to capture only ICMP Packets
sniffer = socket.socket(socket.AF_INET,
socket.SOCK_RAW,
proto=socket.IPPROTO_ICMP)
# binding to the host:port
sniffer.bind((args.host, 0))
# including the ip header in capture packet
sniffer.setsockopt(socket.IPPROTO_IP, socket.IP_HDRINCL, 1)
# getting the raw packet data along with ip and port
pkt = sniffer.recvfrom(65565)
# formatting
print("Remote IP:", pkt[1][0])
print("Remote PORT:", pkt[1][1])
print("Data:", pkt[0])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment