Example #1
0
class Hostapd(object):
    """ Wrapper class for UCI (OpenWRT) commands """
    executor = None
    devicename = None

    def __init__(self, config = "/etc/hostapd/hostapd.conf", wifi_restart_command, executor=None):
        if executor == None:
            self.executor = Executor()
        else:
            self.executor = executor

        self.config = config
        self.restart_command = wifi_restart_command

    def get_wifi_interface(self):
        "Return the wifi interface name (e.g. 'wlan0')"
        ret = self.executor.execute_cmd(['grep', '^interface', self.config, '|', 'cut','-f2','-d"="'])
        
        if ret is None:
            print 'No WiFi device name found.'

        return ret

    def get_bridge_interface(self):
        "Return the bridge interface name (e.g. 'br0')"
        ret = self.executor.execute_cmd(['grep', '^bridge', self.config, '|', 'cut','-f2','-d"="'])
        
        if ret is None:
            print 'No bridge device name found.'

        return ret

    def get_wifi_mode(self):
        "Get operation mode (e.g. n)"
        ret = self.executor.execute_cmd(['grep', '^hw_mode', self.config, '|', 'cut','-f2','-d"="'])
        
        if ret is None:
            print 'No mode found in config.'
        
        return ret

    def set_channel(self, channel):
        "Sets the wifi channel. Requires commit and restart of WiFi for changes to take effect"
        self.executor.execute_cmd(['sudo','cat',self.config,'|','sed','-e',"s/channel=[0-9][0-9]*/channel=%d/g" % channel, '>', '/tmp/tmp_hostapd.conf'])
        self.executor.execute_cmd(['sudo','mv','/tmp/tmp_hostapd.conf',self.config])

    def restart():
        "Restart hostapd"
        self.executor.execute_cmd(wifi_restart_command)

    def get_wifi_ssid(self):
        "Return the wifi ssid (e.g. for 'node1-wifi')"
        ret = self.executor.execute_cmd(['grep', '^ssid', self.config, '|', 'cut','-f2','-d"="'])
        
        if ret is None:
            print 'No SSID found in config.'
        
        return ret