File size: 2,658 Bytes
74bde7a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#!/usr/bin/env python3
"""SSH helper using paramiko for HuggingFace Space access"""

import paramiko
import sys
import os

def execute_ssh_command(command, timeout=60):
    """Execute command on HF Space via SSH and return output"""

    # SSH configuration
    hostname = "ssh.hf.space"
    username = "evgueni-p-fbmc-chronos2-forecast"
    key_file = "/c/Users/evgue/.ssh/id_ed25519"

    # Convert Windows path for paramiko
    key_file_win = "C:\\Users\\evgue\\.ssh\\id_ed25519"

    try:
        # Create SSH client
        client = paramiko.SSHClient()
        client.set_missing_host_key_policy(paramiko.AutoAddPolicy())

        # Load private key
        private_key = paramiko.Ed25519Key.from_private_key_file(key_file_win)

        # Connect
        print(f"[*] Connecting to {hostname}...")
        client.connect(
            hostname=hostname,
            username=username,
            pkey=private_key,
            timeout=30,
            look_for_keys=False,
            allow_agent=False
        )
        print("[+] Connected!")

        # Execute command
        print(f"[*] Executing: {command[:100]}...")
        stdin, stdout, stderr = client.exec_command(command, timeout=timeout)

        # Get output
        output = stdout.read().decode('utf-8')
        error = stderr.read().decode('utf-8')
        exit_code = stdout.channel.recv_exit_status()

        client.close()

        return {
            'stdout': output,
            'stderr': error,
            'exit_code': exit_code,
            'success': exit_code == 0
        }

    except Exception as e:
        return {
            'stdout': '',
            'stderr': str(e),
            'exit_code': -1,
            'success': False
        }

if __name__ == "__main__":
    if len(sys.argv) < 2:
        print("Usage: python ssh_helper.py 'command to execute'")
        sys.exit(1)

    command = sys.argv[1]
    result = execute_ssh_command(command)

    print("\n" + "="*60)
    print("STDOUT:")
    print("="*60)
    # Handle Unicode encoding for Windows console
    try:
        print(result['stdout'])
    except UnicodeEncodeError:
        print(result['stdout'].encode('ascii', errors='replace').decode('ascii'))

    if result['stderr']:
        print("\n" + "="*60)
        print("STDERR:")
        print("="*60)
        # Handle Unicode encoding for Windows console
        try:
            print(result['stderr'])
        except UnicodeEncodeError:
            print(result['stderr'].encode('ascii', errors='replace').decode('ascii'))

    print("\n" + "="*60)
    print(f"Exit code: {result['exit_code']}")
    print("="*60)

    sys.exit(result['exit_code'])