Last active
January 10, 2019 13:59
-
-
Save kescherCode/74c0ca55a30d14644f07c812bd9cc794 to your computer and use it in GitHub Desktop.
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
# Created by Jeremy Kescher. This file is public domain. Do whatever you want with it. | |
# Tests TCP connections on ports in range minport - maxport. | |
# Odd output with Python 2 but works with both Python 2 and 3. | |
import socket | |
import sys | |
import os | |
# Adjust the min-/maxport if you only want to test for a specific range | |
minport = 1 | |
maxport = 65535 | |
maxport += 1 | |
# Adjust this for a different output path | |
tmppath = os.getenv('tmp', '/tmp/') + '/' | |
allowpath = tmppath + "allowedports.txt" | |
blockpath = tmppath + "blockedports.txt" | |
# s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
# s.settimeout(10) | |
# s.connect(("portquiz.net", 80)) | |
# s.close() | |
# | |
# s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
# s.settimeout(10) | |
# s.connect(("file.htl-donaustadt.at", 445)) | |
# s.close() | |
try: | |
os.remove(allowpath) | |
except OSError: | |
pass | |
try: | |
os.remove(blockpath) | |
except OSError: | |
pass | |
allowfile = open(allowpath, "w+") | |
blockfile = open(blockpath, "w+") | |
print (tmppath) | |
for port in range(minport, maxport): | |
if port != 445: | |
host = "portquiz.net" | |
else: | |
# Yup, that's my school's SMB. | |
# 445 is the only port portquiz.net doesn't support. | |
host = "file.htl-donaustadt.at" | |
try: | |
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
# Adjust this timeout (default: 0.1 == 100ms) if you experience false negatives | |
s.settimeout(0.1) | |
s.connect((host, port)) | |
print("pass", port) | |
allowfile.write(str(port) + " ") | |
# Flushing buffers | |
allowfile.flush() | |
# Making sure the buffer actually gets written to file | |
os.fsync(allowfile) | |
s.close() | |
except: | |
pass; | |
print("fail", port) | |
blockfile.write(str(port) + " ") | |
# Flushing buffers | |
blockfile.flush() | |
# Making sure the buffer actually gets written to file | |
os.fsync(blockfile) | |
allowfile.close() | |
blockfile.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment