Wednesday, December 11, 2013

Python & Scapy - Simple port scanner

I have been learning more about python and taking some courses with the Pentester Academy.  One of the projects is to create a simple port scanner using scapy.  The next iteration will incorporate threading.  Hopefully you find this interesting and see the value of Python and Scapy!  Comments, suggestions, observations encouraged. Enjoy!


# Import necessary modules
import logging
from scapy.all import *
import itertools
import thread

# Parse and create IP range
def ip_range(input_string):
    octets = input_string.split('.')
    chunks = [map(int, octet.split('-')) for octet in octets]
    ranges = [range(c[0], c[1] + 1) if len(c) == 2 else c for c in chunks]
    for address in itertools.product(*ranges):
        yield '.'.join(map(str, address))

# Scan each IP address with the identified port number
def scanner(ips):
    for i in ip_range(ips):
        src_port = RandShort()
        dst_port = port
        scan = sr1(IP(dst=i)/TCP(sport=src_port,dport=dst_port,flags="S"),timeout=10)
        if scan is None:
            print "This port is closed on IP: " + i
                print "This port is open for IP: " + i
            print "Unknown state"

# Request port number from user
port = int(raw_input('Enter which port to scan --> '))

# Request IP range from user - form should follow this format ''
ips = raw_input('Enter your range using this format x.x.x.x-x --> ')



  1. Hi, this is an outstanding post!! really very informative information you have provided us about port scanner.Here is another website which also gives great info about this.If you want visit : subnet calculator

  2. I am glad you found this interesting and helpful. I try to share what I've learned from others.

  3. Really good information to show through this blog. I really appreciate you for all the valuable information that you are providing us through your blog. python Online course