shell_exec not working with nmap command

2019-02-16 01:44发布

I got a problem with the shell_exec php function, here is a example code:

$output = shell_exec('nmap -PS80 -n -oG - --send-ip 11.11.11.11');

if ( $output )
{
     echo "Output found...";
}
else
{
     var_dump( $output );
}

It does return: NULL, but when I change the shell_exec command to the following:

$output = shell_exec('echo 1');

then the output is: Output found... so its working properly, and there is no problems with permissions or safe mode (which is off , by the way).

It is having a problems with execute the nmap command. I've check that command in the shell command line in putty and its working properly:

# nmap -PS80 -n -oG - --send-ip 11.11.11.11
# Nmap 5.61TEST2 scan initiated Tue Feb 28 13:55:41 2012 as: nmap -PS80 -n -oG - --send-ip 11.11.11.11
# Nmap done at Tue Feb 28 13:55:43 2012 -- 1 IP address (0 hosts up) scanned in 0.04 seconds

So where is the problem?

2条回答
一夜七次
2楼-- · 2019-02-16 02:00

You might want to resort to exec() instead, which gives you greater error diagnostics:

// Capture outout from STDERR as well
$command = "nmap ... 2>&1";

exec($command, $output, $return_var);

// If return code is not zero, the command failed
if ($return_var != 0) 
{
    // dump all output, including error messages
    var_dump($output);
}
查看更多
淡お忘
3楼-- · 2019-02-16 02:17

Try to specify full path to nmap like /usr/local/bin/nmap. PHP might not know about nmap location. Enjoy!

查看更多
登录 后发表回答