Port-scanning with scapy

In the following example, we see that we have defined a analyze_port() function that has as parameters the host and port to analyze.

You can find the following code in the port_scan_scapy.py file:

from scapy.all import sr1, IP, TCPOPEN_PORTS = []def analyze_port(host, port): """ Function that determines the status of a port: Open / closed :param host: target :param port: port to test :type port: int """  print "[ii] Scanning port %s" % port res = sr1(IP(dst=host)/TCP(dport=port), verbose=False, timeout=0.2) if res is not None and TCP in res:     if res[TCP].flags == 18:         OPEN_PORTS.append(port)         print "Port %s open" % portdef main(): for x in xrange(0, 80):     analyze_port("domain", x) print "[*] Open ports:" for x in OPEN_PORTS: ...

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.