脚本如下,需要用到IPy模块,python-nmap模块,脚本执行后,以csv文档将结果输出到屏幕上
#!/usr/bin/env python3
'''
参考链接 https://blog.csdn.net/qq_36119192/article/details/83717690
使用方法:
nmap_scan.py 192.168.0.0/24 22,3389
支持单个IP,网段扫描
网段格式支持:192.168.0.0/24,192.168.0.0/24
需要扫描的端口用逗号分隔
使用grep 过滤结果
grep -E '22|3389'
'''
import sys
import nmap
from IPy import IP
if len(sys.argv) != 3:
print("参数错误,支持格式:单个IP,后缀或掩码形式的网段")
exit(1)
scan_ip = IP(sys.argv[1])
scan_port = sys.argv[2]
# scan_ip.prefixlen()
# 将网段转所后缀形式
scan_ip = "{}".format(scan_ip)
for port in [ int(i) for i in scan_port.split(",") ]:
if port < 1 or port > 65535:
print("端口范围 1 - 65535")
exit(1)
nm=nmap.PortScanner()
nm.scan(scan_ip, scan_port,'-Pn')
hosts = nm.all_hosts()
for host in hosts:
mac = nm[host]["addresses"].get("mac", "")
tcp = nm[host]["tcp"]
ports = nm[host]["tcp"].keys()
ports_list = []
# ports_list_len = 0
for port in ports:
if tcp[port]["state"] == "open":
ports_list.append("{}".format(port))
# ports_list_len += 1
else:
ports_list.append("")
# if ports_list_len != 0:
# print("{},{},{}".format(host, mac, ",".join(ports_list)))
print("{},{},{}".format(host, mac, ",".join(ports_list)))
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。