ABOUT NETSORT
netsort.pl is a perl script that takes a list of internet hostnames or IP addresses on standard input, and sorts them based on the number of hops away they are. This is useful for quickly determining the closest host or hosts from a set of addresses, i.e. a list of FTP mirror sites.
HOW IT WORKS
The route to a remote host can easily be viewed using the UNIX traceroute command. However, this command takes a while to execute because it has to probe each router along the way, and gathering traceroute information on an entire list of hosts would probably be time-prohibitive.
The distance of a host can also be determined using the ping command, by examining the TTL field of the packet returned from the remote host. The remote host always sets the TTL field of its ICMP reply packet to a certain initial value, and it is decremented as it passes through routers. The distance in hops can be determined by subtracting the final TTL value from the initial TTL value.
The trick is determining the unknown initial TTL value. Different operating systems use different values. For instance, SunOS and IRIX use 256, Linux uses 64, Windows 95 uses 32, and Windows NT uses 64. Fortunately, computers being computers, this number is almost always a power of 2, and the number of hops between two hosts is almost always less than the difference between the powers of 2 that these hosts use for the initial TTL value. Thus, it's a fairly safe assumption that, given a final TTL value, the initial TTL value is the next highest power of 2.
This process allows a fairly quick method of sorting hosts. netsort.pl calls the ping command for each host to gather this information, so if you have a system, like SunOS, which uses a ping with non-standard options, you may need to hack the script slightly. A smarter, faster version of this script would handle the pinging itself instead of spawning a child process for each host, and perform pings in parallel.
DOWNLOAD NETSORT