Synchronous scanning

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 ...

Get Mastering Python for Networking and Security now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.