79 lines
2.2 KiB
Python
79 lines
2.2 KiB
Python
#!/usr/bin/env python3
|
|
import socket
|
|
|
|
PORT = 5055
|
|
ALL_IPS = [
|
|
"172.22.156.3",
|
|
"172.22.158.2",
|
|
"172.22.94.2",
|
|
"172.22.156.4",
|
|
"172.22.158.3",
|
|
"172.22.94.3",
|
|
"172.22.156.5",
|
|
"172.22.158.4",
|
|
"172.22.94.4",
|
|
"172.22.156.6"
|
|
]
|
|
|
|
def send_message(sock, message):
|
|
e_data = message.encode('utf-8')
|
|
#header for the message length
|
|
header = str(len(e_data)).zfill(10).encode('utf-8')
|
|
sock.sendall(header)
|
|
sock.sendall(e_data)
|
|
|
|
def receive_exact(sock, num_bytes):
|
|
received_data = b""
|
|
while len(received_data) < num_bytes:
|
|
chunk = sock.recv(num_bytes - len(received_data))
|
|
if not chunk:
|
|
break
|
|
received_data += chunk
|
|
return received_data
|
|
|
|
def receive_message(sock):
|
|
header = receive_exact(sock, 10)
|
|
if not header:
|
|
return None
|
|
message_length = int(header.decode('utf-8'))
|
|
received_data = receive_exact(sock, message_length)
|
|
return received_data.decode('utf-8')
|
|
|
|
def grep_all_ips(ips, cmd):
|
|
match_count = []
|
|
|
|
for i, ip in enumerate(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}")
|
|
|
|
full_cmd = cmd + f" ./machine.{i + 1}.log"
|
|
|
|
send_message(client_socket, full_cmd)
|
|
data = receive_message(client_socket)
|
|
|
|
count = len(data.splitlines())
|
|
match_count.append(count)
|
|
if count > 0:
|
|
print(f"Filename: machine.{i + 1}.log")
|
|
print(f"Matches: {count}")
|
|
print(data)
|
|
|
|
except Exception as e:
|
|
print(f"Failed to connect to {ip}:{PORT} - {e}")
|
|
finally:
|
|
client_socket.close()
|
|
|
|
return match_count
|
|
|
|
while True:
|
|
cmd = input(">> ").strip()
|
|
match_count = grep_all_ips(ALL_IPS, cmd)
|
|
|
|
print("=========================== SUMMARY ===========================")
|
|
for i, count in enumerate(match_count):
|
|
if count > 0:
|
|
print(f"machine.{i + 1}.log: {count} matches")
|
|
print(f"Total matches: {sum(match_count)}")
|
|
print("===============================================================") |