mirror of
https://github.com/wickedyoda/public-setupfiles.git
synced 2026-11-16 13:31:56 -06:00
commit
1e25e36ce6
195 files changed
+152301
No files matched your search
@@ -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()
|
||||
Reference in new issue
Block a user