import paramiko
import os
import time

HOST = "135.125.102.180"
USER = "ubuntu"
PASS = "BotPascal2026!"
REMOTE_BASE = "/var/www/html/repondeur_mail_grok"

def main():
    try:
        ssh = paramiko.SSHClient()
        ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        ssh.connect(HOST, username=USER, password=PASS)
        
        # 1. Kill background scan
        print("🛑 Arrêt du scan en arrière-plan (recheck_clients_v2)...")
        ssh.exec_command("sudo pkill -f recheck_clients")
        time.sleep(2) # Wait for connections to close
        
        # 2. Run search for archdelgaudio62
        print("\n🔍 Relance de la recherche pour archdelgaudio62@gmail.com...")
        stdin, stdout, stderr = ssh.exec_command(f"cd {REMOTE_BASE} && php scripts/search_archdelgaudio.php")
        print(stdout.read().decode())
        
        ssh.close()
        
    except Exception as e:
        print(f"❌ Error: {e}")

if __name__ == "__main__":
    main()
