diff --git a/MP1/client.py b/MP1/client.py index f462612..c37f832 100644 --- a/MP1/client.py +++ b/MP1/client.py @@ -1,7 +1,7 @@ -# client.py +#!/usr/bin/env python3 import socket -PORT = 5000 +PORT = 5055 ALL_IPS = [ "172.22.156.3", "172.22.158.2", @@ -15,19 +15,20 @@ ALL_IPS = [ "172.22.156.6" ] -def grep_all_ips(ips): +def grep_all_ips(ips, cmd): for ip in ips: try: client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) client_socket.connect((ip, PORT)) print(f"Connected to server at {ip}:{PORT}") - client_socket.sendall(b"Hello from client!") - data = client_socket.recv(1024) + client_socket.sendall(cmd.encode()) + data = client_socket.recv(65536) print("Received:", data.decode()) except Exception as e: print(f"Failed to connect to {ip}:{PORT} - {e}") finally: client_socket.close() -grep_all_ips(ALL_IPS) \ No newline at end of file +cmd = input("Enter the command to execute on all servers: ") +grep_all_ips(ALL_IPS, cmd) \ No newline at end of file diff --git a/MP1/scripts/install_cmd.sh b/MP1/scripts/install_cmd.sh new file mode 100644 index 0000000..34a5f30 --- /dev/null +++ b/MP1/scripts/install_cmd.sh @@ -0,0 +1,6 @@ +chmod +x ../client.py +mkdir -p ~/bin +cp ../client.py ~/bin/grepp +chmod +x ~/bin/grepp +export PATH="$HOME/bin:$PATH" > ~/.bashrc +source ~/.bashrc \ No newline at end of file diff --git a/MP1/server.py b/MP1/server.py index 7ec1143..c2a0c2e 100644 --- a/MP1/server.py +++ b/MP1/server.py @@ -1,25 +1,28 @@ # server.py import socket +import shlex +import subprocess HOST = "0.0.0.0" -PORT = 5000 +PORT = 5055 server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) server_socket.bind((HOST, PORT)) server_socket.listen() -print(f"Server listening on {HOST}:{PORT}") +# print(f"Server listening on {HOST}:{PORT}") while True: conn, addr = server_socket.accept() - print(f"Connected by {addr}") + # print(f"Connected by {addr}") - data = conn.recv(1024) - print("Received:", data.decode()) + command = conn.recv(65536) + # print("Received Grep Command:", command.decode()) + + result = subprocess.run(shlex.split(command.decode()), capture_output=True, text=True) + conn.sendall(result.stdout.encode()) - conn.sendall(b"Hello from server!") - conn.close() server_socket.close() \ No newline at end of file