Question

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.

Était-ce utile?

La solution

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);
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top