tests: hostapd.py/wpasupplicant.py use Host when executing commands

Execute commands using the Host class. This enables use of remote hosts
as well.

Signed-off-by: Janusz Dziedzic <janusz.dziedzic@tieto.com>
This commit is contained in:
Janusz Dziedzic 2016-03-08 14:28:04 +01:00 committed by Jouni Malinen
parent 2fa2671573
commit 8ce4855b23
2 changed files with 17 additions and 17 deletions

View file

@ -10,6 +10,7 @@ import logging
import binascii import binascii
import struct import struct
import wpaspy import wpaspy
import remotehost
logger = logging.getLogger() logger = logging.getLogger()
hapd_ctrl = '/var/run/hostapd' hapd_ctrl = '/var/run/hostapd'
@ -20,6 +21,7 @@ def mac2tuple(mac):
class HostapdGlobal: class HostapdGlobal:
def __init__(self, hostname=None, port=8878): def __init__(self, hostname=None, port=8878):
self.host = remotehost.Host(hostname)
self.hostname = hostname self.hostname = hostname
self.port = port self.port = port
if hostname is None: if hostname is None:
@ -110,6 +112,7 @@ class HostapdGlobal:
class Hostapd: class Hostapd:
def __init__(self, ifname, bssidx=0, hostname=None, port=8877): def __init__(self, ifname, bssidx=0, hostname=None, port=8877):
self.host = remotehost.Host(hostname, ifname)
self.ifname = ifname self.ifname = ifname
if hostname is None: if hostname is None:
self.ctrl = wpaspy.Ctrl(os.path.join(hapd_ctrl, ifname)) self.ctrl = wpaspy.Ctrl(os.path.join(hapd_ctrl, ifname))

View file

@ -10,8 +10,8 @@ import logging
import binascii import binascii
import re import re
import struct import struct
import subprocess
import wpaspy import wpaspy
import remotehost
logger = logging.getLogger() logger = logging.getLogger()
wpas_ctrl = '/var/run/wpa_supplicant' wpas_ctrl = '/var/run/wpa_supplicant'
@ -22,6 +22,7 @@ class WpaSupplicant:
self.hostname = hostname self.hostname = hostname
self.group_ifname = None self.group_ifname = None
self.gctrl_mon = None self.gctrl_mon = None
self.host = remotehost.Host(hostname, ifname)
if ifname: if ifname:
self.set_ifname(ifname, hostname, port) self.set_ifname(ifname, hostname, port)
else: else:
@ -58,6 +59,7 @@ class WpaSupplicant:
if hostname != None: if hostname != None:
self.ctrl = wpaspy.Ctrl(hostname, port) self.ctrl = wpaspy.Ctrl(hostname, port)
self.mon = wpaspy.Ctrl(hostname, port) self.mon = wpaspy.Ctrl(hostname, port)
self.host = remotehost.Host(hostname, ifname)
else: else:
self.ctrl = wpaspy.Ctrl(os.path.join(wpas_ctrl, ifname)) self.ctrl = wpaspy.Ctrl(os.path.join(wpas_ctrl, ifname))
self.mon = wpaspy.Ctrl(os.path.join(wpas_ctrl, ifname)) self.mon = wpaspy.Ctrl(os.path.join(wpas_ctrl, ifname))
@ -93,11 +95,10 @@ class WpaSupplicant:
def interface_add(self, ifname, config="", driver="nl80211", def interface_add(self, ifname, config="", driver="nl80211",
drv_params=None, br_ifname=None, create=False, drv_params=None, br_ifname=None, create=False,
set_ifname=True, all_params=False, if_type=None): set_ifname=True, all_params=False, if_type=None):
try: status, groups = self.host.execute("id")
groups = subprocess.check_output(["id"]) if status != 0:
group = "admin" if "(admin)" in groups else "adm"
except Exception, e:
group = "admin" group = "admin"
group = "admin" if "(admin)" in groups else "adm"
cmd = "INTERFACE_ADD " + ifname + "\t" + config + "\t" + driver + "\tDIR=/var/run/wpa_supplicant GROUP=" + group cmd = "INTERFACE_ADD " + ifname + "\t" + config + "\t" + driver + "\tDIR=/var/run/wpa_supplicant GROUP=" + group
if drv_params: if drv_params:
cmd = cmd + '\t' + drv_params cmd = cmd + '\t' + drv_params
@ -186,18 +187,14 @@ class WpaSupplicant:
if iter == 60: if iter == 60:
logger.error(self.ifname + ": Driver scan state did not clear") logger.error(self.ifname + ": Driver scan state did not clear")
print "Trying to clear cfg80211/mac80211 scan state" print "Trying to clear cfg80211/mac80211 scan state"
try: status, buf = self.host.execute("ifconfig " + self.ifname + " down")
cmd = ["ifconfig", self.ifname, "down"] if status != 0:
subprocess.call(cmd) logger.info("ifconfig failed: " + buf)
except subprocess.CalledProcessError, e: logger.info(status)
logger.info("ifconfig failed: " + str(e.returncode)) status, buf = self.host.execute("ifconfig " + self.ifname + " up")
logger.info(e.output) if status != 0:
try: logger.info("ifconfig failed: " + buf)
cmd = ["ifconfig", self.ifname, "up"] logger.info(status)
subprocess.call(cmd)
except subprocess.CalledProcessError, e:
logger.info("ifconfig failed: " + str(e.returncode))
logger.info(e.output)
if iter > 0: if iter > 0:
# The ongoing scan could have discovered BSSes or P2P peers # The ongoing scan could have discovered BSSes or P2P peers
logger.info("Run FLUSH again since scan was in progress") logger.info("Run FLUSH again since scan was in progress")