0

My main intention is to parse the data returned from a traceroute command executed through PHP

$output = shell_exec("traceroute www.xxx.com");
echo $output;

the returned output is something like this

Tracing route to

www.x.com [173.xx.xx.xx] over a maximum of 30 
hops: 1 1 ms <1 ms <1 ms 192.168.1.1 2 10 ms 10 ms 10 ms 117.xx.xx.xx 
3 11 ms 9 ms 9 ms 218.xx.xx.xx 4 10 ms 11 ms 10 ms 218.xx.xx.xx 5 216 ms 220 
ms 229 ms 59.xx.xx.xx.static.xx.xx.xx [59.xx.xx.xx] 6 203 ms 203 ms 219 ms 
121xx.xx.xx
7 328 ms 371  ms 325 ms 72.xx.xx.xx 8 301 ms 306 ms 313 ms 72.xx.xx.xx 9 
249 ms 251 ms249 ms 72.xx.xx.xx 10 256 ms 254 ms 255 ms sin04s02xx.xx.xx
[173.xx.xx.xx] Trace complete.

I want to parse or get the outputs for each time ,

I tried this

traceroute www.google.com 2>/dev/null | awk 'NR==1

{ print $5;
   exit;
}

That gives me the maximum number of hops I think. If there is a command similar to that to get the single outputs like

1 1 ms <1 ms <1 ms 192.168.1.1
2 10 ms 10 ms 10 ms 117.xx.xx.xx
3 11 ms 9 ms 9 ms 218.xx.xx.xx

maybe I can include it in a loop or so.

random
  • 9,774
  • 10
  • 66
  • 83
mega-crazy
  • 838
  • 2
  • 17
  • 36
  • 1
    Are you sure your output is actually corrupted, as opposed to appearing that way due to HTML's whitespace collapse rules? – DCoder Oct 06 '12 at 13:37
  • as long as you're not asking an awk programming question, I'd suggest the unix Q&A for help with specific unix commands. – hakre Oct 06 '12 at 13:37
  • Exact duplicate, http://stackoverflow.com/questions/1183625/parsing-data-from-traceroute-command – FirmView Oct 06 '12 at 13:38

1 Answers1

1

You could use popen():

$handle = popen("traceroute www.xxx.com 2>&1", "r");
while(!feof($handle)) {
    $buffer = fgets($handle);
    $buffer = "<p>".$buffer."</p>\n";
    echo $buffer;
}
pclose($handle);
Mihai Iorga
  • 39,330
  • 16
  • 106
  • 107