Based on the Net_Ping class,
Net_Traceroute allows you to trace the route between your server and a remote
host. Like the other modules in the PHP Extension and Application Repository
(PEAR), Net_Traceroute is available as a free download.
Once you download and install it, you can use the following simple script to
see how it works:
// include class file
// create object instance
$tr = Net_Traceroute::factory();
// trace route to host and get response
$response = $tr->traceroute(‘builder.com’);
// print response
As with Net_Ping(), the output of the call to the
traceroute() method is an object containing details of the route to the named
host. This object, structured as a series of nested arrays, contains
information on the number of hops it took to get to the host, as well as the
response times for each hop.
Listing A contains an example of what this object looks like.
Retrieving readable hop information
For most of us, the result object in Listing A needs to be
massaged into a format suitable for viewing. To this end, the Net_Traceroute
class offers a few methods to extract the relevant information from the result
object. Consider the script in Listing B,
which illustrates these methods by converting the raw output of the traceroute()
method into a neatly formatted HTML table.
In addition to making the previous script more interactive (the
user can now enter the name of the target host through a form), this version
also introduces the getHops() method, which returns an array. Each element of
this array represents one hop between the source and destination hosts, and
each element is itself structured as an array containing information on the
host name, IP address, and hop response times. An examination of Listing A will
reveal this structure more clearly.
You could add a for() loop to iterate over this array of
arrays and convert the confused jumble of information into a readable report.