def test_create_network_interface(self):
        network_interface = NetworkInterface(0, "eth0")

        assert isinstance(network_interface, NetworkInterface)

        self.assertEqual(Status.unknown, network_interface.status)

        network_interface.mac = "d8:31:26:45:56:2f"

        network_interface.add_ip_address("192.168.1.1", 24)
        network_interface.add_ip_address("fe80::a11:96ff:fe05:3024", 64)

        network_interface.status = Status.up

        network_interface.wifi_information = self._get_wifi_informations()

        self.assertEqual("eth0", network_interface.name)
        self.assertEqual("d8:31:26:45:56:2f", network_interface.mac)
        self.assertEqual(Status.up, network_interface.status)
        self.assertEqual(ipaddress.ip_interface("192.168.1.1/24"),
                         network_interface.ipaddress_lst[0])
        self.assertEqual(ipaddress.ip_interface("fe80::a11:96ff:fe05:3024/64"),
                         network_interface.ipaddress_lst[1])
        self.assertEqual("0x1", network_interface.wifi_information.wdev)
        self.assertEqual(WlanType.managed,
                         network_interface.wifi_information.type)
        self.assertEqual(1, network_interface.wifi_information.channel)
        self.assertEqual(20, network_interface.wifi_information.channel_width)
        self.assertEqual(2412,
                         network_interface.wifi_information.channel_center1)
    def test_create_network_interface(self):
        network_interface = NetworkInterface(0, "eth0")

        assert isinstance(network_interface, NetworkInterface)

        self.assertEqual(Status.unknown, network_interface.status)

        network_interface.mac = "d8:31:26:45:56:2f"

        network_interface.add_ip_address("192.168.1.1", 24)
        network_interface.add_ip_address("fe80::a11:96ff:fe05:3024", 64)

        network_interface.status = Status.up

        network_interface.wifi_information = self._get_wifi_informations()

        self.assertEqual("eth0", network_interface.name)
        self.assertEqual("d8:31:26:45:56:2f", network_interface.mac)
        self.assertEqual(Status.up, network_interface.status)
        self.assertEqual(ipaddress.ip_interface("192.168.1.1/24"), network_interface.ipaddress_lst[0])
        self.assertEqual(ipaddress.ip_interface("fe80::a11:96ff:fe05:3024/64"), network_interface.ipaddress_lst[1])
        self.assertEqual("0x1", network_interface.wifi_information.wdev)
        self.assertEqual(WlanType.managed, network_interface.wifi_information.type)
        self.assertEqual(1, network_interface.wifi_information.channel)
        self.assertEqual(20, network_interface.wifi_information.channel_width)
        self.assertEqual(2412, network_interface.wifi_information.channel_center1)
Пример #3
0
    def _get_router_network_interfaces(self) -> Dict:
        """
        :return: the network interfaces of the given Router object
        """
        interfaces = dict()
        # Get all network interfaces
        raw_iface_lst = self.network_ctrl.send_command("ip a | grep '^[0-9]*:*:'")
        iface_names_lst = list()
        iface_id_lst = list()

        # Get only the wifi interfaces
        raw_wifi_inface_name_lst = self.network_ctrl.send_command("iw dev | grep Interface")
        wifi_iface_name_lst = list()
        for raw_wifi_iface_name in raw_wifi_inface_name_lst:
            wifi_iface_name_lst.append(raw_wifi_iface_name.split("Interface ")[1].replace("\n", ""))

        # transform a line tmp:
        # '2: enp0s25: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc fq_codel state DOWN group default qlen 1000'
        # into '2'; 'enp0s25' and lists it.
        for raw_iface in raw_iface_lst:
            # Iface Id
            iface_id = int(raw_iface.split(":")[0])
            if iface_id_lst.count(iface_id) == 0:
                iface_id_lst.append(iface_id)

            # Iface Name
            iface_name = raw_iface.split(":")[1].replace(" ", "")
            if "@" in iface_name:
                iface_name = iface_name.split("@")[0]
            if iface_names_lst.count(iface_name) == 0:
                iface_names_lst.append(iface_name)

        # Take the information of the interfaces separately
        for i, iface_id in enumerate(iface_id_lst):
            iface_name = iface_names_lst[i]
            interface = NetworkInterface(iface_id, iface_name)
            raw_iface_info_lst = self.network_ctrl.send_command("ip addr show " + iface_name)
            for raw_iface_info in raw_iface_info_lst:
                # Status
                raw_status = raw_iface_info.split("state")
                if len(raw_status) > 1:
                    status = raw_status[1].split(" ")[1]
                    # Status: UP | DOWN | UNKNOWN
                    if status == "UP":
                        interface.status = Status.up
                    elif status == "DOWN":
                        interface.status = Status.down
                    else:
                        interface.status = Status.unknown

                # MAC
                raw_mac = raw_iface_info.split("link/ether")
                if len(raw_mac) > 1:
                    raw_mac = raw_mac[1].split()
                    interface.mac = raw_mac[0]

                # IP Address
                raw_ip = raw_iface_info.split("inet")
                if len(raw_ip) > 1:
                    raw_ip = raw_ip[1].split(" ")[1].split("/")
                    interface.add_ip_address(raw_ip[0], int(raw_ip[1]))

                # Wifi information
                if wifi_iface_name_lst.count(iface_name) != 0:
                    interface.wifi_information = self.__get_wifi_information(iface_name)
            interfaces[iface_name] = interface
        return interfaces
