Python Nmap篇

安装:

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

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Python-nmap是一个用于操作和解析Nmap扫描结果的Python库。Nmap是一个网络探测和安全审核工具,它可以扫描大量主机并识别它们上运行的服务和开放的端口。 要使用Python-nmap库,首先需要安装它。可以使用pip命令来安装: ``` pip install python-nmap ``` 安装完成后,就可以在Python脚本中导入并使用它。以下是一个简单的示例,展示了如何使用Python-nmap来扫描主机并获取开放的端口信息: ```python import nmap # 创建一个Nmap扫描对象 nm = nmap.PortScanner() # 执行主机扫描(可以传入IP地址、主机名或CIDR) nm.scan('127.0.0.1', arguments='-p 22-443') # 遍历主机列表 for host in nm.all_hosts(): print('------------------------------------') print(f'Host: {host}') print(f'State: {nm[host].state()}') # 遍历每个协议的端口列表 for proto in nm[host].all_protocols(): print('----------') print(f'Protocol: {proto}') # 遍历每个端口 for port in nm[host][proto].keys(): print(f'Port: {port}\tState: {nm[host][proto][port]["state"]}') ``` 在上面的示例中,我们创建了一个Nmap扫描对象,并指定要扫描的主机和端口范围。然后,我们遍历扫描结果,输出每个主机和它们开放的端口信息。 请注意,执行Nmap扫描需要具有适当的权限。因此,在运行脚本时,可能需要以管理员身份或具有足够权限的帐户运行。 相关问题: 1. 除了获取端口信息,Python-nmap还可以用来做什么? 2. 如何在Python中执行Nmap扫描并获取扫描结果? 3. 如何处理Nmap扫描结果中的错误和异常情况? 4. 如何使用Python-nmap库来执行高级的Nmap扫描任务,如OS检测或服务版本识别? 5. 有没有其他可以替代Python-nmap的库或工具可以用来操作Nmap扫描结果?
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值