Eseguire un port scanner con Python e Nmap

Mattepuffo's logo
Eseguire un port scanner con Python e Nmap

Eseguire un port scanner con Python e Nmap

Di Nmap ne abbiamo parlato in lungo e in largo!

Oggi vediamo come usarlo in Python attraverso la libreria python-nmap!

La potete installare tramite pip:

pip install python-nmap

Qui sotto un esempio in cui eseguiamo uno scan su un indirizzo ip:

import nmap

nm = nmap.PortScanner()

# ESEGUIAMO UNA SCANSIONE SU UNDIRIZZO / RANGE PORTE
print(nm.scan('127.0.0.1', '22-443'))
# VISUALIZIAMO L'ULTIMA SCANSIONE FATTA
print(nm.command_line())

# ESEGUIAMO OPERAZIONI SU TUTTI GLI HOST TROVATI
for host in nm.all_hosts():
    print('----------------------------------------------------')
    print('Host : %s (%s)' % (host, nm[host].hostname()))
    print('Stato : %s' % nm[host].state())
    for proto in nm[host].all_protocols():
        print('----------')
        print('Protocollo : %s' % proto)
        lport = nm[host][proto].keys()
        lport.sort()
        for port in lport:
            print('Porta : %stState : %s' % (port, nm[host][proto][port]['state']))

E se volessimo eseguire una scansione sulla rete?

import nmap

nm = nmap.PortScanner()

nm.scan(hosts='192.168.0.1/24', arguments='-n -sP')
hosts_list = [(x, nm[x]['status']['state']) for x in nm.all_hosts()]
for host, status in hosts_list:
    print(host + ': ' + status)

Ovviamente potete sperimentare con le opzioni di Nmap!

Enjopy!


Condividi

Commentami!