Use the nmap module of python to scan the host to generate csv documents
The script is as follows. You need to use IPy module and python-nmap module. After the script is executed, output the result to the screen in a csv document.
#! / usr/bin/env python3''' reference link https://blog.csdn.net/qq_36119192/article/details/83717690 usage: nmap_scan.py 192.168.0.0Universe 24 22Magi 3389 supports a single IP, network segment scanning format supports: 192.168.0.0Universe 24 192.168.0.024 the ports to be scanned are separated by commas using grep filter results grep-E'22 | 3389''''import sysimport nmapfrom IPy import IPif len (sys.argv)! = 3: print ("Parameter error, supported format: single IP Network segment in suffix or mask form ") exit (1) scan_ip = IP (sys.argv [1]) scan_port = sys.argv [2] # scan_ip.prefixlen () # transfer the network segment to the suffix form scan_ip =" {} ".format (scan_ip) for port in [int (I) for i in scan_port.split (", ")]: if port
< 1 or port >65535: print ("Port range 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 # print ("{}) {}, {} ".format (host, mac,", ".join (ports_list)) print (" {}, {}, {} ".format (host, mac,", ".join (ports_list)