Skip to content
Snippets Groups Projects
Commit 91d259f0 authored by anhndd's avatar anhndd
Browse files

first commit

parents
Branches
No related tags found
No related merge requests found
import socket
import time, sys, queue
import threading
import subprocess
# Configuration
IP_server = "192.168.123.1"
LISTEN_PORT = 12346 # Specify the port number to listen on
ip_queue = queue.Queue()
array_point_save = []
num_machine = 0
map_point = {}
OUTPUT_FILE = "measurement.txt"
def read_and_write_points():
global ip_queue, num_machine
while True:
try:
time_ip = ip_queue.get()
if time_ip[1] not in map_point:
map_point[time_ip[1]] = time_ip[0]
array_point_save.append(time_ip)
with open(OUTPUT_FILE, "w") as file:
file.write(str(array_point_save))
print("saved", array_point_save)
len_actual = len(array_point_save)
print(len_actual, "machine infected")
if num_machine == len_actual:
command = "nc -q 5 " + IP_server +" 12345 < measurement.txt"
print("run", command)
try:
subprocess.run(command, shell=True, check=True)
print("done")
except subprocess.CalledProcessError as e:
print(f"Error executing command: {e}")
else:
print("duplicate ", time_ip[1])
#time.sleep(1)
except queue.Empty:
pass
def main():
global ip_queue, num_machine
if len(sys.argv) > 1:
num_machine = int(sys.argv[1])
else:
return
thread = threading.Thread(target=read_and_write_points)
thread.daemon = True
thread.start()
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as server_socket:
server_socket.bind(('0.0.0.0', LISTEN_PORT))
server_socket.listen()
print(f"Listening for incoming connections on port {LISTEN_PORT}...")
while True:
client_socket, client_address = server_socket.accept()
ip_queue.put([time.time(), client_address[0]])
#print(f"Connection {connection_count}: {client_address[0]}")
if __name__ == "__main__":
main()
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment