NMAP result using PHP - how to show the NMAP result in a table in the browser?

1

I'm trying to make the output of the nmap result appear in the browser in the form of a table. follow the code below:

<?php
$host = $_POST["host"];
$saida = shell_exec('nmap -P0 ' . $host);
$vetorLinhas = explode("\n", $saida);
echo "<pre>$alinhamento</pre>";
?>

<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>tecnologia</title>
    </head>
    <body>
        <table>
            <tr>
                <td colspan="5">Scanner de Porta TCP/UDP</td>
            </tr>
            <tr>
                <td height="60" colspan="5" align="center"> 
                    <form id="form1" name="form1" method="post" action="exemplo.php">
                        <br/>
                        Endereço de Host
                        <label for="textfield">
                            <input type="text" name="host" id="textfield" />
                            <input type="submit" name="button" id="button" value="Ir" />
                        </label>
                    </form>
                </td>
            </tr>
            <tr>
                <td height="60" colspan="5">
                    <pre>
                        <?php
                        echo "<b><center>$host</center></b><br>";
                        if (count($vetorLinhas)) {
                            foreach ($vetorLinhas as $indice => $linha) {
                                echo "<pre>$indice - $linha</pre>";
                            }
                        }
                        ?>
                    </pre>
                </td>
            </tr>
        </table>
    </body>
</html>

It's like this in the browser

0 - 
1 - Starting Nmap 7.01 ( https://nmap.org ) at 2016-11-20 19:36 BRT
2 - Nmap scan report for localhost (127.0.0.1)
3 - Host is up (0.000088s latency).
4 - Not shown: 994 closed ports
5 - PORT     STATE SERVICE
6 - 21/tcp   open  ftp
7 - 80/tcp   open  http
8 - 139/tcp  open  netbios-ssn
9 - 445/tcp  open  microsoft-ds
10 - 3306/tcp open  mysql
11 - 5432/tcp open  postgresql
12 - 
13 - Nmap done: 1 IP address (1 host up) scanned in 0.07 seconds
14 -

The question is how would this algorithm in PHP go out in table format in html from the line "PORT STATE SERVICE" and stop displaying in line "Nmap done: 1 IP address (1 host up) scanned in 0.07 seconds" ??

    
asked by anonymous 20.11.2016 / 23:55

1 answer

2

You can do this:

$saida = shell_exec('nmap -P0 localhost');
$vetorLinhas = explode("\n", $saida);

$start = array_keys(preg_grep('/^PORT/', $vetorLinhas))[0];

$data = array_slice($vetorLinhas, $start, -3)

echo "<pre>"; print_r($data); exit;

Output:

Array
(
    [0] => PORT     STATE SERVICE
    [1] => 25/tcp   open  smtp
    [2] => 80/tcp   open  http
    [3] => 587/tcp  open  submission
    [4] => 902/tcp  open  iss-realsecure
    [5] => 3306/tcp open  mysql
)

Explanation:

The variable $start will get the index of the line that starts with PORT, the $data will receive the range of the array that goes from $start to 3 lines before finishing (removing Nmap done ...) .

NOTE: I used this answer to complete the reasoning: link

    
21.11.2016 / 12:57