diff --git a/system_command_run/Run_command_on_machines.sh b/system_command_run/Run_command_on_machines.sh new file mode 100644 index 0000000..6285036 --- /dev/null +++ b/system_command_run/Run_command_on_machines.sh @@ -0,0 +1,21 @@ +#!/bin/bash + +MACHINE_FILE="machines.txt" +LOG_FILE="ssh_command_log.txt" + +echo -n "Enter command to execute: " +read COMMAND + +echo "Executing on all machines..." +echo "Execution Log - $(date)" > "$LOG_FILE" + +while IFS=" " read -r HOST USER PASS; do + echo "Running on $HOST..." + if [[ -z "$PASS" ]]; then + ssh "$USER@$HOST" "$COMMAND" >> "$LOG_FILE" 2>&1 + else + sshpass -p "$PASS" ssh -o StrictHostKeyChecking=no "$USER@$HOST" "$COMMAND" >> "$LOG_FILE" 2>&1 + fi +done < "$MACHINE_FILE" + +echo "Execution complete. Logs saved in $LOG_FILE." \ No newline at end of file diff --git a/system_command_run/machines.txt b/system_command_run/machines.txt new file mode 100644 index 0000000..0dfc8d8 --- /dev/null +++ b/system_command_run/machines.txt @@ -0,0 +1 @@ +docker1 root \ No newline at end of file diff --git a/system_command_run/run_command_on_machines.py' b/system_command_run/run_command_on_machines.py' new file mode 100644 index 0000000..e69de29 diff --git a/system_command_run/run_command_on_machines_Keys.py' b/system_command_run/run_command_on_machines_Keys.py' new file mode 100644 index 0000000..7a06960 --- /dev/null +++ b/system_command_run/run_command_on_machines_Keys.py' @@ -0,0 +1,30 @@ +def execute_command(hostname, username, command): + ssh = paramiko.SSHClient() + ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) + + try: + ssh.connect(hostname, username=username, key_filename="~/.ssh/id_rsa", timeout=5) + stdin, stdout, stderr = ssh.exec_command(command) + output = stdout.read().decode().strip() + error = stderr.read().decode().strip() + + with open(LOG_FILE, "a") as log: + log.write(f"----- {hostname} ({username}) -----\n") + log.write(f"Command: {command}\n") + log.write(f"Output:\n{output}\n") + if error: + log.write(f"Errors:\n{error}\n") + log.write("\n") + + print(f"[{hostname}] Command executed successfully.") + if error: + print(f"[{hostname}] ERROR: {error}") + + except Exception as e: + print(f"[{hostname}] Connection failed: {e}") + with open(LOG_FILE, "a") as log: + log.write(f"----- {hostname} ({username}) -----\n") + log.write(f"ERROR: {e}\n\n") + + finally: + ssh.close() \ No newline at end of file