import paramiko

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)
        
        # PHP script to test detailed exec
        php_code = """<?php
        $output = [];
        $return = 0;
        // Try basic ls
        exec('ls -la /tmp 2>&1', $output, $return);
        echo "LS RETURN: $return\\n";
        echo "LS OUTPUT: " . implode("\\n", $output) . "\\n";
        
        // Try writing to file
        exec('echo "test" > /tmp/test_exec.log 2>&1', $output2, $return2);
        echo "WRITE RETURN: $return2\\n";
        echo "WRITE OUTPUT: " . implode("\\n", $output2) . "\\n";
        
        // Try nohup
        exec('nohup echo "background" > /tmp/bg_test.log 2>&1 &', $output3, $return3);
        echo "NOHUP RETURN: $return3\\n";
        echo "NOHUP OUTPUT: " . implode("\\n", $output3) . "\\n";
        ?>"""
        
        cmd = f"echo '{php_code}' > /tmp/test_exec.php && echo '{PASS}' | sudo -S mv /tmp/test_exec.php {REMOTE_BASE}/public/test_exec.php"
        ssh.exec_command(cmd)
        
        print("🔍 Testing exec() via Web...")
        stdin, stdout, stderr = ssh.exec_command(f"curl -s http://localhost/repondeur_mail_grok/public/test_exec.php")
        print(stdout.read().decode())
        
        # Cleanup
        ssh.exec_command(f"echo '{PASS}' | sudo -S rm {REMOTE_BASE}/public/test_exec.php")
        
        ssh.close()
    except Exception as e:
        print(f"❌ Error: {e}")

if __name__ == "__main__":
    main()
