DEV Community

Andrew Markhai
Andrew Markhai

Posted on

PHP Traceroute

A simple traceroute implementation in PHP, inspired by the article How Does Traceroute Work.
Result here: php-traceroute

How It Works
The idea is straightforward — send ICMP packets with increasing TTL values until:

  • We receive a reply from the target IP, or
  • The TTL reaches its maximum.

Currently, the script supports two parameters:

  • host (required) — target hostname or IP
  • max-hops (optional) — maximum TTL, defaults to 256

Known Issues & Open Questions

  • Timeout at TTL=2
    There’s a noticeable delay after sending a packet with TTL=2. In Wireshark, I see no incoming data until an NBNS packet with Registration NB SMB_NSCHECK appears. Cause unknown.

  • Why UDP in standard traceroute?
    Regular traceroute sends UDP packets, but UDP datagrams don’t carry TTL info themselves. How does TTL handling work in that case?

Notes & Implementation Details

  1. Packet inspection Used Wireshark to confirm packet similarity with standard traceroute. Initially tried capturing only Docker container traffic to avoid noise, but getprotobyname() failed inside the container (missing /etc/protocols). Instead, I ran everything locally and filtered traffic with:
udp or icmp
Enter fullscreen mode Exit fullscreen mode
  1. Socket options
    All socket_set_option parameters were found via Google. Network programming in PHP isn’t very common, so examples are scarce.

  2. CLI handling
    symfony/console is optional — I just didn’t want to deal with raw input/output.

  3. Simplified packet count
    Only one ICMP packet is sent per hop (instead of the usual three), so the output shows a single time in milliseconds.

Example Usage

php traceroute.php example.com --max-hops=30
Enter fullscreen mode Exit fullscreen mode

The script works, so feel free to try it out for fun — just don’t expect production-grade traceroute magic.

Top comments (0)