3 files changed
+23
-13
No files matched your search
+7
-6
@@ -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)
|
||||
cmd = input("Enter the command to execute on all servers: ")
|
||||
grep_all_ips(ALL_IPS, cmd)
|
||||
@@ -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
|
||||
+10
-7
@@ -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()
|
||||
Reference in new issue
Block a user