用python写一个可视化的端口扫描
时间: 2024-10-14 10:05:19 浏览: 28
在Python中,我们可以使用第三方库如`scapy`来进行网络端口扫描,并结合`matplotlib`或`seaborn`等库进行可视化结果。以下是一个简单的示例,演示如何使用`scapy`做TCP端口扫描并生成柱状图:
```python
from scapy.all import *
import matplotlib.pyplot as plt
def portscan(ip, ports):
result = []
for port in ports:
response = sr1(IP(dst=ip)/TCP(dport=port, flags="S"), timeout=2)
# 如果收到了SYN+ACK响应,说明端口开放
if response and response[TCP].flags == 0x12:
result.append((port, "Open"))
else:
result.append((port, "Closed"))
return result
# 目标IP地址和端口范围
target_ip = '192.168.1.1'
ports_to_scan = range(1, 100)
# 扫描并获取结果
scan_results = portscan(target_ip, ports_to_scan)
# 绘制柱状图
plt.bar([r[0] for r in scan_results], [r[1] for r in scan_results])
plt.xlabel('Ports')
plt.ylabel('Status')
plt.title(f'Scanning {target_ip} for Open Ports')
plt.xticks(range(1, len(scan_results)+1))
plt.show()
阅读全文