using shlex on the client
This commit is contained in:
1 parent
e1fbb3b33e
commit
fd11784744
2 files changed
+10
-11
No files matched your search
+5
-5
@@ -2,6 +2,7 @@
|
|||||||
import socket
|
import socket
|
||||||
import sys
|
import sys
|
||||||
import time
|
import time
|
||||||
|
import shlex
|
||||||
|
|
||||||
PORT = 5056
|
PORT = 5056
|
||||||
ALL_IPS = [
|
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 = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||||
client_socket.connect((ip, PORT))
|
client_socket.connect((ip, PORT))
|
||||||
|
|
||||||
if if_unit_test:
|
filename = f"machine.{i + 1}.test.log" if if_unit_test else f"machine.{i + 1}.log"
|
||||||
full_cmd = cmd + f" ./machine.{i + 1}.test.log"
|
|
||||||
else:
|
full_cmd = shlex.join(cmd + [filename])
|
||||||
full_cmd = cmd + f" ./machine.{i + 1}.log"
|
|
||||||
|
|
||||||
send_message(client_socket, full_cmd)
|
send_message(client_socket, full_cmd)
|
||||||
data = receive_message(client_socket)
|
data = receive_message(client_socket)
|
||||||
@@ -74,7 +74,7 @@ def grep_all_ips(ips, cmd, if_unit_test):
|
|||||||
|
|
||||||
#main
|
#main
|
||||||
#if unit test flag exists, then use the test log files instead of the actual log files
|
#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
|
#track time
|
||||||
start_time = time.perf_counter()
|
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)
|
match_count = grep_all_ips(ALL_IPS, cmd, if_unit_test=[x for x in sys.argv].count("-t") > 0)
|
||||||
|
|||||||
+5
-6
@@ -44,6 +44,7 @@ while True:
|
|||||||
conn, addr = server_socket.accept()
|
conn, addr = server_socket.accept()
|
||||||
print(f"Connected by {addr}")
|
print(f"Connected by {addr}")
|
||||||
|
|
||||||
|
try:
|
||||||
command = receive_message(conn)
|
command = receive_message(conn)
|
||||||
if command is None:
|
if command is None:
|
||||||
conn.close()
|
conn.close()
|
||||||
@@ -51,18 +52,16 @@ while True:
|
|||||||
|
|
||||||
command_args = shlex.split(command)
|
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")
|
command_args.insert(1, "-n")
|
||||||
|
|
||||||
result = subprocess.run(command_args, cwd=SCRIPT_DIR, capture_output=True, text=True)
|
result = subprocess.run(command_args, cwd=SCRIPT_DIR, capture_output=True, text=True)
|
||||||
|
|
||||||
send_message(conn, result.stdout + result.stderr)
|
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()
|
conn.close()
|
||||||
|
|
||||||
server_socket.close()
|
server_socket.close()
|
||||||
Reference in new issue
Block a user