[PATCH v5 20/25] wpaspy: add Host class

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



This could be used for remote host execution.
In such case we have to configure authorized keys
for root user.

So, execution for remote host looks like:

ssh root@hostname <command>

Signed-off-by: Janusz Dziedzic <janusz.dziedzic@xxxxxxxxx>
---
 wpaspy/wpaspy.py | 86 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 86 insertions(+)

diff --git a/wpaspy/wpaspy.py b/wpaspy/wpaspy.py
index 76ac785..7d6fadf 100644
--- a/wpaspy/wpaspy.py
+++ b/wpaspy/wpaspy.py
@@ -11,10 +11,96 @@ import stat
 import socket
 import select
 import logging
+import subprocess
+import threading
 
 logger = logging.getLogger()
 counter = 0
 
+
+def execute_thread(command, reply):
+    try:
+         status = 0;
+         buf = subprocess.check_output(command, stderr=subprocess.STDOUT)
+    except subprocess.CalledProcessError as e:
+         status = e.returncode
+         buf = e.output
+
+    logger.debug("thread exit status: " + str(status))
+    logger.debug("thread exit buf: " + str(buf))
+    reply.append(status)
+    reply.append(buf)
+
+class Host():
+    def __init__(self, host=None, ifname=None, port=None, name=""):
+        self.host = host
+        self.name = name
+        self.ifname = ifname
+        self.port = port
+        if self.name == "" and host != None:
+           self.name = host
+    def local_execute(self, command):
+        logger.debug("execute: " + command)
+        words = command.split()
+        cmd = []
+        for word in words:
+            cmd.append(word)
+        try:
+             status = 0;
+             buf = subprocess.check_output(cmd, stderr=subprocess.STDOUT)
+        except subprocess.CalledProcessError as e:
+             logger.debug("status: " + str(e.returncode))
+             logger.debug("buf: " + str(e.output))
+             return e.returncode, e.output
+
+        logger.debug("status: " + str(status))
+        logger.debug("buf: " + str(buf))
+        return status, buf
+
+    def execute(self, command):
+        if self.host is None:
+            return self.local_execute(command)
+
+        cmd = ["ssh", "root@" + self.host, command]
+        _cmd = self.name + " execute: "
+        for c in cmd:
+            _cmd = _cmd + " " + c
+        logger.debug(_cmd)
+        try:
+            status = 0
+            buf = subprocess.check_output(cmd, stderr=subprocess.STDOUT)
+        except subprocess.CalledProcessError as e:
+             logger.debug(self.name + " status: " + str(e.returncode))
+             logger.debug(self.name + " buf: " + str(e.output))
+             return e.returncode, e.output
+        logger.debug(self.name + " status: " + str(status))
+        logger.debug(self.name + " buf: " + str(buf))
+        return status, buf
+
+    # async execute
+    def execute_run(self, command, res):
+        if self.host is None:
+            cmd = [command]
+        else:
+            cmd = ["ssh",  "root@" + self.host, command]
+        _cmd = self.name + " execute_run: "
+        for c in cmd:
+            _cmd = _cmd + " " + c
+        logger.debug(_cmd)
+        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"
+
+        logger.debug(self.name + " wait_execute_complete(" + wait_str + "): ")
+        if t.isAlive():
+                t.join(wait)
+
 class Ctrl:
     def __init__(self, path, port=9877):
         global counter
-- 
1.9.1


_______________________________________________
Hostap mailing list
Hostap@xxxxxxxxxxxxxxxxxxx
http://lists.infradead.org/mailman/listinfo/hostap



[Index of Archives]     [Linux Wireless]     [Linux Kernel]     [ATH6KL]     [Linux Bluetooth]     [Linux Netdev]     [Kernel Newbies]     [IDE]     [Security]     [Git]     [Netfilter]     [Bugtraq]     [Yosemite News]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux ATA RAID]     [Samba]     [Device Mapper]

  Powered by Linux