Last active
June 5, 2017 17:25
-
-
Save psidex/08aea12b65119544110e91a3a992f4ed to your computer and use it in GitHub Desktop.
This file contains 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
import argparse | |
import socket | |
import sys | |
Version = "1.0" | |
parser = argparse.ArgumentParser() | |
parser.add_argument("host", action="store", help="The host to test") | |
parser.add_argument("-r", action="store", dest="range", help="A range of ports to scan speperated by a -, for example: -r 125-150") | |
parser.add_argument("--version", action="version", version="%(prog)s {}".format(Version)) | |
results = parser.parse_args() | |
popular = [21, 22, 23, 25, 53, 67, 68, 69, 80, 110, 123, 135, 137, 138, 139, | |
143, 161, 162, 179, 389, 443, 445, 636, 989, 990] | |
s = socket.socket() | |
host = results.host | |
p_range = results.range | |
def attempt_connection(inp_host, inp_port): | |
try: | |
s.connect((inp_host, inp_port)) | |
except (ConnectionRefusedError, OSError): | |
print("Port {} : Connection failed".format(inp_port)) | |
else: | |
print("Port {} is open".format(inp_port)) | |
if p_range == None: | |
print("Checking popular ports on {}".format(host)) | |
for port in popular: | |
attempt_connection(host, port) | |
else: | |
try: | |
start, finish = p_range.split("-", 1) | |
int(start) | |
int(finish) | |
if finish <= start: | |
raise ValueError | |
except ValueError: | |
print("range does not contain legal values") | |
else: | |
print("Checking ports {} to {} on host {}".format(start, finish, host)) | |
for port in range(int(start), int(finish)+1): | |
attempt_connection(host, port) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment