check_remote_git.py 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. import os
  2. import sys
  3. import paramiko
  4. from dotenv import load_dotenv
  5. sys.stdout.reconfigure(encoding='utf-8')
  6. sys.stderr.reconfigure(encoding='utf-8')
  7. repo_root = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
  8. load_dotenv(dotenv_path=os.path.join(repo_root, ".env"))
  9. host = os.environ.get('SERVER_HOST')
  10. user = os.environ.get('SERVER_USER')
  11. password = os.environ.get('SERVER_PASS')
  12. if password == "your_db_password_here" or not password:
  13. password = None
  14. ssh = paramiko.SSHClient()
  15. ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
  16. try:
  17. ssh.connect(host, username=user, password=password, timeout=10)
  18. print("SSH Connected!")
  19. # Git log on the server
  20. stdin, stdout, stderr = ssh.exec_command("cd food_project && git log -n 5 --oneline")
  21. print("Git Log on Server:\n", stdout.read().decode('utf-8'))
  22. # Git status on the server
  23. stdin, stdout, stderr = ssh.exec_command("cd food_project && git status")
  24. print("Git Status on Server:\n", stdout.read().decode('utf-8'))
  25. # Git diff on the server
  26. stdin, stdout, stderr = ssh.exec_command("cd food_project && git diff app.py")
  27. print("Git Diff app.py on Server:\n", stdout.read().decode('utf-8'))
  28. except Exception as e:
  29. print(f"Error: {e}")
  30. finally:
  31. ssh.close()