Exemple #1
0
    def _get_network_info_tuple(self) -> NetworkInfo:
        def network_interface_gen():
            for interface in netifaces.interfaces():
                try:
                    mac_address = get_if_mac_address(interface)
                except ValueError:
                    mac_address = None

                try:
                    ip_addresses = get_all_ips_from_if_cidr(
                        interface, IpPreference.IPV4_ONLY)
                except ValueError:
                    ip_addresses = []

                try:
                    ipv6_addresses = get_all_ips_from_if_cidr(
                        interface, IpPreference.IPV6_ONLY)
                except ValueError:
                    ipv6_addresses = []

                yield NetworkInterface(
                    network_interface_id=interface,
                    status="UP" if is_interface_up(interface) else "DOWN",
                    mac_address=mac_address,
                    ip_addresses=ip_addresses,
                    ipv6_addresses=ipv6_addresses,
                )

        def make_route_tuple(route) -> Route:
            return Route(
                destination_ip=route.destination,
                gateway_ip=route.gateway,
                genmask=route.genmask,
                network_interface_id=route.interface,
            )

        routing_cmd_result = get_routing_table()
        if routing_cmd_result.error is not None:
            logging.error("Failed to get routing table: %s",
                          routing_cmd_result.error)

        network_info = NetworkInfo(
            network_interfaces=[
                network_interface._asdict()
                for network_interface in network_interface_gen()
            ],
            routing_table=[
                make_route_tuple(route)._asdict()
                for route in routing_cmd_result.routing_table
            ],
        )
        return network_info
Exemple #2
0
    def _get_network_info() -> NetworkInfo:
        def network_interface_gen():
            for interface in netifaces.interfaces():
                try:
                    mac_address = get_if_mac_address(interface)
                except ValueError:
                    mac_address = None

                try:
                    ip_addresses = get_all_ips_from_if_cidr(
                        interface,
                        IpPreference.IPV4_ONLY,
                    )
                except ValueError:
                    ip_addresses = []

                try:
                    ipv6_addresses = get_all_ips_from_if_cidr(
                        interface,
                        IpPreference.IPV6_ONLY,
                    )
                except ValueError:
                    ipv6_addresses = []

                yield NetworkInterface(
                    network_interface_id=interface,
                    status="UP" if is_interface_up(interface) else "DOWN",
                    mac_address=mac_address,
                    ip_addresses=ip_addresses,
                    ipv6_addresses=ipv6_addresses,
                )

        routing_cmd_result = get_routing_table()
        if routing_cmd_result.error is not None:
            logging.error(
                "Failed to get routing table: %s",
                routing_cmd_result.error,
            )

        network_info = NetworkInfo(
            network_interfaces=[
                network_interface._asdict()
                for network_interface in network_interface_gen()
            ],
            routing_table=routing_cmd_result.routing_table,
        )
        return network_info