#!/usr/bin/python

import socket
import sys
import argparse

def main():
	parser = argparse.ArgumentParser(description = "Test a specified IP for open ports.")
	mutex = parser.add_mutually_exclusive_group()
	parser.add_argument('ip', metavar='IP', help='The ip to be scanned for open ports')
	parser.add_argument('-v', '--verbose', help='Add extra verbosity to the output of the scanner', action='store_true')
	parser.add_argument('-t', '--type', help='The type of the ports to be scanned', choices=['tcp', 'TCP', 'udp', 'UDP'], metavar='[ udp | tcp ]', default='tcp')
	mutex.add_argument('-a', '--all', help='Scan all the possible ports', action='store_true')
	mutex.add_argument('-p', '--ports', help='Scan the specified ports only', type=int, metavar='PORT', choices=range(0,65536), nargs='*', default=[])

	args = parser.parse_args()

	ports = []
	if args.all:
		ports = range(0, 65536)
	elif args.ports:
		ports = args.ports
    
	ip = socket.gethostbyname(args.ip)
	try:
		host = socket.gethostbyaddr(ip)[0]
	except socket.herror:
		host = 'Uknown Host'
	protocol = args.type

	for port in ports:		    # For every given port attempt to connect...
		if (args.type == 'tcp' or args.type == 'TCP'):
			s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
		elif (args.type == 'udp' or args.type == 'UDP'):
			s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
		banner = False
		s.settimeout(3)
		try:
			serv = socket.getservbyport(port)
		except socket.error:
			serv = 'Uknown Service'
		try:
			if args.verbose:
				print '--> Attempting to connect to ' + ip + '(' + host + ')' + ':' + str(port) + '/' + protocol.upper() + ' [' + serv.upper() + ']'
			s.connect((ip ,int(port)))
			s.send("Port Checking")
			try:
				banner = s.recv(1024)
			except socket.timeout:
				banner = ''
		except socket.error:	    # If a socket.error exception is caught, it means the attempt to connect has failed, 
			continue		    # hence the port is closed... In that case advance to the next port.
		if banner=='':
			banner = 'No Response...'
		print '[+] Port ' + str(port) + '/' + protocol.upper() + ' [' + serv.upper() + ']' + ' is open!' + '  ==> Reply: ' + str(banner)
		s.close()

if __name__ =='__main__':
	main()

@@ -37,16 +37,26 @@
 ort sys%0A
+import os%0A
 import a
@@ -142,18 +142,23 @@
 ecified 
-IP
+ip/host
  for ope
@@ -213,16 +213,29 @@
 e_group(
+required=True
 )%0A%09parse
@@ -281,16 +281,21 @@
 ='The ip
+/host
  to be s
@@ -961,16 +961,23 @@
 ts%0A    %0A
+%09try:%0A%09
 %09ip = so
@@ -1004,16 +1004,75 @@
 rgs.ip)%0A
+%09except socket.gaierror:%0A%09%09sys.exit('Invalid ip address!')%0A
 %09try:%0A%09%09
@@ -1152,16 +1152,16 @@
 n Host'%0A
-
 %09protoco
@@ -1175,16 +1175,79 @@
 s.type%0A%0A
+%09print ''%0A%09ping_response = os.system('ping -q -c1 -w2 ' + ip)%0A%0A
 %09for por

Resolve #5