From 914ce22e5aefe759cdc3b46b751dcecf50a7cacc Mon Sep 17 00:00:00 2001 From: Aryan Singh Date: Sun, 13 Sep 2026 15:33:41 -0500 Subject: [PATCH 1/4] change to 5056 --- MP1/client.py | 8 ++++++-- MP1/scripts/kill_mp.sh | 4 ++-- MP1/server.py | 2 +- MP1/test.py | 4 ++-- 4 files changed, 11 insertions(+), 7 deletions(-) diff --git a/MP1/client.py b/MP1/client.py index 1c5f14a..9f6a658 100644 --- a/MP1/client.py +++ b/MP1/client.py @@ -2,7 +2,7 @@ import socket import sys -PORT = 5055 +PORT = 5056 ALL_IPS = [ "172.22.156.3", "172.22.158.2", @@ -74,7 +74,10 @@ def grep_all_ips(ips, cmd, if_unit_test): #main #if unit test flag exists, then use the test log files instead of the actual log files cmd = "grep " + " ".join([arg for arg in sys.argv[1:] if arg != "-t"]) +#track time +start_time = time.perf_counter() match_count = grep_all_ips(ALL_IPS, cmd, if_unit_test=[x for x in sys.argv].count("-t") > 0) +end_time = time.perf_counter() print("=========================== SUMMARY ===========================") for i, count in enumerate(match_count): @@ -89,4 +92,5 @@ for i, count in enumerate(match_count): else: print(f"machine.{i + 1}.log: {count} matches") print(f"Total matches: {max(0, sum(match_count))}") -print("===============================================================") \ No newline at end of file +print("===============================================================") +print(f"Time taken: {end_time - start_time:.2f} seconds") diff --git a/MP1/scripts/kill_mp.sh b/MP1/scripts/kill_mp.sh index b93e9e0..92959e2 100755 --- a/MP1/scripts/kill_mp.sh +++ b/MP1/scripts/kill_mp.sh @@ -3,13 +3,13 @@ if [[ "$1" == "all" ]]; then for i in {1..10}; do echo "Killing server$i..." - ssh -o BatchMode=yes "server$i" "fuser -k -9 5055/tcp" + ssh -o BatchMode=yes "server$i" "fuser -k -9 5056/tcp" echo "" done else for i in "$@"; do echo "Killing server$i..." - ssh -o BatchMode=yes "server$i" "fuser -k -9 5055/tcp" + ssh -o BatchMode=yes "server$i" "fuser -k -9 5056/tcp" echo "" done fi \ No newline at end of file diff --git a/MP1/server.py b/MP1/server.py index b3893f5..aee259e 100644 --- a/MP1/server.py +++ b/MP1/server.py @@ -5,7 +5,7 @@ import subprocess from pathlib import Path HOST = "0.0.0.0" -PORT = 5055 +PORT = 5056 server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) diff --git a/MP1/test.py b/MP1/test.py index 8e5cb10..97e5640 100644 --- a/MP1/test.py +++ b/MP1/test.py @@ -304,9 +304,9 @@ print(f"Running grep command on a {machine_num} random machine...") for i in tests: result = subprocess.run(["ssh", f"asing271@{ALL_IPS[machine_num - 1]}", f"cd ~/fa26_cs425_.group-mp_.team-01/MP1 && grepp {i} -t"], capture_output=True, text=True) if result.stdout.strip() == results[tests.index(i)].strip(): - print("PASS {i} test") + print(f"PASS {i} test") else: - print("FAIL {i} test") + print(f"FAIL {i} test") #delete log files on all the machines after running this test print("Deleting log files on all machines...") From e1fbb3b33ea6eaac52c9d1872b4fe51c5df43afd Mon Sep 17 00:00:00 2001 From: Aryan Singh Date: Sun, 13 Sep 2026 15:38:54 -0500 Subject: [PATCH 2/4] Update client.py --- MP1/client.py | 1 + 1 file changed, 1 insertion(+) diff --git a/MP1/client.py b/MP1/client.py index 9f6a658..3f3298e 100644 --- a/MP1/client.py +++ b/MP1/client.py @@ -1,6 +1,7 @@ #!/usr/bin/env python3 import socket import sys +import time PORT = 5056 ALL_IPS = [ From fd117847448ae34a1e70949081181c010ea4c620 Mon Sep 17 00:00:00 2001 From: Aryan Singh Date: Sun, 13 Sep 2026 16:06:07 -0500 Subject: [PATCH 3/4] using shlex on the client --- MP1/client.py | 10 +++++----- MP1/server.py | 37 ++++++++++++++++++------------------- 2 files changed, 23 insertions(+), 24 deletions(-) diff --git a/MP1/client.py b/MP1/client.py index 3f3298e..0f12fc3 100644 --- a/MP1/client.py +++ b/MP1/client.py @@ -2,6 +2,7 @@ import socket import sys import time +import shlex PORT = 5056 ALL_IPS = [ @@ -49,10 +50,9 @@ def grep_all_ips(ips, cmd, if_unit_test): client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) client_socket.connect((ip, PORT)) - if if_unit_test: - full_cmd = cmd + f" ./machine.{i + 1}.test.log" - else: - full_cmd = cmd + f" ./machine.{i + 1}.log" + filename = f"machine.{i + 1}.test.log" if if_unit_test else f"machine.{i + 1}.log" + + full_cmd = shlex.join(cmd + [filename]) send_message(client_socket, full_cmd) data = receive_message(client_socket) @@ -74,7 +74,7 @@ def grep_all_ips(ips, cmd, if_unit_test): #main #if unit test flag exists, then use the test log files instead of the actual log files -cmd = "grep " + " ".join([arg for arg in sys.argv[1:] if arg != "-t"]) +cmd = ["grep ", *[arg for arg in sys.argv[1:] if arg != "-t"]] #track time start_time = time.perf_counter() match_count = grep_all_ips(ALL_IPS, cmd, if_unit_test=[x for x in sys.argv].count("-t") > 0) diff --git a/MP1/server.py b/MP1/server.py index aee259e..7810f75 100644 --- a/MP1/server.py +++ b/MP1/server.py @@ -44,25 +44,24 @@ while True: conn, addr = server_socket.accept() print(f"Connected by {addr}") - command = receive_message(conn) - if command is None: + try: + command = receive_message(conn) + if command is None: + conn.close() + continue + + command_args = shlex.split(command) + + command_args.insert(1, "-n") + + result = subprocess.run(command_args, cwd=SCRIPT_DIR, capture_output=True, text=True) + + send_message(conn, result.stdout + result.stderr) + + except Exception as e: + error_message = f"Error executing command: {e}" + send_message(conn, error_message) + finally: conn.close() - continue - - command_args = shlex.split(command) - - if not command_args or command_args[0] != "grep": - response = "Invalid command. Please use the 'grep' command." - send_message(conn, response) - conn.close() - continue - - command_args.insert(1, "-n") - - result = subprocess.run(command_args, cwd=SCRIPT_DIR, capture_output=True, text=True) - - send_message(conn, result.stdout + result.stderr) - - conn.close() server_socket.close() From 99ca593c06ebf8b3d7e0f67cf18ddfcdeba717be Mon Sep 17 00:00:00 2001 From: Aryan Singh Date: Sun, 13 Sep 2026 16:14:49 -0500 Subject: [PATCH 4/4] minor fix --- MP1/client.py | 8 +++++--- MP1/server.py | 3 +-- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/MP1/client.py b/MP1/client.py index 0f12fc3..e1cee3c 100644 --- a/MP1/client.py +++ b/MP1/client.py @@ -74,10 +74,11 @@ def grep_all_ips(ips, cmd, if_unit_test): #main #if unit test flag exists, then use the test log files instead of the actual log files -cmd = ["grep ", *[arg for arg in sys.argv[1:] if arg != "-t"]] +cmd = ["grep", *[arg for arg in sys.argv[1:] if arg != "-t"]] #track time start_time = time.perf_counter() -match_count = grep_all_ips(ALL_IPS, cmd, if_unit_test=[x for x in sys.argv].count("-t") > 0) +is_unit_test = "-t" in sys.argv +match_count = grep_all_ips(ALL_IPS, cmd, if_unit_test=is_unit_test) end_time = time.perf_counter() print("=========================== SUMMARY ===========================") @@ -92,6 +93,7 @@ for i, count in enumerate(match_count): print(f"machine.{i + 1}.test.log: {count} matches") else: print(f"machine.{i + 1}.log: {count} matches") -print(f"Total matches: {max(0, sum(match_count))}") +total_matches = sum(count for count in match_count if count > 0) +print(f"Total matches: {total_matches}") print("===============================================================") print(f"Time taken: {end_time - start_time:.2f} seconds") diff --git a/MP1/server.py b/MP1/server.py index 7810f75..1e51567 100644 --- a/MP1/server.py +++ b/MP1/server.py @@ -59,8 +59,7 @@ while True: send_message(conn, result.stdout + result.stderr) except Exception as e: - error_message = f"Error executing command: {e}" - send_message(conn, error_message) + None finally: conn.close()