39 lines
1.3 KiB
Python
39 lines
1.3 KiB
Python
import subprocess
|
|
from config import RUNNING_ON_MAIN_SERVER, MAIN_SERVER_IP
|
|
|
|
def run_command(command):
|
|
"""Run command on main server (directly or via SSH)"""
|
|
if RUNNING_ON_MAIN_SERVER:
|
|
try:
|
|
result = subprocess.run(command, shell=True, capture_output=True, text=True, timeout=30)
|
|
return result.stdout, result.stderr
|
|
except subprocess.TimeoutExpired:
|
|
return "", "Command timeout"
|
|
except Exception as e:
|
|
return "", str(e)
|
|
else:
|
|
ssh_cmd = [
|
|
"ssh",
|
|
"-o", "StrictHostKeyChecking=no",
|
|
"-o", "ConnectTimeout=10",
|
|
"-o", "BatchMode=yes",
|
|
f"root@{MAIN_SERVER_IP}",
|
|
command
|
|
]
|
|
try:
|
|
result = subprocess.run(ssh_cmd, capture_output=True, text=True, timeout=30)
|
|
return result.stdout, result.stderr
|
|
except subprocess.TimeoutExpired:
|
|
return "", "Connection timeout"
|
|
except Exception as e:
|
|
return "", str(e)
|
|
|
|
def run_ssh_to_vm(command):
|
|
"""Run command on VM via reverse tunnel"""
|
|
from config import VM_PASSWORD
|
|
import os
|
|
|
|
cmd = f"sshpass -p '{VM_PASSWORD}' ssh -p 2223 -o StrictHostKeyChecking=no -o ConnectTimeout=5 root@localhost '{command}'"
|
|
result = os.popen(cmd).read()
|
|
return result
|