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 to256
Known Issues & Open Questions
Timeout at TTL=2
There’s a noticeable delay after sending a packet withTTL=2
. In Wireshark, I see no incoming data until an NBNS packet withRegistration 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
-
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
Socket options
Allsocket_set_option
parameters were found via Google. Network programming in PHP isn’t very common, so examples are scarce.CLI handling
symfony/console
is optional — I just didn’t want to deal with raw input/output.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
The script works, so feel free to try it out for fun — just don’t expect production-grade traceroute magic.
Top comments (0)