Skip to content

Instantly share code, notes, and snippets.

@KhasMek
Created June 7, 2024 23:22
Show Gist options
  • Save KhasMek/95c7f8478cc48c4a6bbbfb98430d9d87 to your computer and use it in GitHub Desktop.
Save KhasMek/95c7f8478cc48c4a6bbbfb98430d9d87 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
#
# Parse the output file form generate_port_targets.py and test if the listed
# ports are open or closed.
# TODO:
# Filter unique ports and sort ports numerically
import csv
import select
import signal
import socket
import string
import sys
import time
def parse_csv():
with open('port_targets.csv', 'rt') as csvfile:
_reader = csv.reader(csvfile)
for row in _reader:
_host = row[0]
row.pop(0)
_ports = row
check_port(_host, _ports)
def check_port(_host, _ports):
_output = open('port_test_results', 'a+')
print(_host)
_output.write(_host + '\n')
for _port in _ports:
_port = int(_port)
_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
_socket.settimeout(25)
try :
_socket.connect((_host, _port))
yeh = str(' [*] Port: ' + str(_port))
print(yeh)
_output.write(yeh + '\n')
_socket.close()
except :
neh = str(' [ ] Port: ' + str(_port))
print(neh)
_output.write(neh + '\n')
pass
# uncomment to (kinda) account for load ballancing
time.sleep(30)
def signal_handler(signal, frame):
print "[-] Ctrl-C received! Killing..."
os._exit(0)
signal.signal(signal.SIGINT, signal_handler)
if __name__ == '__main__':
parse_csv()%
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment