Shell_exec does not work with nmap command

I am having a problem with the php function shell_exec, here is a sample code:

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

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

It returns: NULLbut when I change the command shell_execto the following:

$output = shell_exec('echo 1');

then exit:, Output found...so it works correctly, and there are no problems with permissions or safe mode (which, by the way, is disabled).

He is having trouble executing the command nmap. I checked this command on the shell command line in putty and its proper operation:

# 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?

+1
source share
2 answers

Try specifying the full path to nmap as /usr/local/bin/nmap. PHP may not know the location of nmap. Enjoy it!

+4

exec(), :

// 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);
}
+2

Source: https://habr.com/ru/post/1545033/


All Articles