Get list of traceroute hops with php

烈酒焚心 提交于 2019-12-23 05:17:16

问题


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?


回答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!




回答2:


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); 


来源:https://stackoverflow.com/questions/13701842/get-list-of-traceroute-hops-with-php

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!