安装:
window10 64 先安装的nmap 在安装的python2.6 和python3.5
经检验 python2.6 与 python nmap的包兼容性更高。 python3.5 好像是需要修改 nmap 的安装路径(就不告诉你怎么修改)(我会告诉你我其实也不会改)。
文档:
自带文档: nmap.html (在压缩包里)
官方文档:http://xael.org/pages/python-nmap-en.html
https://pypi.python.org/pypi/python-nmap
使用:
#coding: utf-8
import nmap
#Host = '127.0.0.1'
#Port = '21-445'
#nm = nmap.PortScanner()
#nm.scan(Host, Port)
"""
scan(self, hosts='127.0.0.1', ports=None, arguments='-sV', sudo=False)
Scan given hosts
May raise PortScannerError exception if nmap output was not xml
Test existance of the following key to know
if something went wrong : ['nmap']['scaninfo']['error']
If not present, everything was ok.
:param hosts: string for hosts as nmap use it 'scanme.nmap.org' or '198.116.0-255.1-127' or '216.163.128.20/20'
:param ports: string for ports as nmap use it '22,53,110,143-4564'
:param arguments: string of arguments for nmap '-sU -sX -sC'
:param sudo: launch nmap with sudo if True
:returns: scan_result as dictionnary
"""
#print nm.command_line()
"""
command_line(self)
returns command line used for the scan
may raise AssertionError exception if called before scanning
"""
#print nm.scaninfo()
"""
scaninfo(self)
returns scaninfo structure
{'tcp': {'services': '22', 'method': 'connect'}}
may raise AssertionError exception if called before scanning
"""
#print nm.all_hosts()
"""
all_hosts(self)
returns a sorted list of all hosts
"""
#print nm[Host].hostname()
"""
"""
#print nm[Host].state()
"""
"""
#print nm[Host].all_protocols()
"""
"""
#print nm[Host]['tcp'].keys()
"""
"""
#print nm[Host].has_tcp(21)
"""
"""
#print nm[Host]['tcp'][21]
"""
"""
#print nm[Host].tcp(21)
"""
"""
#print nm[Host]['tcp'][21]['state']
"""
"""
#print nm.csv()
"""
csv(self)
returns CSV output as text
Example :
host;hostname;hostname_type;protocol;port;name;state;product;extrainfo;reason;version;conf;cpe
127.0.0.1;localhost;PTR;tcp;22;ssh;open;OpenSSH;protocol 2.0;syn-ack;5.9p1 Debian 5ubuntu1;10;cpe
127.0.0.1;localhost;PTR;tcp;23;telnet;closed;;;conn-refused;;3;
127.0.0.1;localhost;PTR;tcp;24;priv-mail;closed;;;conn-refused;;3;
"""
#print nm.get_nmap_last_output()
"""
get_nmap_last_output(self)
Returns the last text output of nmap in raw text
this may be used for debugging purpose
:returns: string containing the last text output of nmap in raw text
"""
#print nm.listscan(Host)
"""
listscan(self, hosts='127.0.0.1')
do not scan but interpret target hosts and return a list a hosts
"""
#print nm.nmap_version()
"""
nmap_version(self)
returns nmap version if detected (int version, int subversion)
or (0, 0) if unknown
:returns: (nmap_version_number, nmap_subversion_number)
"""
#print nm.scanstats()
"""
scanstats(self)
returns scanstats structure
{'uphosts': '3', 'timestr': 'Thu Jun 3 21:45:07 2010', 'downhosts': '253', 'totalhosts': '256', 'elapsed': '5.79'}
may raise AssertionError exception if called before scanning
"""
"""
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]['state']))
"""
"""
----------------------------------------------------
Host : 127.0.0.1 ()
State : up
----------
Protocol : tcp
port : 80 state : open
port : 81 state : open
port : 135 state : open
port : 137 state : filtered
port : 445 state : open
"""
"""
nm = nmap.PortScanner()
nm.scan(hosts='192.168.1.0/30', 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(host+' '+status)
"""
"""
220.181.112.204 up
220.181.112.207 up
220.181.112.208 up
220.181.112.211 up
220.181.112.212 up
220.181.112.215 up
220.181.112.218 up
220.181.112.219 up
[...]
"""
"""
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)
"""
"""
2015/05/08 (v0.3.5)
- correcting a bug in all_protocols()
- correcting issue 8 : PortScannerAsync Doesn't work in windows...日了狗了
"""
"""
nm = nmap.PortScannerYield()
for i in nm.scan('127.0.0.1/24', '22-25'):
print(i)
"""
"""
nm = nmap.PortScannerYield()
for progressive_result in nm.scan('127.0.0.1/24', '22-25'):
print(progressive_result)
"""
"""
"""
其他知识:
Nmap所识别的6个端口
open(开放的)
closed(关闭的)
filtered(被过滤的)
unfiltered(未被过滤的)
open|filtered(开放或者被过滤的)
closed|filtered(关闭的或者被的)
127.0.0.1/24 个人感觉表示一段IP地址
转载地址:
http://xael.org/pages/python-nmap-en.html
https://pypi.python.org/pypi/python-nmap
http://blog.csdn.net/lee244868149/article/details/39177669
http://blog.csdn.net/www3300300/article/details/38680843