In this example, we implemented a class that allows us to scan an IP address and a list of ports that are passed to the script as a parameter.
In the main program, we add the necessary configuration for the treatment of the input parameters. We perform a loop that processes each port sent by parameter, and call the nmapScan (ip, port) method of the NmapScanner class.
You can find the following code in the filename: NmapScanner.py:
import optparse, nmapclass NmapScanner: def __init__(self): self.nmsc = nmap.PortScanner() def nmapScan(self, host, port): self.nmsc.scan(host, port) self.state = self.nmsc[host]['tcp'][int(port)]['state'] print " [+] "+ host + " tcp/" + port + " " + self.statedef main(): parser = optparse.OptionParser("usage%prog ...