今天分享一个用python实现nmap扫描的方法,以下是实现步骤
代码如下:
import subprocess
missing_ips = {'166.139.144.163', '31.47.8.35', '58.242.86.191', '212.178.135.62', '103.1.35.114'}
port = "7"
for missing_ip in missing_ips:
# 构造nmap命令Z
nmap_command = ['nmap', '-sU', '-p', port, missing_ip]
# 执行命令并获取输出
result = subprocess.run(nmap_command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
# 打印输出
print(f"Scan results for {missing_ip}:{port}:")
print(result.stdout)
print(result.stderr) # 打印任何错误信息
print("------------------------------------")
运行结果:
执行即可自动下发nmap任务,并输出扫描结果