59 lines
1.8 KiB
Python
59 lines
1.8 KiB
Python
import paramiko
|
|
import os
|
|
|
|
SERVER_HOST = "172.17.0.253"
|
|
SERVER_USER = "itguys"
|
|
PASSWORD = "vR7Ag$Pk"
|
|
|
|
def restore_nginx():
|
|
client = paramiko.SSHClient()
|
|
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
|
|
client.connect(SERVER_HOST, username=SERVER_USER, password=PASSWORD)
|
|
|
|
local_file = "producao/nginx/nginx.conf"
|
|
remote_tmp = "/tmp/nginx.conf"
|
|
dest_file = "/etc/nginx/nginx.conf"
|
|
|
|
# 1. Upload to /tmp
|
|
print(f"[*] Uploading {local_file} -> {remote_tmp}")
|
|
sftp = client.open_sftp()
|
|
sftp.put(local_file, remote_tmp)
|
|
sftp.close()
|
|
|
|
# 2. Check File Difference (Optional but good for log)
|
|
cmd = f"echo '{PASSWORD}' | sudo -S diff {remote_tmp} {dest_file}"
|
|
stdin, stdout, stderr = client.exec_command(cmd)
|
|
# print(stdout.read().decode()) # Might be large
|
|
|
|
# 3. Move to /etc/nginx/
|
|
print(f"[*] Overwriting {dest_file}...")
|
|
cmd = f"echo '{PASSWORD}' | sudo -S cp {remote_tmp} {dest_file}"
|
|
stdin, stdout, stderr = client.exec_command(cmd)
|
|
print(stdout.read().decode())
|
|
print(stderr.read().decode())
|
|
|
|
# 4. Test Config
|
|
print("[*] Testing Nginx Config...")
|
|
cmd = f"echo '{PASSWORD}' | sudo -S nginx -t"
|
|
stdin, stdout, stderr = client.exec_command(cmd)
|
|
out = stdout.read().decode()
|
|
err = stderr.read().decode()
|
|
print(out)
|
|
print(err)
|
|
|
|
if "successful" in err or "successful" in out:
|
|
# 5. Restart
|
|
print("[*] Restarting Nginx...")
|
|
cmd = f"echo '{PASSWORD}' | sudo -S systemctl restart nginx"
|
|
stdin, stdout, stderr = client.exec_command(cmd)
|
|
print(stdout.read().decode())
|
|
print(stderr.read().decode())
|
|
print("[*] DONE. Service should be UP.")
|
|
else:
|
|
print("[!] Config Test Failed. Not restarting.")
|
|
|
|
client.close()
|
|
|
|
if __name__ == "__main__":
|
|
restore_nginx()
|