File size: 2,808 Bytes
7212614
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
import json
import argparse
import paramiko

def load_servers(config_path='config.json'):
    """تحميل قائمة الخوادم من ملف إعدادات JSON"""
    try:
        with open(config_path, 'r') as f:
            data = json.load(f)
            return data.get('servers', [])
    except Exception as e:
        print(f"[!] Failed to load config: {e}")
        return []

def copy_file(server, local_file, remote_file):
    """نسخ ملف من الجهاز المحلي إلى الخادم عبر SSH"""
    try:
        ssh = paramiko.SSHClient()
        ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        ssh.connect(
            hostname=server['host'],
            port=server.get('port', 22),  # 22 هو البورت الافتراضي لـ SSH
            username=server['username'],
            password=server['password']
        )
        sftp = ssh.open_sftp()
        sftp.put(local_file, remote_file)
        sftp.close()
        print(f"[+] Uploaded '{local_file}' to '{server['host']}:{remote_file}'")
        return ssh
    except Exception as e:
        print(f"[!] Failed to upload '{local_file}': {e}")
        return None

def execute_remote_command(ssh, command):
    """تنفيذ أمر على الخادم عن بعد وطباعة النتيجة"""
    try:
        stdin, stdout, stderr = ssh.exec_command(command)
        output = stdout.read().decode()
        errors = stderr.read().decode()
        if output:
            print(f"[+] Output:\n{output}")
        if errors:
            print(f"[!] Errors:\n{errors}")
    except Exception as e:
        print(f"[!] Failed to execute command: {e}")

def main():
    parser = argparse.ArgumentParser(description="Upload files to server.")
    parser.add_argument("files", nargs='+', help="Local files to upload")
    parser.add_argument("--remote-path", default="/tmp", help="Remote path to upload files to")
    parser.add_argument("--run", action="store_true", help="Execute main.py after upload")
    parser.add_argument("--config", default="config.json", help="Path to server config JSON file")
    args = parser.parse_args()

    servers = load_servers(args.config)
    if not servers:
        print("[!] No servers loaded.")
        return

    server = servers[0]  # اختيار أول سيرفر في القائمة
    for file in args.files:
        if not os.path.exists(file):
            print(f"[!] File not found: {file}")
            continue

        remote_file = os.path.join(args.remote_path, os.path.basename(file))
        ssh = copy_file(server, file, remote_file)
        if ssh and args.run and os.path.basename(file) == "main.py":
            execute_remote_command(ssh, f"python3 {remote_file}")
            ssh.close()

if __name__ == "__main__":
    main()