forked from armadaplatform/armada
-
Notifications
You must be signed in to change notification settings - Fork 0
/
remote.py
38 lines (30 loc) · 1.17 KB
/
remote.py
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
import subprocess
def execute_local_command(command):
p = subprocess.Popen(
command,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
shell=True
)
out, err = p.communicate()
return p.returncode, out, err
def execute_remote_command(remote_address, command):
import paramiko
class SilentPolicy(paramiko.WarningPolicy):
def missing_host_key(self, client, hostname, key):
pass
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(SilentPolicy())
ssh_key = paramiko.RSAKey.from_private_key_file(remote_address['ssh_key'])
ssh.connect(remote_address['host'], username=remote_address['user'], pkey=ssh_key, port=int(remote_address['port']),
timeout=10)
ssh_stdin, ssh_stdout, ssh_stderr = ssh.exec_command(command)
ssh_out = ssh_stdout.read()
ssh_err = ssh_stderr.read()
ssh_return_code = ssh_stdout.channel.recv_exit_status()
ssh.close()
return ssh_return_code, ssh_out, ssh_err
def execute_command(command, remote_address=None):
if remote_address:
return execute_remote_command(remote_address, command)
return execute_local_command(command)