Added summary block + filename, match count, and line number in output
This commit is contained in:
1 parent
fdb0532e44
commit
a022c3cba5
2 files changed
+42
-10
No files matched your search
+28
-7
@@ -16,19 +16,40 @@ ALL_IPS = [
|
||||
]
|
||||
|
||||
def grep_all_ips(ips, cmd):
|
||||
for ip in ips:
|
||||
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}")
|
||||
# print(f"Connected to server at {ip}:{PORT}")
|
||||
|
||||
full_cmd = cmd + f" ./machine.{i + 1}.log"
|
||||
|
||||
client_socket.sendall(full_cmd.encode())
|
||||
data = client_socket.recv(65536).decode()
|
||||
|
||||
count = len(data.splitlines())
|
||||
match_count.append(count)
|
||||
if count > 0:
|
||||
print(f"Filename: machine.{i + 1}.log")
|
||||
print(f"Matches: {count}")
|
||||
print(data)
|
||||
|
||||
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()
|
||||
|
||||
return match_count
|
||||
|
||||
cmd = input("Enter the command to execute on all servers: ")
|
||||
grep_all_ips(ALL_IPS, cmd)
|
||||
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("===============================================================")
|
||||
Reference in new issue
Block a user