Patchwork [RFC,v2,3/8] lib/oeqa/utils/sshcontrol.py: helper module for running remote commands

login
register
mail settings
Submitter Stanacar, StefanX
Date July 5, 2013, 9:27 a.m.
Message ID <a29e7a16581e848d017a56de42e4ee751639d529.1373013888.git.stefanx.stanacar@intel.com>
Download mbox | patch
Permalink /patch/53143/
State Accepted
Commit ac341af8fafc5ea6e3f937ea6ad078ab0812b2bc
Headers show

Comments

Stanacar, StefanX - July 5, 2013, 9:27 a.m.
Provides a class for setting up ssh connections,
running commands and copying files to/from a target.

Signed-off-by: Stefan Stanacar <stefanx.stanacar@intel.com>
---
 meta/lib/oeqa/utils/__init__.py   |   0
 meta/lib/oeqa/utils/sshcontrol.py | 100 ++++++++++++++++++++++++++++++++++++++
 2 files changed, 100 insertions(+)
 create mode 100644 meta/lib/oeqa/utils/__init__.py
 create mode 100644 meta/lib/oeqa/utils/sshcontrol.py
Colin Walters - July 7, 2013, 9:55 p.m.
On Fri, 2013-07-05 at 12:27 +0300, Stefan Stanacar wrote:

> +        actualcmd = "ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -l root %s '%s'" % (self.host, cmd)

This is going to fail if cmd contains a single quote.  Personally, I
*always* pass around subprocess arguments as an array.  Anything else is
going to lead to quoting problems.
Stanacar, StefanX - July 9, 2013, 4:50 p.m.
Hi Colin,

On Sun, 2013-07-07 at 17:55 -0400, Colin Walters wrote:
> On Fri, 2013-07-05 at 12:27 +0300, Stefan Stanacar wrote:
> 
> > +        actualcmd = "ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -l root %s '%s'" % (self.host, cmd)
> 
> This is going to fail if cmd contains a single quote.  Personally, I
> *always* pass around subprocess arguments as an array.  Anything else is
> going to lead to quoting problems.
> 
> 
> 

You're right, I just sent a patch that drops that and doesn't use shlex
anymore.

Thanks,
Stefan

Patch

diff --git a/meta/lib/oeqa/utils/__init__.py b/meta/lib/oeqa/utils/__init__.py
new file mode 100644
index 0000000..e69de29
diff --git a/meta/lib/oeqa/utils/sshcontrol.py b/meta/lib/oeqa/utils/sshcontrol.py
new file mode 100644
index 0000000..85a09a0
--- /dev/null
+++ b/meta/lib/oeqa/utils/sshcontrol.py
@@ -0,0 +1,100 @@ 
+import subprocess
+import time
+import os
+import shlex
+
+class SSHControl(object):
+
+    def __init__(self, host=None, timeout=200, logfile=None):
+        self.host = host
+        self.timeout = timeout
+        self._out = ''
+        self._ret = 126
+        self.logfile = logfile
+
+    def log(self, msg):
+        if self.logfile:
+            with open(self.logfile, "a") as f:
+                f.write("%s\n" % msg)
+
+    def _internal_run(self, cmd):
+        # ssh hangs without os.setsid
+        proc = subprocess.Popen(shlex.split(cmd), shell=False, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, preexec_fn=os.setsid)
+        return proc
+
+    def run(self, cmd, timeout=None):
+        """Run cmd and get it's return code and output.
+        Let it run for timeout seconds and then terminate/kill it,
+        if time is 0 will let cmd run until it finishes.
+        Time can be passed to here or can be set per class instance."""
+
+
+
+        actualcmd = "ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -l root %s '%s'" % (self.host, cmd)
+        if self.host:
+            sshconn = self._internal_run(actualcmd)
+        else:
+            raise Exception("Remote IP hasn't been set: '%s'" % actualcmd)
+
+        if timeout == 0:
+            self.log("[SSH run without timeout]$ %s" % actualcmd)
+            self.log("  # %s" % cmd)
+            self._out = sshconn.communicate()[0]
+            self._ret = sshconn.poll()
+        else:
+            if timeout is None:
+                endtime = time.time() + self.timeout
+            else:
+                endtime = time.time() + timeout
+            while sshconn.poll() is None and time.time() < endtime:
+                time.sleep(1)
+            self.log("[SSH run with timeout]$ %s" % actualcmd)
+            self.log("  # %s" % cmd)
+            # process hasn't returned yet
+            if sshconn.poll() is None:
+                self._ret = 255
+                sshconn.terminate()
+                sshconn.kill()
+                self._out = sshconn.stdout.read()
+                sshconn.stdout.close()
+                self.log("[!!! process killed]")
+            else:
+                self._out = sshconn.stdout.read()
+                self._ret = sshconn.poll()
+        # remove first line from output which is always smth like (unless return code is 255 - which is a ssh error):
+        # Warning: Permanently added '192.168.7.2' (RSA) to the list of known hosts.
+        if self._ret != 255:
+            self._out = '\n'.join(self._out.splitlines()[1:])
+        self.log("%s" % self._out)
+        self.log("[SSH command returned]: %s" % self._ret)
+        return (self._ret, self._out)
+
+    def _internal_scp(self, cmd):
+        self.log("[SCP]$ %s" % cmd)
+        scpconn = self._internal_run(cmd)
+        try:
+            self._out = scpconn.communicate()[0]
+            self._ret = scpconn.poll()
+            if self._ret != 0:
+                self.log("%s" % self._out)
+                raise Exception("Error copying file")
+        except Exception as e:
+            print("Execution failed: %s :" % cmd)
+            print e
+        self.log("%s" % self._out)
+        return (self._ret, self._out)
+
+    def copy_to(self, localpath, remotepath):
+        actualcmd = "scp -o UserKnownHostsFile=/dev/null  -o StrictHostKeyChecking=no %s root@%s:%s" % (localpath, self.host, remotepath)
+        return self._internal_scp(actualcmd)
+
+
+    def copy_from(self, remotepath, localpath):
+        actualcmd = "scp -o UserKnownHostsFile=/dev/null  -o StrictHostKeyChecking=no root@%s:%s %s" % (self.host, remotepath, localpath)
+        return self._internal_scp(actualcmd)
+
+    def get_status(self):
+        return self._ret
+
+    def get_output(self):
+        return self._out