diff mbox

[RFC,v1,2/3] tests/wpaspy: add Host class

Message ID 1453899551-19482-3-git-send-email-janusz.dziedzic@tieto.com
State RFC
Headers show

Commit Message

Janusz.Dziedzic@tieto.com Jan. 27, 2016, 12:59 p.m. UTC
Add Host class.
This should be used with authorized_keys - to access
remote machines. Currently only used to run
wpa_supplicant/hostapd. In the future we could use
this for iw/iperf/ping ...

Signed-off-by: Janusz Dziedzic <janusz.dziedzic@tieto.com>
---
 wpaspy/test.py   | 12 ++++++++++
 wpaspy/wpaspy.py | 67 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 79 insertions(+)
diff mbox

Patch

diff --git a/wpaspy/test.py b/wpaspy/test.py
index 9141a8d..e302a9c 100755
--- a/wpaspy/test.py
+++ b/wpaspy/test.py
@@ -17,6 +17,18 @@  def wpas_connect(host=None, port=9877):
     ifaces = []
 
     if host != None:
+        res = []
+        rhost = wpaspy.Host(host)
+        status, buf = rhost.execute("id");
+        print status
+        print buf
+        t = rhost.execute_run("id; sleep 2", res)
+        rhost.wait_execute_complete(t)
+        print res[0]
+        print res[1]
+        status, buf = rhost.local_execute("uname -a")
+        print status
+        print buf
         try:
             wpas = wpaspy.Ctrl(host, port)
             return wpas
diff --git a/wpaspy/wpaspy.py b/wpaspy/wpaspy.py
index f2c7c17..4499b10 100644
--- a/wpaspy/wpaspy.py
+++ b/wpaspy/wpaspy.py
@@ -10,14 +10,77 @@  import os
 import stat
 import socket
 import select
+import subprocess
+import threading
 
 counter = 0
 
+
+def execute_thread(command, reply):
+    try:
+         status = 0;
+         buf = subprocess.check_output(command)
+    except subprocess.CalledProcessError as e:
+         status = e.returncode
+         buf = e.output
+
+    reply.append(status)
+    reply.append(buf)
+    print "thread exit"
+
+class Host:
+    def __init__(self, host="localhost", iface=None, port=None, name=""):
+        self.host = host
+        self.iface = iface
+        self.port = port
+        self.name = name
+        if self.name == "":
+           self.name = host
+    def local_execute(self, command):
+        try:
+             status = 0;
+             buf = subprocess.check_output([command])
+        except subprocess.CalledProcessError as e:
+             print " Status: ", e.returncode
+             return e.returncode, e.output
+
+        print " Status: ", status
+        return status, buf
+
+    def execute(self, command):
+        try:
+            status = 0
+            buf = subprocess.check_output(["ssh", "root@" + self.host, command])
+        except subprocess.CalledProcessError as e:
+            print self.name + " Status: ", e.returncode
+            return e.returncode, e.output
+        print self.name + " Status: ", status
+        return status, buf
+
+    # async execute
+    def execute_run(self, command, res):
+        cmd = ["ssh",  "root@" + self.host, command]
+        t = threading.Thread(target = execute_thread, args=(cmd, res))
+        t.start()
+        return t
+
+    def wait_execute_complete(self, t, wait=None):
+        if wait == None:
+           wait_str = "infinite"
+        else:
+           wait_str = str(wait) + "s"
+
+        print self.name + " wait_execute_complete(" + wait_str + "): "
+        if t.isAlive():
+                t.join(wait)
+
 class Ctrl:
     def __init__(self, path, port=9877):
         global counter
         self.started = False
         self.attached = False
+        self.path = path
+        self.debug = True
 
         try:
             mode = os.stat(path).st_mode
@@ -76,6 +139,8 @@  class Ctrl:
             self.started = False
 
     def request(self, cmd, timeout=10):
+        if self.debug is True:
+            print self.path + " request: " + cmd
         if self.udp == True:
             self.s.sendto(self.cookie + cmd, self.sockaddr)
         else:
@@ -113,4 +178,6 @@  class Ctrl:
 
     def recv(self):
         res = self.s.recv(4096)
+        if self.debug is True:
+            print self.path + " recv: " + res
         return res