[PATCH v2 19/22] 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 | 93 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 93 insertions(+)

diff --git a/wpaspy/wpaspy.py b/wpaspy/wpaspy.py
index b6a3c6f..a6ac931 100644
--- a/wpaspy/wpaspy.py
+++ b/wpaspy/wpaspy.py
@@ -10,10 +10,103 @@ import os
 import stat
 import socket
 import select
+import subprocess
+import threading
 
 counter = 0
 debug = False
 
+
+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)
+
+class Host():
+    def __init__(self, host=None, ifname=None, port=None, name=""):
+        global debug
+        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):
+        if debug is True:
+            print "execute: " + command
+        words = command.split()
+        cmd = []
+        for word in words:
+            cmd.append(word)
+        try:
+             status = 0;
+             buf = subprocess.check_output(cmd)
+        except subprocess.CalledProcessError as e:
+             if debug is True:
+                 print "status: ", e.returncode
+                 print "buf: ", e.output
+             return e.returncode, e.output
+
+        if debug is True:
+            print "status: ", status
+            print "buf: ", buf
+        return status, buf
+
+    def execute(self, command):
+        if self.host is None:
+            return self.local_execute(command)
+
+        cmd = ["ssh", "root@" + self.host, command]
+        if debug is True:
+            _cmd = self.name + " execute: "
+            for c in cmd:
+                _cmd = _cmd + " " + c
+            print _cmd
+        try:
+            status = 0
+            buf = subprocess.check_output(cmd)
+        except subprocess.CalledProcessError as e:
+             if debug is True:
+                 print self.name + " status: ", e.returncode
+                 print self.name + " buf: ", e.output
+             return e.returncode, e.output
+        if debug is True:
+            print self.name + " status: ", status
+            print self.name + " buf: ", 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]
+        if debug is True:
+            _cmd = self.name + " execute_run: "
+            for c in cmd:
+                _cmd = _cmd + " " + c
+            print _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"
+
+        if debug is True:
+            print 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