[PATCH v2 19/22] wpaspy: add Host class
Janusz Dziedzic
janusz.dziedzic at tieto.com
Sun Feb 7 05:07:52 PST 2016
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 at hostname <command>
Signed-off-by: Janusz Dziedzic <janusz.dziedzic at tieto.com>
---
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
More information about the Hostap
mailing list