python 使用nmap 模块

官网 https://pypi.org/project/python-nmap/

>>> import nmap
>>> nm = nmap.PortScannerScanner()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: module 'nmap' has no attribute 'PortScannerScanner'
>>> nm = nmap.PortScanner()
>>> nm.scan('127.0.0.1','22-443')
{'nmap': {'command_line': 'nmap -oX - -p 22-443 -sV 127.0.0.1', 'scaninfo': {'tcp': {'method': 'syn', 'services': '22-443'}}, 'scanstats': {'timestr': 'Wed Oct 23 17:25:01 2019', 'elapsed': '6.20', 'uphosts': '1', 'downhosts': '0', 'totalhosts': '1'}}, 'scan': {'127.0.0.1': {'hostnames': [{'name': 'localhost', 'type': 'PTR'}], 'addresses': {'ipv4': '127.0.0.1'}, 'vendor': {}, 'status': {'state': 'up', 'reason': 'localhost-response'}, 'tcp': {22: {'state': 'open', 'reason': 'syn-ack', 'name': 'ssh', 'product': 'OpenSSH', 'version': '7.4', 'extrainfo': 'protocol 2.0', 'conf': '10', 'cpe': 'cpe:/a:openbsd:openssh:7.4'}, 25: {'state': 'open', 'reason': 'syn-ack', 'name': 'smtp', 'product': 'Postfix smtpd', 'version': '', 'extrainfo': '', 'conf': '10', 'cpe': 'cpe:/a:postfix:postfix'}, 111: {'state': 'open', 'reason': 'syn-ack', 'name': 'rpcbind', 'product': '', 'version': '2-4', 'extrainfo': 'RPC #100000', 'conf': '10', 'cpe': ''}}}}}
>>> nm.command_line
<bound method PortScanner.command_line of <nmap.nmap.PortScanner object at 0x2ba54ca28978>>
>>> nm.command_line()
'nmap -oX - -p 22-443 -sV 127.0.0.1'
>>> nm.scaninfo
<bound method PortScanner.scaninfo of <nmap.nmap.PortScanner object at 0x2ba54ca28978>>
>>> nm.scaninfo()
{'tcp': {'method': 'syn', 'services': '22-443'}}
>>> nm.scaninfo()['tcp']
{'method': 'syn', 'services': '22-443'}
>>> nm.scaninfo()['tcp']['method']
'syn'
>>> nm.all_hosts()
['127.0.0.1']
>>> nm['127.0.0.1'].hostname()
'localhost'
>>> nm['127.0.0.1'].hostnames()
[{'name': 'localhost', 'type': 'PTR'}]
>>> nm.scan(hosts="192.168.1.0/24",arguments="-n -sP -PE")
{'nmap': {'command_line': 'nmap -oX - -n -sP -PE 192.168.1.0/24', 'scaninfo': {}, 'scanstats': {'timestr': 'Wed Oct 23 17:29:13 2019', 'elapsed': '1.07', 'uphosts': '15', 'downhosts': '241', 'totalhosts': '256'}}, 'scan': {'192.168.1.1': {'hostnames': [{'name': '', 'type': ''}], 'addresses': {'ipv4': '192.168.1.1'}, 'vendor': {}, 'status': {'state': 'up', 'reason': 'echo-reply'}}, '192.168.1.43': {'hostnames': [{'name': '', 'type': ''}], 'addresses': {'ipv4': '192.168.1.43'}, 'vendor': {}, 'status': {'state': 'up', 'reason': 'echo-reply'}}, '192.168.1.50': {'hostnames': [{'name': '', 'type': ''}], 'addresses': {'ipv4': '192.168.1.50'}, 'vendor': {}, 'status': {'state': 'up', 'reason': 'echo-reply'}}, '192.168.1.72': {'hostnames': [{'name': '', 'type': ''}], 'addresses': {'ipv4': '192.168.1.72'}, 'vendor': {}, 'status': {'state': 'up', 'reason': 'echo-reply'}}, '192.168.1.74': {'hostnames': [{'name': '', 'type': ''}], 'addresses': {'ipv4': '192.168.1.74'}, 'vendor': {}, 'status': {'state': 'up', 'reason': 'echo-reply'}}, '192.168.1.77': {'hostnames': [{'name': '', 'type': ''}], 'addresses': {'ipv4': '192.168.1.77'}, 'vendor': {}, 'status': {'state': 'up', 'reason': 'echo-reply'}}, '192.168.1.80': {'hostnames': [{'name': '', 'type': ''}], 'addresses': {'ipv4': '192.168.1.80'}, 'vendor': {}, 'status': {'state': 'up', 'reason': 'echo-reply'}}, '192.168.1.103': {'hostnames': [{'name': '', 'type': ''}], 'addresses': {'ipv4': '192.168.1.103'}, 'vendor': {}, 'status': {'state': 'up', 'reason': 'echo-reply'}}, '192.168.1.203': {'hostnames': [{'name': '', 'type': ''}], 'addresses': {'ipv4': '192.168.1.203'}, 'vendor': {}, 'status': {'state': 'up', 'reason': 'echo-reply'}}, '192.168.1.204': {'hostnames': [{'name': '', 'type': ''}], 'addresses': {'ipv4': '192.168.1.204'}, 'vendor': {}, 'status': {'state': 'up', 'reason': 'echo-reply'}}, '192.168.1.211': {'hostnames': [{'name': '', 'type': ''}], 'addresses': {'ipv4': '192.168.1.211'}, 'vendor': {}, 'status': {'state': 'up', 'reason': 'echo-reply'}}, '192.168.1.222': {'hostnames': [{'name': '', 'type': ''}], 'addresses': {'ipv4': '192.168.1.222'}, 'vendor': {}, 'status': {'state': 'up', 'reason': 'echo-reply'}}, '192.168.1.243': {'hostnames': [{'name': '', 'type': ''}], 'addresses': {'ipv4': '192.168.1.243'}, 'vendor': {}, 'status': {'state': 'up', 'reason': 'echo-reply'}}, '192.168.1.245': {'hostnames': [{'name': '', 'type': ''}], 'addresses': {'ipv4': '192.168.1.245'}, 'vendor': {}, 'status': {'state': 'up', 'reason': 'echo-reply'}}, '192.168.1.255': {'hostnames': [{'name': '', 'type': ''}], 'addresses': {'ipv4': '192.168.1.255'}, 'vendor': {}, 'status': {'state': 'up', 'reason': 'echo-reply'}}}}
>>> nm.all_hosts()
['192.168.1.1', '192.168.1.103', '192.168.1.203', '192.168.1.204', '192.168.1.211', '192.168.1.222', '192.168.1.243', '192.168.1.245', '192.168.1.255', '192.168.1.43', '192.168.1.50', '192.168.1.72', '192.168.1.74', '192.168.1.77', '192.168.1.80']

猜你喜欢

转载自www.cnblogs.com/zoulixiang/p/11727633.html