Permalink
Browse files

Merge branch 'master' of https://github.com/MyIgel/php-traceroute int…

…o MyIgel-master

Conflicts:
	traceroute.php
  • Loading branch information...
jaytaph committed Dec 29, 2013
2 parents c0184ad + 3288d7c commit ade9435f184bb0054ec9b13aa2ae85a81a69603c
Showing with 52 additions and 6 deletions.
  1. +3 −0 README
  2. +49 −6 traceroute.php
View
3 README
@@ -1 +1,4 @@
A simple traceroute example in PHP.
+
+Original by Joshua Thijssen (jaytaph)
+Edited by Igor Scheller (MyIgel)
View
@@ -29,11 +29,45 @@
define ("SOL_IP", 0);
define ("IP_TTL", 2); // On OSX, use '4' instead of '2'.
-
- $dest_url = "www.google.com"; // Fill in your own URL here, or use $argv[1] to fetch from commandline.
+
+ echo"\n ============================ PHP-Traceroute ============================\n\n";
+
+ if (!isset($argv[1]) || $argv[1] == "-h" || $argv[1] == "--help") {
+ // Show the helppage
+ echo " Usage: sudo php ".$argv[0]." [args] host\n\n";
+ echo " The available args are:\n";
+ echo " -h, --help Show this Help\n";
+ echo " -g Show GeoIP lookup for the hosts\n";
+ echo "\n";
+ echo " Example: sudo php ".$argv[0]." -g github.com\n";
+
+ echo "\n =======================================================================\n\n";
+ exit;
+ }
+
+ if ($argv[1] == "-g"){
+ $dest_url = $argv[2];
+ } else {
+ $dest_url = $argv[1];
+ }
+
$maximum_hops = 30;
$port = 33434; // Standard port that traceroute programs use. Could be anything actually.
-
+
+ function ip2geo ($host) {
+ global $argv;
+ // Get GeoIP info
+ @$data = file_get_contents('http://freegeoip.net/json/'.$host);
+ if ($data) {
+ $data = json_decode($data);
+ // Return the countrycode, the regionname and the city
+ return $data->country_code.": ".$data->region_name.": ".$data->city;
+ } else {
+ // An error has accourred
+ return "(No geo info found)";
+ }
+ }
+
// Get IP from URL
$dest_addr = gethostbyname ($dest_url);
print "Tracerouting to destination: $dest_addr\n";
@@ -68,19 +102,26 @@
socket_recvfrom ($recv_socket, $buf, 512, 0, $recv_addr, $recv_port);
// Calculate the roundtrip time
- $roundtrip_time = ( microtime(true) $t1 ) * 1000;
+ $roundtrip_time = ( microtime(true) - $t1 ) * 1000;
// No decent address found, display a * instead
if (empty ($recv_addr)) {
$recv_addr = "*";
$recv_name = "*";
} else {
- // Otherwise, fetch the hostname for the address found
+ // Otherwise, fetch the hostname and geoinfo for the address found
$recv_name = gethostbyaddr ($recv_addr);
+ if ($argv[1] == "-g") {
+ $recv_geo = ip2geo ($recv_addr);
+ }
}
// Print statistics
- printf ("%3d %-15s %.3f ms %s\n", $ttl, $recv_addr, $roundtrip_time, $recv_name);
+ if ($argv[1] == "-g") {
+ printf ("%3d %-15s %.3f ms %-30s %s\n", $ttl, $recv_addr, $roundtrip_time, $recv_geo, $recv_name);
+ } else {
+ printf ("%3d %-15s %.3f ms %s\n", $ttl, $recv_addr, $roundtrip_time, $recv_name);
+ }
} else {
// A timeout has occurred, display a timeout
printf ("%3d (timeout)\n", $ttl);
@@ -96,5 +137,7 @@
// When we have hit our destination, stop the traceroute
if ($recv_addr == $dest_addr) break;
}
+
+ echo "\n ======================================================================\n\n";
?>

0 comments on commit ade9435

Please sign in to comment.