Пример #4
0
    def _get_router_network_interfaces(self) -> Dict:
        """
        :return: the network interfaces of the given Router object
        """
        interfaces = dict()
        # Get all network interfaces
        raw_iface_lst = self.network_ctrl.send_command(
            "ip a | grep '^[0-9]*:*:'")
        iface_names_lst = list()
        iface_id_lst = list()

        # Get only the wifi interfaces
        raw_wifi_inface_name_lst = self.network_ctrl.send_command(
            "iw dev | grep Interface")
        wifi_iface_name_lst = list()
        for raw_wifi_iface_name in raw_wifi_inface_name_lst:
            wifi_iface_name_lst.append(
                raw_wifi_iface_name.split("Interface ")[1].replace("\n", ""))

        # transform a line tmp:
        # '2: enp0s25: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc fq_codel state DOWN group default qlen 1000'
        # into '2'; 'enp0s25' and lists it.
        for raw_iface in raw_iface_lst:
            # Iface Id
            iface_id = int(raw_iface.split(":")[0])
            if iface_id_lst.count(iface_id) == 0:
                iface_id_lst.append(iface_id)

            # Iface Name
            iface_name = raw_iface.split(":")[1].replace(" ", "")
            if "@" in iface_name:
                iface_name = iface_name.split("@")[0]
            if iface_names_lst.count(iface_name) == 0:
                iface_names_lst.append(iface_name)

        # Take the information of the interfaces separately
        for i, iface_id in enumerate(iface_id_lst):
            iface_name = iface_names_lst[i]
            interface = NetworkInterface(iface_id, iface_name)
            raw_iface_info_lst = self.network_ctrl.send_command(
                "ip addr show " + iface_name)
            for raw_iface_info in raw_iface_info_lst:
                # Status
                raw_status = raw_iface_info.split("state")
                if len(raw_status) > 1:
                    status = raw_status[1].split(" ")[1]
                    # Status: UP | DOWN | UNKNOWN
                    if status == "UP":
                        interface.status = Status.up
                    elif status == "DOWN":
                        interface.status = Status.down
                    else:
                        interface.status = Status.unknown

                # MAC
                raw_mac = raw_iface_info.split("link/ether")
                if len(raw_mac) > 1:
                    raw_mac = raw_mac[1].split()
                    interface.mac = raw_mac[0]

                # IP Address
                raw_ip = raw_iface_info.split("inet")
                if len(raw_ip) > 1:
                    raw_ip = raw_ip[1].split(" ")[1].split("/")
                    interface.add_ip_address(raw_ip[0], int(raw_ip[1]))

                # Wifi information
                if wifi_iface_name_lst.count(iface_name) != 0:
                    interface.wifi_information = self.__get_wifi_information(
                        iface_name)
            interfaces[iface_name] = interface
        return interfaces