Skip to content

Instantly share code, notes, and snippets.

@tbhaxor
Last active December 3, 2019 14:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tbhaxor/4c5df18eb8a62b08847e2000d68004b9 to your computer and use it in GitHub Desktop.
Save tbhaxor/4c5df18eb8a62b08847e2000d68004b9 to your computer and use it in GitHub Desktop.
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