0

shell_exec("traceroute IPaddress) returns traceroute to IPaddress (IPaddress), 30 hops max, 40 byte packets

How do I retrieve the actual list of hops so I can tell where a problem occurs?

Nick Iredale
  • 79
  • 2
  • 11
  • 1
    Check http://www.adayinthelifeof.nl/2010/07/30/creating-a-traceroute-program-in-php/ or http://stackoverflow.com/questions/10965891/hide-command-usage-argument-on-browser-of-shell-exec-php-in-windows – Hugo Delsing Dec 04 '12 at 11:28
  • I can't get this to work - ignorance of how to construct the command with the parameters in it is probably the cause! – Nick Iredale Dec 04 '12 at 12:18

2 Answers2

1

Those messages are supposed to be written to stderr instead of the regular stdout, so I'm not too sure why you're seeing them appear in the output.

Instead of shell_exec() I would recommend using exec() because it captures both the output AND the return code of the process:

exec('traceroute example.com 2>&1', $out, $code);
if ($code) {
    die("An error occurred while trying to traceroute: " . join("\n", $out);
}
print_r($out);

To speed up the command a little you could use the -n option when you run traceroute to avoid having to do DNS lookups for the intermediate hops.

Note that running traceroute can take a while; if you run it on the command line you can sometimes see lines with * * * in them, which can take ages!

Ja͢ck
  • 170,779
  • 38
  • 263
  • 309
  • This also returns `Array ( [0] => traceroute to 88.97.30.31 (88.97.30.31), 30 hops max, 40 byte packets )` - what defines the variable $code? – Nick Iredale Dec 04 '12 at 12:25
  • @NickIredale Then your `traceroute` is messed up, it should print that message on `stderr`; `$code` gives the return value of the command; `0` means no errors, anything else means something went wrong. – Ja͢ck Dec 04 '12 at 12:29
  • ah! printing $code gives 1 not 0, so as you say it isn't working. If it was, then the command should give the list of hops, should it? – Nick Iredale Dec 04 '12 at 12:38
  • To print the errors you could do `exec('traceroute example.com 2>&1', $out, $code)`. – Ja͢ck Dec 04 '12 at 12:40
  • Thanks, you've been really helpful :) It now returns `Array ( [0] => traceroute to example.com (192.0.43.10), 30 hops max, 40 byte packets [1] => send: Operation not permitted ) 1` so the server must be set up to prevent this - I'll contact my host and see what can be done if anything – Nick Iredale Dec 04 '12 at 12:47
  • For security reasons shell access is not permitted on my server - oh well, worth a try. – Nick Iredale Dec 07 '12 at 09:24
  • @Jack Can you explain what "2>&1" means? I tried with and without it and got different results. I would like to know what it means and which one I could rely most on. At least give me a link where to read about it I have tried to google with no success. – Colandus Oct 05 '13 at 14:48
  • I have read some what it means, according to them it's only get the errors? I don't see why I get different results then. I can send you my results in private if you want to take a look at them. – Colandus Oct 05 '13 at 15:07
0

Use exec and look at its second parameter :

string exec ( string $command [, array &$output [, int &$return_var ]] )

Example :

<?php

exec('traceroute test.com -m 2', $output);
var_dump($output); 
Sorin Trimbitas
  • 1,467
  • 18
  • 35
  • Thanks, just tried that but the result is effectively the same as the original code - the array contains just the one variable - `array(1) { [0]=> string(69) "traceroute to 88.97.30.31 (88.97.30.31), 30 hops max, 40 byte packets" }`, which is the summary of the traceroute rather than the list of hops – Nick Iredale Dec 04 '12 at 11:50
  • see the edited message, m parameter means number of hops .. "traceroute --help" for more details – Sorin Trimbitas Dec 04 '12 at 11:56
  • edited version still returns the same - `array(1) { [0]=> string(68) "traceroute to 88.97.30.31 (88.97.30.31), 2 hops max, 40 byte packets" }` – Nick Iredale Dec 04 '12 at 12:28
  • for me it returns the right output ... array (size=3) 0 => string 'traceroute to test.com (174.36.85.72), 2 hops max, 60 byte packets' (length=66) 1 => string ' 1 * * *' (length=9) 2 => string ' 2 * * *' (length=9) – Sorin Trimbitas Dec 04 '12 at 12:51
  • The server doesn't permit it, that's why it won't work. Thanks for your help – Nick Iredale Dec 04 '12 at 13:02