コード例 #1
0
    def test_normalise_keys(self):
        """Test replacing v18 keys works as expected."""

        data = {'dest': "10.1.1.11"}

        data = utilities.normalise_keys(data)

        assert data[Constants.MODERN_KEYS["dest"]] == '10.1.1.11'
コード例 #2
0
    def test_normalise_key_stripping(self):
        """Test replacing dots and spaces works."""

        data = {
            ".name": "cfg07ee1",
            ".type": "host",
            "name": "imac-ethernet",
            ".index": 4,
            "mac": "c8:2a:10:4a:10:dd",
            "dns": "1",
            ".anonymous": True,
            "ip": "192.168.1.124"
        }

        data = utilities.normalise_keys(data)

        assert data['_name'] == "cfg07ee1"
        assert data['_type'] == "host"
        assert data['ip'] == "192.168.1.124"
コード例 #3
0
    def test_normalise_keys(self):
        """Test replacing v17 keys works as expected."""

        data = {
            "HW type": "0x1",
            "Mask": "*",
            "Flags": "0x2",
            "Device": "br-lan",
            'HW address': '9C:20:7B:CA:A2:16',
            'IP address': "127.0.0.1",
        }

        data = utilities.normalise_keys(data)

        assert data[Constants.MODERN_KEYS["HW_address"]] == '9C:20:7B:CA:A2:16'
        assert data[Constants.MODERN_KEYS["IP_address"]] == '127.0.0.1'
        assert data['HW_type'] == "0x1"
        assert data['Mask'] == "*"
        assert data['Flags'] == "0x2"
        assert data['Device'] == "br-lan"
コード例 #4
0
    def get_all_connected_devices(self, only_reachable, wlan_interfaces):
        """
        Get details of all connected devices.

        Notes around newer OpenWrt releases (18.06+)

            Do not use `reachable` or `stale` values
            as they flap constantly even
            when the device is inside the network.
            The very existence of the mac in the results
            is enough to determine the "device is home"

        :param only_reachable: boolean, if true,
               only return devices which are reachable
               (this is for 17.06 or earlier only. 18+
               does not have a proper ability to determine
               this, as above)
        """
        log.info("Checking for connected devices")
        last_results = []
        # rpc_sys__winfo_call = Constants.\
        #     LUCI_RPC_SYS_PATH.format(self.host_api_url), \
        #                       'wifi.getiwinfo', wlan_interfaces
        rpc_uci_call = Constants.LUCI_RPC_UCI_PATH.format(
            self.host_api_url), 'get_all', 'dhcp'

        try:
            # First, try find the associated wifi devices
            # winfo_result = self._call_json_rpc(*rpc_sys__winfo_call)
            arp_result = self._call_json_rpc(*self.arp_call)
            dhcp_result = self._call_json_rpc(*rpc_uci_call)
        except InvalidLuciTokenError:
            log.info("Refreshing login token")
            self._refresh_token()
            return self.get_all_connected_devices()

        for device_entry in arp_result:
            device_entry = utilities.normalise_keys(device_entry)

            if "mac" not in device_entry:
                continue

            device_entry['hostname'] = utilities.get_hostname_from_dhcp(
                dhcp_result, device_entry['mac'])

            # As a convenience, add the router IP as the host
            # for every device. Can be useful when a network has more
            # than one router.
            if "host" not in device_entry:
                device_entry['host'] = self.host

            device = namedtuple("Device",
                                device_entry.keys())(*device_entry.values())

            if "Flags" in device_entry and only_reachable:
                # Check if the Flags for each device contain
                # NUD_REACHABLE and if not, skip.
                if not int(device_entry['Flags'], 16) & 0x2:
                    continue

            last_results.append(device)

        log.debug(last_results)
        return last_results
コード例 #5
0
    def test_normalise_key_none_type(self):
        """Test a None result is handled."""

        data = utilities.normalise_keys(None)

        assert data is None