-
nmap下载:nmap
-
将所在路径放入环境变量:
D:\Program Files\Nmap\zenmap\bin
- 安装模块
pip install python-nmap
python代码实现:
import nmap
def scan(host, min_port, max_port):
port_range = str(min_port) + '-' + str(max_port)
nm = nmap.PortScanner()
# 使用-sV选项获取端口上的服务信息
nm.scan(host, port_range, arguments='-sV')
# 输出主机扫描结果
for host in nm.all_hosts():
print('Host : %s (%s)' % (host, nm[host].hostname()))
for proto in nm[host].all_protocols():
print('----------')
print('Protocol : %s' % proto)
for port in nm[host][proto].keys():
print('port : %s\tstate : %s' % (port, nm[host][proto][port]['state']))
if 'service' in nm[host][proto][port]:
print('service : %s' % nm[host][proto][port]['service'])
print('version : %s' % nm[host][proto][port]['service']['version'])
if __name__ == '__main__':
scan("baidu.com", 80, 85)
输出: