环境mac
pip3 install python-nmap #先安装
导入包
import nmap
nm = nmap.PortScanner()
nm.scan(hosts=‘10.12.11.67/24’,arguments=‘-n -sP -PE’)
结果
{‘nmap’: {‘command_line’: ‘nmap -oX – -n -sP -PE 10.12.11.0/24’,
‘scaninfo’: {‘warning’: [‘Warning: You are not root — using TCP pingscan rather than ICMP\n’]},
‘scanstats’: {‘downhosts’: ‘254’,
‘elapsed’: ‘6.64’,
‘timestr’: ‘Tue Mar 27 04:13:39 2018’,
‘totalhosts’: ‘256’,
‘uphosts’: ‘2’}},
‘scan’: {‘10.12.11.50’: {‘addresses’: {‘ipv4’: ‘10.12.11.50’},
‘hostnames’: [{‘name’: ”, ‘type’: ”}],
‘status’: {‘reason’: ‘syn-ack’, ‘state’: ‘up’},
‘vendor’: {}},
‘10.12.11.67’: {‘addresses’: {‘ipv4’: ‘10.12.11.67’},
‘hostnames’: [{‘name’: ”, ‘type’: ”}],
‘status’: {‘reason’: ‘conn-refused’, ‘state’: ‘up’},
‘vendor’: {}}}}
nm.all_hosts()
[‘10.12.11.50’, ‘10.12.11.67’]
host是网段
arguments是参数。
-n只扫描IP,不扫描解析。
-sP扫描一次,提高效率
-PE扫描服务端口
nm.all_hosts()返回扫描到的主机IP