[libvirt] [libvirt-test-API][PATCH 2/2] Add four helpful functions in utils.py.

Guannan Ren gren at redhat.com
Wed Jul 27 06:58:33 UTC 2011


    * get_local_hostname() get hostname on local machine
    * exec_cmd() execute shell command
    * remote_exec_pexpect() execture command on remote host
    * scp_file() copy file to remote host
---
 utils/Python/utils.py |   69 +++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 69 insertions(+), 0 deletions(-)

diff --git a/utils/Python/utils.py b/utils/Python/utils.py
index eaa8eab..6787e87 100644
--- a/utils/Python/utils.py
+++ b/utils/Python/utils.py
@@ -26,6 +26,9 @@ import commands
 import socket
 import fcntl
 import struct
+import pexpect
+import string
+import subprocess
 from xml.dom import minidom
 
 class Utils(object):
@@ -58,6 +61,10 @@ class Utils(object):
         arch = ret.split(" ")[-2]
         return arch
 
+    def get_local_hostname(self):
+        """ get local host name """
+        return socket.gethostname()
+
     def get_libvirt_version(self, ver = ''):
         ver = commands.getoutput("rpm -q libvirt|head -1")
         if ver.split('-')[0] == 'libvirt':
@@ -364,3 +371,65 @@ class Utils(object):
         (ret, out) = commands.getstatusoutput(cmd)
         return ret
 
+    def exec_cmd(self, command, sudo=False, cwd=None, infile=None, outfile=None, shell=False, data=None):
+        """
+        Executes an external command, optionally via sudo.
+        """
+        if sudo:
+            if type(command) == type(""):
+                command = "sudo " + command
+            else:
+                command = ["sudo"] + command
+        if infile == None:
+            infile = subprocess.PIPE
+        if outfile == None:
+            outfile = subprocess.PIPE
+        p = subprocess.Popen(command, shell=shell, close_fds=True, cwd=cwd,
+                        stdin=infile, stdout=outfile, stderr=subprocess.PIPE)
+        (out, err) = p.communicate(data)
+        if out == None:
+            # Prevent splitlines() from barfing later on
+            out = ""
+        return (p.returncode, out.splitlines())
+
+    def remote_exec_pexpect(self, hostname, username, password, cmd):
+        """ Remote exec function via pexpect """
+        user_hostname = "%s@%s" % (username, hostname)
+        child = pexpect.spawn("/usr/bin/ssh", [user_hostname, cmd],
+                              timeout = 60, maxread = 2000, logfile = None)
+        while True:
+            index = child.expect(['(yes\/no)', 'password:', pexpect.EOF,
+                                 pexpect.TIMEOUT])
+            if index == 0:
+                child.sendline("yes")
+            elif index == 1:
+                child.sendline(password)
+            elif index == 2:
+                child.close()
+                return 0
+            elif index == 3:
+                child.close()
+                return 1
+
+        return 0
+
+    def scp_file(self, hostname, username, password, target_path, file):
+        """ Scp file to remote host """
+        user_hostname = "%s@%s:%s" % (username, hostname, target_path)
+        child = pexpect.spawn("/usr/bin/scp", [file, user_hostname])
+        while True:
+            index = child.expect(['yes\/no', 'password: ',
+                                   pexpect.EOF,
+                                   pexpect.TIMEOUT])
+            if index == 0:
+                child.sendline("yes")
+            elif index == 1:
+                child.sendline(password)
+            elif index == 2:
+                child.close()
+                return 0
+            elif index == 3:
+                child.close()
+                return 1
+
+        return 0
-- 
1.7.1




More information about the libvir-list mailing list