-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathssh_subprocess.py
More file actions
35 lines (27 loc) · 1.21 KB
/
ssh_subprocess.py
File metadata and controls
35 lines (27 loc) · 1.21 KB
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
import subprocess
import signal
from oslo_log import log
LOG = log.getLogger(__name__)
def _subprocess_setup():
# Python installs a SIGPIPE handler by default. This is usually not what
# non-Python subprocesses expect.
signal.signal(signal.SIGPIPE, signal.SIG_DFL)
def _subprocess_popen(args, stdin=None, stdout=None, stderr=None, shell=False,
env=None, preexec_fn=_subprocess_setup, close_fds=True):
return subprocess.Popen(args, shell=shell, stdin=stdin, stdout=stdout,
stderr=stderr, preexec_fn=preexec_fn,
close_fds=close_fds, env=env)
class SSHClient(object):
def __init__(self, host, user, passwd, script):
self.host = host
self.user = user
self.passwd = passwd
self.script = script
def exec_command(self, cmd):
LOG.info("Exec command: %s", cmd)
cmd = self.script + ' ' + self.host + ' ' + self.user \
+ ' ' + self.passwd + ' ' + '"' + cmd + '"'
return _subprocess_popen(cmd, shell=True,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)