Nmap API 是用 Python 编写的,
-v, -o, -A, --script, -Pn
将运行这些参数并向用户询问 IP 地址,并使用 -IL
参数将输出传输到“targets.txt”文件。你能写出代码吗?
我使用了nmap3,但我无法使用它们的参数运行代码。
如果尚未安装 python-nmap 库,请运行:
pip install python-nmap
现在,您可以使用以下Python代码:
import nmap
def run_nmap_scan(ip_address):
# Create an Nmap PortScanner object
nm = nmap.PortScanner()
# Define Nmap scan parameters
scan_arguments = "-v -oN - -A --script=default -Pn"
# Perform the Nmap scan
nm.scan(ip_address, arguments=scan_arguments)
# Get the scan results
scan_results = nm.csv()
# Save the scan results to "targets.txt"
with open("targets.txt", "w") as file:
file.write(scan_results)
print(f"Scan results saved to targets.txt for {ip_address}")
if __name__ == "__main__":
# Prompt the user for an IP address
target_ip = input("Enter the target IP address: ")
# Run the Nmap scan with the specified parameters
run_nmap_scan(target_ip)
此代码定义了一个函数 run_nmap_scan,它将 IP 地址作为参数,使用指定参数运行 Nmap 扫描,并将结果保存到“targets.txt”。 main 块提示用户输入 IP 地址,然后使用提供的 IP 地址调用 run_nmap_scan 函数。
注意:调整 scan_arguments 变量以包含特定用例所需的参数。提供的示例包括一些常见参数,例如 -v、-oN、-A、--script 和 -Pn。