#!/usr/bin/env python3 from socket import * from time import sleep from _thread import start_new_thread from subprocess import getoutput from scolor import * from sys import argv, exit # Stuff we need to know to run this: # 1. Which ports to monitor? # 2. How would the user like the data saved? (keep this simple) # 3. Should we support more that one connection to a port at once? # Usage: this_script.py lowest_port highest_port if(len(argv) < 3): print("Usage: " + argv[0] + " lowest_port highest_port") exit(1) lowest_port = int(argv[1]) highest_port = int(argv[2]) # for every port we're going to monitor # This'll be run once on each port. We'll use a function for this def per_port_monitor(port): our_socket = socket(AF_INET, SOCK_STREAM) our_socket.bind(("0.0.0.0", port)) our_socket.listen(5) while True: client_socket, client_address = our_socket.accept() print(dgreen(getoutput("date"))) # Only Linux,Mac,FreeBSD, etc. -specific line print(green("We got a new connection from: "), client_address[0], client_address[1], "port ", port) while True: try: data = client_socket.recv(1024) if(len(data) == 0): break print(blue(port), ": ", data) except: break print(dgreen(getoutput("date"))) # Only Linux,Mac,FreeBSD, etc. -specific line print(red("Connection closed"), client_address, "port", port); client_socket.close() our_socket.close() # for each port to monitor for port in range(lowest_port, highest_port): start_new_thread(per_port_monitor, (port, )) while True: sleep(60)