Home | About Me | Photos | Writing | Research | Scratchpad | Projects

portscan


#!/usr/bin/env python
import sys, socket

def checkPortTCP(host, port):
     sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
     try:
             sock.connect((host, port))
             return 1
     except socket.error, reason:
             return 0

def main():
     hostname = raw_input("Please enter an IP or hostname: ")
     lowIP = int(raw_input("Please enter the start port number: "))
     highIP = int(raw_input("Please enter the end port number: "))

     port_range = range(lowIP, highIP + 1)
     open_ports = []
     sys.stdout.write("Port: ")
     sys.stdout.flush()
     for port in port_range:
             sys.stdout.write(str(port))
             sys.stdout.flush()
             if checkPortTCP(hostname, port):
                     open_ports.append(port)
             sys.stdout.write("\b" * len(str(port)))
             sys.stdout.flush()

     sys.stdout.write("Open ports: " + str(open_ports) + "")        sys.stdout.flush()

if __name__ == "__main__":
     main()