I did this
$lines = null;
echo "vystup nmap-u s prikazom : " . $cfg->nmapcmd . ' ' . $args . ' 2>&1';
system($cfg->nmapcmd . ' ' . $args . ' 2>&1');
//here the output is stored in $lines variable
exec($cfg->nmapcmd . ' ' . $args . ' 2>&1',$lines);
// just to show whats in $lines variable
echo "<pre>" . var_export($lines, TRUE) . "</pre>\\n";
// Assume $lines is an array of all of the lines in nmap's report.
$port_mappings = preg_grep('!^\d+/\S+ \S+ !', $lines); // get the interesting lines
$parsed_port_mappings = array();
foreach($port_mappings as $port_mapping)
{
preg_match('!^(?P<port>\d+)/(?P<protocol>\S+) (?P<status>\S+) (?P<service>\S+)!', $port_mappings, $parsed_port_mappings[]);
}
// Just to see what we get
var_dump($parsed_port_mappings);
and this tells me this :
Warning: preg_match() expects parameter 2 to be string, array given in /home/dulus/public_html/php-nmap-0.3/index.php on line 192
array(1) {
[0]=>
NULL
}
line 192 is : preg_match('!(?P<port>\d+)/(?P<protocol>\S+) (?P<status>\S+) (?P<service>\S+)!', $port_mappings, $parsed_port_mappings[]);
so the second parameter needs to be an string not an array.
the output of my echo "<pre>" . var_export($lines, TRUE) . "</pre>\n"; function, which i have given there for testing is (it writes out the $lines which should be an array storing the nmap output):
array (
0 => '',
1 => 'Starting Nmap 5.00 ( http://nmap.org ) at 2012-02-02 11:28 CET',
2 => 'Interesting ports on localhost (127.0.0.1):',
3 => 'Not shown: 65527 closed ports',
4 => 'PORT STATE SERVICE',
5 => '21/tcp open ftp',
6 => '53/tcp open domain',
7 => '80/tcp open http',
8 => '443/tcp open https',
9 => '631/tcp open ipp',
10 => '953/tcp open rndc',
11 => '3306/tcp open mysql',
12 => '28029/tcp open unknown',
13 => '',
14 => 'Nmap done: 1 IP address (1 host up) scanned in 0.73 seconds',
)