|
| 1 | +import subprocess |
| 2 | +import signal |
| 3 | +from oslo_log import log |
| 4 | + |
| 5 | +LOG = log.getLogger(__name__) |
| 6 | + |
| 7 | + |
| 8 | +def _subprocess_setup(): |
| 9 | + # Python installs a SIGPIPE handler by default. This is usually not what |
| 10 | + # non-Python subprocesses expect. |
| 11 | + signal.signal(signal.SIGPIPE, signal.SIG_DFL) |
| 12 | + |
| 13 | + |
| 14 | +def _subprocess_popen(args, stdin=None, stdout=None, stderr=None, shell=False, |
| 15 | + env=None, preexec_fn=_subprocess_setup, close_fds=True): |
| 16 | + return subprocess.Popen(args, shell=shell, stdin=stdin, stdout=stdout, |
| 17 | + stderr=stderr, preexec_fn=preexec_fn, |
| 18 | + close_fds=close_fds, env=env) |
| 19 | + |
| 20 | + |
| 21 | +class SSHClient(object): |
| 22 | + def __init__(self, host, user, passwd, script): |
| 23 | + self.host = host |
| 24 | + self.user = user |
| 25 | + self.passwd = passwd |
| 26 | + self.script = script |
| 27 | + |
| 28 | + def exec_command(self, cmd): |
| 29 | + LOG.info("Exec command: %s", cmd) |
| 30 | + cmd = self.script + ' ' + self.host + ' ' + self.user \ |
| 31 | + + ' ' + self.passwd + ' ' + '"' + cmd + '"' |
| 32 | + return _subprocess_popen(cmd, shell=True, |
| 33 | + stdin=subprocess.PIPE, |
| 34 | + stdout=subprocess.PIPE, |
| 35 | + stderr=subprocess.PIPE) |
0 commit comments