Last active
January 8, 2017 19:54
-
-
Save BlackVikingPro/4bf6fa29dbb362adfde2eca5ba071fa3 to your computer and use it in GitHub Desktop.
Simple Python Port Scanner
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import os, sys, socket | |
def usage(): | |
print "\n\nOption Switches:\nPlease define '--verbose' before you define '--custom', should you define either one.\n" | |
print "--verbose -- Will show all connection attempts. Even failed ones." | |
print "\nExample: python %s 127.0.0.1 --verbose" % sys.argv[0] | |
pass | |
try: | |
hostname = sys.argv[1] | |
pass | |
except IndexError as e: | |
if sys.argv[0].startswith('./'): | |
print "%s <hostname>" % sys.argv[0] | |
usage() | |
sys.exit() | |
else: | |
print "./%s <hostname>" % sys.argv[0] | |
usage() | |
sys.exit() | |
pass | |
# ping common ports | |
for port in range(1, 65535): | |
try: | |
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # create/define a tcp socket | |
sock.connect((hostname, port)) | |
server = (hostname, port) | |
print "Host: %s is \033[92mopen\033[0m to \033[92mport %s!\033[0m" % server | |
sock.close() | |
except socket.error as e: | |
server = (hostname, port) | |
if "--verbose" in sys.argv: | |
print "Host: %s is \033[91mnot\033[0m open to \033[91mport %s!\033[0m" % server | |
else: | |
pass | |
except KeyboardInterrupt as k: | |
print "Closing connection cleanly..." | |
sock.close() | |
sys.exit() | |
pass | |
pass | |
sock.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment