Wednesday, February 29, 2012

Python - Simple Port Scanner

Description: Simple Port Scanner with Python.

#!/usr/bin/env python

# http://kuntoaji.blogspot.com

import sys
import socket
import threading

class Scanner(threading.Thread):
 def __init__(self, host, port):
  threading.Thread.__init__(self)
  self.host = host
  self.port = port
  self.status = ""

 def run(self):
  self.sk = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  self.sk.settimeout(0.03)
  try:
   self.sk.connect((self.host, self.port))
  except:
   pass
  else:
   self.status = "open"
   self.sk.close()

def error():
 print "Usage: python portScanner.py [target] [start port] [end port]"

def welcomeMsg():
 print "---------------------------------------------"
 print " portScanner.py - A Simple Port Scanner Tool"
 print " http://kuntoaji.blogspot.com"
 print "---------------------------------------------"

if (__name__ == "__main__"):
 if (len(sys.argv) != 4):
  error()
 else:
  welcomeMsg()

  target = sys.argv[1]
  startPort = int(sys.argv[2])
  endPort = int(sys.argv[3])
  threads = []
  for port in range(startPort, endPort):
   thread = Scanner(target, port)
   threads.append(thread)
   thread.start()

  print "Target = ", target

  for thread in threads:
   if (thread.status == "open"):
    print "Port", thread.port, " : ", thread.status
Artikel Terkait

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...