python

python27.zip

#!/usr/bin/env python

import sys, os
from optparse import OptionParser
import socket

VERSION="1.0.0 (2015-06-21)"

# auto-detect basedir
old_pwd = os.getcwd()
os.chdir(os.path.join(os.path.dirname(sys.argv[0]), ".."))
BASEDIR = os.getcwd()
os.chdir(old_pwd)



def usage():
#   print "1        10        20        30        40        50        60        70       80"
#   print "|...'....|....'....|....'....|....'....|....'....|....'....|....'....|....'....|"
	print "Usage: %s [OPTIONS] [HOST]" % sys.argv[0]
	print ""
	print "OPTIONS:"
	print "    -h          --help            Show this help"
	print "                --version         Display version information"
	print "    -v          --verbose         Show verbose output"
	print "    -q          --quick           Scan common ports only"
	print ""
	print "EXAMPLES:"
	print "    %s -q hostname     Start quick portscan on hostname" % sys.argv[0]
	print ""



def scan_system(remoteServer, scanCommonPorts):
	common_ports = [22, 80, 139, 161, 389, 443, 445, 631, 636, 8000, 8080, 8443, 9000, 9001, 9002, 9003]
	all_ports = range(1,1025)

	try:
		remoteServerIP = socket.gethostbyname(remoteServer)
		if(scanCommonPorts):
			scan_ports = common_ports
		else:
			scan_ports = all_ports

		print "scanning %s (%s) common_ports = %s" % (remoteServer, remoteServerIP, scanCommonPorts)
		for port in scan_ports:
			sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
			result = sock.connect_ex((remoteServerIP, port))
			if result == 0:
				print "Port {}: 	 Open".format(port)
			sock.close()

	except KeyboardInterrupt:
		print "You pressed Ctrl+C"
		sys.exit()

	except socket.gaierror:
		print 'Hostname could not be resolved. Exiting'
		sys.exit()

	except socket.error:
		print "Couldn't connect to server"
		sys.exit()


def main(argv):
	parser = OptionParser(usage="%prog [OPTIONS]", version="%prog, Version "+VERSION)
	parser.remove_option("-h")
	parser.remove_option("--version")
	parser.add_option("-h", "--help", dest="help", action="store_true")
	parser.add_option("--version", action="version")
	parser.add_option("-v", "--verbose", dest="verbose", action="store_true")
	parser.add_option("-q", "--quick", action="store_true", dest="common_ports", default=False)

	(options, args) = parser.parse_args(argv)

	args.extend([""])

	system = args[0]

	if options.help or system == "":
		usage()
		sys.exit()

	scan_system(system, options.common_ports)
	sys.exit(0)

#
# MAIN
#
if __name__ == "__main__":
	main(sys.argv[1:])
  • python.txt
  • Zuletzt geändert: 16.11.2016 23:18 (vor 8 Jahren)
  • von 127.0.0.1