From patchwork Fri Mar 4 09:20:36 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Janusz.Dziedzic@tieto.com X-Patchwork-Id: 591858 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.9]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id D637C14018C for ; Fri, 4 Mar 2016 20:26:11 +1100 (AEDT) Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (1024-bit key; unprotected) header.d=tieto.com header.i=@tieto.com header.b=d3fOH2NV; dkim-atps=neutral Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.80.1 #2 (Red Hat Linux)) id 1ablzp-00051d-N7; Fri, 04 Mar 2016 09:26:05 +0000 Received: from casper.infradead.org ([2001:770:15f::2]) by bombadil.infradead.org with esmtps (Exim 4.80.1 #2 (Red Hat Linux)) id 1ablvT-0001ye-F9 for hostap@bombadil.infradead.org; Fri, 04 Mar 2016 09:21:35 +0000 Received: from mail-lb0-x231.google.com ([2a00:1450:4010:c04::231]) by casper.infradead.org with esmtps (Exim 4.85 #2 (Red Hat Linux)) id 1ablvR-0006pJ-6s for hostap@lists.infradead.org; Fri, 04 Mar 2016 09:21:33 +0000 Received: by mail-lb0-x231.google.com with SMTP id k15so54126255lbg.0 for ; Fri, 04 Mar 2016 01:21:13 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=tieto.com; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=0Wz+R1GoAlnctibf6PxrXxtExQxOYD3DNwb9VUE87J0=; b=d3fOH2NVXos7ld46fubc21+0B8qxfhAbaarL9mqXmwk9INS0f8cTMJ1BjfM8hA/bkR vtnIaig5fhRG4mZujTDDG1p+DrS8AlZ08CmTi+VpXUiCsyiK3hb+4O6iw+kwLRIEesZY P/xJwZKfCubOiQEM9qdGhmF9vOx+FisA+Gtwg= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=0Wz+R1GoAlnctibf6PxrXxtExQxOYD3DNwb9VUE87J0=; b=XqwXVD7NGxCsXWeD7nrCcYtGNWeffkmWcE8/zKx7nLge4RdZ+a95VxwOUJvNHeJh34 DDMxsRMTDjw4VgogIDhBXg3Cnsr7lXL30lLcIHEgFtut8XtRKGyDoZ2k7FzEos4o0KGV ohv5Yrt8xRqm4ljJl+pnmlm1Ci2o+R5WMeOb+42t3mwebyJXbaF29HQiQyKcmq8qnZyI XFNOJHYWgny6hSnGc813Joc13Oy6mTBGJ9ULjqVAoJ6vF+K4dq883Q3J7rIqHvR9zgj1 fEjy3/+Xkq/KptqpQ9bEClhFYhFmLgjcnoklmrCljgPprBAV2aj7t5l3jCM3bGQDMC5W 4dTQ== X-Gm-Message-State: AD7BkJLVNCuyedWviJeNlgobIxcg6jE72OSgNh/1dX/1sjzEbNajx3QBRYvopDTd3Nj65wTZxcMwfTYNJ/2SFnl0ifFG1SEC7S2teY5+ZBhSUY4B4ZRRizW9M1Wx/zr4+qvGXIQFBTLsOzIdf+GlwMRQIgeRBlTdhU0rUa+j5BLAEeL1MQegtNp9pxjxhXGEXTao X-Received: by 10.25.78.137 with SMTP id c131mr2276358lfb.21.1457083272590; Fri, 04 Mar 2016 01:21:12 -0800 (PST) Received: from dell6430.guest.wifi ([91.198.246.8]) by smtp.gmail.com with ESMTPSA id r191sm437001lfe.28.2016.03.04.01.21.11 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Fri, 04 Mar 2016 01:21:12 -0800 (PST) From: Janusz Dziedzic To: hostap@lists.infradead.org Subject: [PATCH v5 20/25] wpaspy: add Host class Date: Fri, 4 Mar 2016 10:20:36 +0100 Message-Id: <1457083241-7492-21-git-send-email-janusz.dziedzic@tieto.com> X-Mailer: git-send-email 1.9.1 In-Reply-To: <1457083241-7492-1-git-send-email-janusz.dziedzic@tieto.com> References: <1457083241-7492-1-git-send-email-janusz.dziedzic@tieto.com> X-DomainID: tieto.com X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20160304_092133_314045_D5DD4C03 X-CRM114-Status: UNSURE ( 9.07 ) X-CRM114-Notice: Please train this message. X-Spam-Score: -2.7 (--) X-Spam-Report: SpamAssassin version 3.4.0 on casper.infradead.org summary: Content analysis details: (-2.7 points, 5.0 required) pts rule name description ---- ---------------------- -------------------------------------------------- -0.7 RCVD_IN_DNSWL_LOW RBL: Sender listed at http://www.dnswl.org/, low trust [2a00:1450:4010:c04:0:0:0:231 listed in] [list.dnswl.org] -0.0 SPF_PASS SPF: sender matches SPF record -1.9 BAYES_00 BODY: Bayes spam probability is 0 to 1% [score: 0.0000] -0.1 DKIM_VALID_AU Message has a valid DKIM or DK signature from author's domain 0.1 DKIM_SIGNED Message has a DKIM or DK signature, not necessarily valid -0.1 DKIM_VALID Message has at least one valid DKIM or DK signature X-BeenThere: hostap@lists.infradead.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: j@w1.fi, Janusz Dziedzic MIME-Version: 1.0 Sender: "Hostap" Errors-To: hostap-bounces+incoming=patchwork.ozlabs.org@lists.infradead.org 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 Signed-off-by: Janusz Dziedzic --- 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