Skip to content

Impacket ping.py

Back to the category index / Back to the command index

A small example that sends ICMP Echo requests using a raw IPv4 socket and Impacket’s packet structures.

ItemDetails
Implementationexamples/ping.py
CategoryPackets / ICMP / capture
Size93 lines
DistributionInstalled as a script through scripts=examples/*.py in setup.py

This older sample does not handle -h. Check its usage message and top-level sys.argv handling.

python examples/ping.py <src-ip> <dst-ip>
  • src-ip: Source address set in the IPv4 header; also used to match the destination of replies.
  • dst-ip: Destination for Echo Requests; also used to match the source of replies.
  • There are no additional options. With fewer than two arguments, it prints usage and exits with code 1.

Related technology: ImpactPacket in Packets / registry / ESE / NTFS.

The script builds an IP/ICMP Echo packet for the destination, calculates its checksums, and sends it through a raw socket. It waits for a response with select, decodes the received buffer with IPDecoder, and displays the matching Echo Reply’s source, sequence, and round-trip time.

Rather than calling the OS ping utility, this implementation assembles the IPv4 header, ICMP Echo Request, and payload as ImpactPacket objects. It sets the source and destination in the IP header, sets the ICMP type and identifier, layers the objects, calculates checksums, and sends the completed packet through a raw socket with IP_HDRINCL enabled.

Each loop increments the ICMP identifier and waits up to one second for a reply with select. IPDecoder turns the received bytes back into an IPv4-to-ICMP hierarchy. The script checks whether the source, destination, and ICMP type match the requested Echo Reply. This code uses the identifier as a sequence counter rather than the sequence field.

A missing response does not prove that the host is down. ICMP filtering, routing, source-spoofing prevention, raw socket permissions, or target policy can suppress it. The script does not implement RTT statistics, packet-loss totals, or multiple outstanding requests. When extending it, match the identifier, sequence, and payload so unrelated ICMP packets are not counted as replies.

SymbolLocationWhere to trace its responsibility
module-level entry1Parses arguments and runs at the top level
ModuleMain names used
impacketImpactDecoder, ImpactPacket

Output, side effects, and required privileges

Section titled “Output, side effects, and required privileges”
  • Performs network probing or live packet capture. Check the send and receive scope, interface, filters, and handling of captured data.
  • Raw socket or packet capture privileges may be required. Captured data may include third-party traffic.
  • Distinguish a missing input file, a damaged file, an incomplete read, and an unsupported format.

The code depends on ImpactPacket, ImpactDecoder, and standard raw sockets. This positional-only sample does not use argparse. It normally needs raw socket privileges; do not weaken identifier or sequence matching.

  • Syntax: python -m py_compile examples/ping.py
  • CLI: Use a subprocess to check the usage message and exit code with no or too few arguments.
  • No test specific to this command was found. Choose shared-module tests and, if needed, a run in an isolated environment.