Exemple #1
0
 def power(self, power=None):
     if power is True:
         cmd("nmcli nm wifi on")
     elif power is False:
         cmd("nmcli nm wifi off")
     else:
         response = cmd("nmcli nm wifi")
         return "enabled" in response
Exemple #2
0
    def _clean(self, partial):
        # list matching connections
        response = cmd("nmcli --fields UUID,NAME con show | grep {}".format(partial))

        # delete all of the matching connections
        for line in response.splitlines():
            if len(line) > 0:
                uuid = line.split()[0]
                cmd("nmcli con delete uuid {}".format(uuid))
Exemple #3
0
    def connect(self, ssid, password):
        # clean up previous connection
        self._clean(self.current())

        # attempt to connect
        response = cmd("nmcli dev wifi connect {} password {} iface {}".format(
            ssid, password, self._interface))

        # parse response
        return not self._errorInResponse(response)
Exemple #4
0
    def current(self):
        # list active connections for all interfaces
        response = cmd("nmcli con status | grep {}".format(self.interface()))

        # the current network is in the first column
        for line in response.splitlines():
            if len(line) > 0:
                return line.split()[0]

        # return none if there was not an active connection
        return None
Exemple #5
0
    def connect(self, ssid, password):
        # attempt to stop any active wpa_supplicant instances
        # ideally we do this just for the interface we care about
        cmd("sudo killall wpa_supplicant")

        # don't do DHCP for GoPros; can cause dropouts with the server
        cmd("sudo ifconfig {} 10.5.5.10/24 up".format(self._interface))

        # create configuration file
        f = open(self._file, "w")
        f.write('network={{\n    ssid="{}"\n    psk="{}"\n}}\n'.format(
            ssid, password))
        f.close()

        # attempt to connect
        cmd("sudo wpa_supplicant -i{} -c{} -B".format(self._interface,
                                                      self._file))

        # check that the connection was successful
        # i've never seen it take more than 3 seconds for the link to establish
        sleep(5)
        if self.current() != ssid:
            return False

        # attempt to grab an IP
        # better hope we are connected because the timeout here is really long
        # cmd('sudo dhclient {}'.format(self._interface))

        # parse response
        return True
Exemple #6
0
    def interfaces(self):
        # grab list of interfaces
        response = cmd("nmcli dev")

        # parse response
        interfaces = []
        for line in response.splitlines():
            if "wireless" in line:
                # this line has our interface name in the first column
                interfaces.append(line.split()[0])

        # return list
        return interfaces
Exemple #7
0
    def interfaces(self):
        # grab list of interfaces
        response = cmd("iwconfig")

        # parse response
        interfaces = []
        for line in response.splitlines():
            if len(line) > 0 and not line.startswith(" "):
                # this line contains an interface name!
                if "no wireless extensions" not in line:
                    # this is a wireless interface
                    interfaces.append(line.split()[0])

        # return list
        return interfaces
Exemple #8
0
    def current(self):
        # get interface status
        response = cmd("iwconfig {}".format(self.interface()))

        # the current network is on the first line.
        # ex: wlan0     IEEE 802.11AC  ESSID:"SSID"  Nickname:"<WIFI@REALTEK>"
        line = response.splitlines()[0]
        match = re.search('ESSID:"(.+?)"', line)
        if match is not None:
            network = match.group(1)
            if network != "off/any":
                return network

        # return none if there was not an active connection
        return None
Exemple #9
0
 def setUp(self):
     self.com = cmd('echo "test_ok"')
     self.empty_com = cmd("echo -n")