2/7/2018 python-nmap : nmap from python
Alexandre Norman
de choses et d’autres
Blog Projets CV
Translations: fr
pytho -nmap : nmap fro pytho
Abou
python-nmap is a python library which helps in using nmap port scanner. It allows to easilly
manipulate nmap scan results and will be a perfect tool for systems administrators who
want to automatize scanning task and reports. It also supports nmap script outputs.
It can even be used asynchronously. Results are returned one host at a time to a callback
function defined by the user.
Downloa lates
python-nmap-0.6.1.tar.gz - 2016-07-30
md5sum : 2795bfcbc05cbbbccfcf4df59facaab1
Downloa developmen versio
https://xael.org/pages/python-nmap-en.html 1/7
2/7/2018 python-nmap : nmap from python
Get source from the code repository .
hg clone https://bitbucket.org/xael/python-nmap
Bug report : http://bitbucket.org/xael/python-nmap/issues
Installatio
Instal fro PIP
Installing python-nmap is just as simple as :
pip install python-nmap
Manua installatio
From the shell, uncompress python-nmap-0.4.0.tar.gz and then run make:
tar xvzf python-nmap-0.6.1.tar.gz
cd python-nmap-0.6.1
python setup.py install
Now you may invoke nmap from python:
$ python
Python 2.6.4 (r264:75706, Dec 7 2009, 18:45:15)
[GCC 4.4.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import nmap
Usag
From python :
>>> import nmap
>>> nm = nmap.PortScanner()
https://xael.org/pages/python-nmap-en.html 2/7
2/7/2018 python-nmap : nmap from python
>>> nm.scan('127.0.0.1', '22-443')
>>> nm.command_line()
'nmap -oX - -p 22-443 -sV 127.0.0.1'
>>> nm.scaninfo()
{'tcp': {'services': '22-443', 'method': 'connect'}}
>>> nm.all_hosts()
['127.0.0.1']
>>> nm['127.0.0.1'].hostname()
'localhost'
>>> nm['127.0.0.1'].state()
'up'
>>> nm['127.0.0.1'].all_protocols()
['tcp']
>>> nm['127.0.0.1']['tcp'].keys()
[80, 25, 443, 22, 111]
>>> nm['127.0.0.1'].has_tcp(22)
True
>>> nm['127.0.0.1'].has_tcp(23)
False
>>> nm['127.0.0.1']['tcp'][22]
{'state': 'open', 'reason': 'syn-ack', 'name': 'ssh'}
>>> nm['127.0.0.1'].tcp(22)
{'state': 'open', 'reason': 'syn-ack', 'name': 'ssh'}
>>> nm['127.0.0.1']['tcp'][22]['state']
'open'
>>> for host in nm.all_hosts():
>>> print('----------------------------------------------------')
>>> print('Host : %s (%s)' % (host, nm[host].hostname()))
>>> print('State : %s' % nm[host].state())
>>> for proto in nm[host].all_protocols():
>>> print('----------')
>>> print('Protocol : %s' % proto)
>>>
>>> lport = nm[host][proto].keys()
>>> lport.sort()
>>> for port in lport:
>>> print ('port : %s\tstate : %s' % (port, nm[host][proto][port]['stat
----------------------------------------------------
Host : 127.0.0.1 (localhost)
State : up
----------
Protocol : tcp
port : 22 state : open
port : 25 state : open
port : 80 state : open
port : 111 state : open
port : 443 state : open
>>> print(nm.csv())
https://xael.org/pages/python-nmap-en.html 3/7
2/7/2018 python-nmap : nmap from python
host;protocol;port;name;state;product;extrainfo;reason;version;conf
127.0.0.1;tcp;22;ssh;open;OpenSSH;protocol 2.0;syn-ack;5.9p1 Debian 5ubuntu1;10
127.0.0.1;tcp;25;smtp;open;Exim smtpd;;syn-ack;4.76;10
127.0.0.1;tcp;53;domain;open;dnsmasq;;syn-ack;2.59;10
127.0.0.1;tcp;80;http;open;Apache httpd;(Ubuntu);syn-ack;2.2.22;10
127.0.0.1;tcp;111;rpcbind;open;;;syn-ack;;10
127.0.0.1;tcp;139;netbios-ssn;open;Samba smbd;workgroup: WORKGROUP;syn-ack;3.X;10
127.0.0.1;tcp;443;;open;;;syn-ack;;
>>> nm.scan(hosts='192.168.1.0/24', arguments='-n -sP -PE -PA21,23,80,3389')
>>> hosts_list = [(x, nm[x]['status']['state']) for x in nm.all_hosts()]
>>> for host, status in hosts_list:
>>> print('{0}:{1}'.host)
192.168.1.0:down
192.168.1.1:up
192.168.1.10:down
192.168.1.100:down
192.168.1.101:down
192.168.1.102:down
192.168.1.103:down
192.168.1.104:down
192.168.1.105:down
[...]
>>> nma = nmap.PortScannerAsync()
>>> def callback_result(host, scan_result):
>>> print '------------------'
>>> print host, scan_result
>>>
>>> nma.scan(hosts='192.168.1.0/30', arguments='-sP', callback=callback_result)
>>> while nma.still_scanning():
>>> print("Waiting >>>")
>>> nma.wait(2) # you can do whatever you want but I choose to wait after the
>>>
192.168.1.1 {'nmap': {'scanstats': {'uphosts': '1', 'timestr': 'Mon Jun 7 11:31:11
------------------
192.168.1.2 {'nmap': {'scanstats': {'uphosts': '0', 'timestr': 'Mon Jun 7 11:31:11
------------------
192.168.1.3 {'nmap': {'scanstats': {'uphosts': '0', 'timestr': 'Mon Jun 7 11:31:11
>>> nm = nmap.PortScannerYield()
>>> for progressive_result in nm.scan('127.0.0.1/24', '22-25'):
>>> print(progressive\_result)
See also example.py in archive file.
https://xael.org/pages/python-nmap-en.html 4/7
2/7/2018 python-nmap : nmap from python
Contributor
Steve ‘Ashcrow’ Milner
Brian Bustin
old.schepperhand
Johan Lundberg
Thomas D. maaaaz
Robert Bost
David Peltier
Licens
This program is free software; you can redistribute it and/or modify it under the terms of
the GNU General Public License as published by the Free Software Foundation; either
version 3 of the License, or any later version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with this
program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA
02139, USA.
Menu
Projets Python
python-gantt
python-nmap
pyClamd
Catégories
A t i k
https://xael.org/pages/python-nmap-en.html 5/7
2/7/2018 python-nmap : nmap from python
Asterisk
django
Outils
Poules
Raspberry Pi
Archives
Tags
asterisk (1)
bash (2)
Bricolage (1)
debian (6)
django (1)
dovecot (1)
lighttpd (1)
méthode (2)
mpd (1)
Nagios (1)
openvpn (1)
OSM ( )
https://xael.org/pages/python-nmap-en.html 6/7
2/7/2018 python-nmap : nmap from python
OSM (2)
outils (5)
Palettes (1)
postfix (1)
postgrey (1)
Poules (1)
Python (5)
Raspberry Pi (6)
raspbian (6)
selenium (1)
shellscript (2)
voip (1)
wazo (1)
wifi (1)
Site géré avec Pelican - thème personnel
https://xael.org/pages/python-nmap-en.html 7/7