Skip to content

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also .

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also .
  • 4 commits
  • 2 files changed
  • 0 commit comments
  • 2 contributors
Commits on Aug 20, 2013
@MyIgel MyIgel Added GeoIP support and help page
The GeoIP is based on the freegeoip.net RESTful web service API
The helppage shows you the args and an example
a9a8cde
Commits on Aug 21, 2013
@jaytaph Create LICENSE
Signed-off-by: Igor Scheller <igor.scheller@igorshp.de>
cf308c7
@jaytaph Update traceroute.php
Signed-off-by: Igor Scheller <igor.scheller@igorshp.de>
3288d7c
Commits on Dec 29, 2013
@jaytaph Merge branch 'master' of https://github.com/MyIgel/php-traceroute int…
…o MyIgel-master

Conflicts:
	traceroute.php
ade9435
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
55 traceroute.php
@@ -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";
?>

No commit comments for this range

Something went wrong with that request. Please try again.