From patchwork Fri Feb 26 09:38:35 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: 588738 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from bombadil.infradead.org (bombadil.infradead.org [IPv6:2001:1868:205::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 918EF1402A9 for ; Fri, 26 Feb 2016 20:50:44 +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=Djgp742Q; 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 1aZF2i-000306-RF; Fri, 26 Feb 2016 09:50:36 +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 1aZF2e-0002rk-5r for hostap@bombadil.infradead.org; Fri, 26 Feb 2016 09:50:32 +0000 Received: from mail-wm0-x234.google.com ([2a00:1450:400c:c09::234]) by casper.infradead.org with esmtps (Exim 4.85 #2 (Red Hat Linux)) id 1aZEsB-0002vB-4S for hostap@lists.infradead.org; Fri, 26 Feb 2016 09:39:43 +0000 Received: by mail-wm0-x234.google.com with SMTP id g62so65133492wme.0 for ; Fri, 26 Feb 2016 01:39:21 -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=Djgp742QwZWID3+e3yDobiK7BoxAX0NczAgearpCUMTiPYeo+cnfT7LMMNMkk+8aMq OfwURpGGuT5sY3O3Tq2wGqYqjiy3sR0beI6YgvWyc2RqnpH1sCKyPPjzYW8/dYjdZTMo 87MUh+lHbnTaxUZt5vd54X1NyS+WOTbSoJgzU= 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=iJefxd32cUuFagaQe2Cdt7bNo92d8Qbugp6ouq/BV/P5OnRSrQiwHX2QG2DN+VQDjK E3ZhwlxduMi4aKZgEtXm++MtaPwIOiuncrVOjAePCokrvdmD2cAkxnILLWj6UOrWfq+y HYMox59LTCsYcSBlcwSZYCYRJNPEt3tuppWhcclh+NE6MuejCTbqYUdph8SVCwrcy32f LMEp2iVF2Pb+W9QkP6OXINkXVoSW1sTkr+xdjqlpji8s3sAHEjbF1g2/OKPP8yElFDKb +OW+Go1GwZ5AGa9/5gq9oC8khcs4G1wGo+/A+Z+VH04DN7ZzcWCQpYRDWP07hhRTBthY CLoQ== X-Gm-Message-State: AD7BkJLHj7z7iZ9LTUEMIC183JfMHdMXWSIq/ZmAjin+RS03YvUJAS7i/OMiBkmt2XkD2wY7Wg0YWw44yA85S/27W8kkTNc3XCrDyLkKQ+fFLQZqnXp8g62iwhg9RnVAJ55+rAuJ0SkKGteBvx3ef+Hqh7/ZotMupttQSsZzfQL2uIx8krmcFbU+50XEuq6XGBlG X-Received: by 10.194.103.72 with SMTP id fu8mr720781wjb.70.1456479560942; Fri, 26 Feb 2016 01:39:20 -0800 (PST) Received: from dell6430.guest.wifi ([91.198.246.8]) by smtp.gmail.com with ESMTPSA id z65sm1915950wmg.1.2016.02.26.01.39.19 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Fri, 26 Feb 2016 01:39:20 -0800 (PST) From: Janusz Dziedzic To: hostap@lists.infradead.org Subject: [PATCH v4 20/29] wpaspy: add Host class Date: Fri, 26 Feb 2016 10:38:35 +0100 Message-Id: <1456479524-14185-21-git-send-email-janusz.dziedzic@tieto.com> X-Mailer: git-send-email 1.9.1 In-Reply-To: <1456479524-14185-1-git-send-email-janusz.dziedzic@tieto.com> References: <1456479524-14185-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-20160226_093943_217863_55197805 X-CRM114-Status: UNSURE ( 8.77 ) 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.0 SPF_PASS SPF: sender matches SPF record -0.7 RCVD_IN_DNSWL_LOW RBL: Sender listed at http://www.dnswl.org/, low trust [2a00:1450:400c:c09:0:0:0:234 listed in] [list.dnswl.org] -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