def _activated_device_msg(self, devname): msg = _("Wired (%(interface_name)s) connected\n") \ % {"interface_name": devname} device = self.nm_client.get_device_by_iface(devname) if device: addr_str = dnss_str = gateway_str = netmask_str = "" ipv4config = device.get_ip4_config() if ipv4config: addresses = ipv4config.get_addresses() if addresses: a0 = addresses[0] addr_str = a0.get_address() prefix = a0.get_prefix() netmask_str = network.prefix_to_netmask(prefix) gateway_str = ipv4config.get_gateway() or '' dnss_str = ",".join(ipv4config.get_nameservers()) msg += _(" IPv4 Address: %(addr)s Netmask: %(netmask)s Gateway: %(gateway)s\n") % \ {"addr": addr_str, "netmask": netmask_str, "gateway": gateway_str} msg += _(" DNS: %s\n") % dnss_str ipv6config = device.get_ip6_config() if ipv6config: for address in ipv6config.get_addresses(): addr_str = address.get_address() prefix = address.get_prefix() # Do not display link-local addresses if not addr_str.startswith("fe80:"): msg += _(" IPv6 Address: %(addr)s/%(prefix)d\n") % \ {"addr": addr_str, "prefix": prefix} return msg
def _activated_device_msg(self, devname): msg = _("Wired (%(interface_name)s) connected\n") \ % {"interface_name": devname} device = self.nm_client.get_device_by_iface(devname) if device: ipv4config = device.get_ip4_config() if ipv4config: addresses = ipv4config.get_addresses() if addresses: a0 = addresses[0] addr_str = a0.get_address() prefix = a0.get_prefix() netmask_str = network.prefix_to_netmask(prefix) gateway_str = ipv4config.get_gateway() or '' dnss_str = ",".join(ipv4config.get_nameservers()) else: addr_str = dnss_str = gateway_str = netmask_str = "" msg += _(" IPv4 Address: %(addr)s Netmask: %(netmask)s Gateway: %(gateway)s\n") % \ {"addr": addr_str, "netmask": netmask_str, "gateway": gateway_str} msg += _(" DNS: %s\n") % dnss_str ipv6config = device.get_ip6_config() if ipv6config: for address in ipv6config.get_addresses(): addr_str = address.get_address() prefix = address.get_prefix() # Do not display link-local addresses if not addr_str.startswith("fe80:"): msg += _(" IPv6 Address: %(addr)s/%(prefix)d\n") % \ {"addr": addr_str, "prefix": prefix} return msg
def set_from_connection(self, connection): """Set the object from NM RemoteConnection. :param connection: connection to be used to set the object :type connection: NM.RemoteConnection """ connection_uuid = connection.get_uuid() ip4_config = connection.get_setting_ip4_config() ip4_method = ip4_config.get_method() if ip4_method == NM.SETTING_IP4_CONFIG_METHOD_AUTO: self.ip = "dhcp" elif ip4_method == NM.SETTING_IP4_CONFIG_METHOD_MANUAL: if ip4_config.get_num_addresses() > 0: addr = ip4_config.get_address(0) self.ip = addr.get_address() self.netmask = network.prefix_to_netmask(addr.get_prefix()) else: log.error("No ip4 address found for manual method in %s", connection_uuid) elif ip4_method == NM.SETTING_IP4_CONFIG_METHOD_DISABLED: self.ip = "" else: log.error("Unexpected ipv4 method %s found in connection %s", ip4_method, connection_uuid) self.ip = "dhcp" self.gateway = ip4_config.get_gateway() or "" ip6_config = connection.get_setting_ip6_config() self.ipv6addrgenmode = ip6_config.get_addr_gen_mode() ip6_method = ip6_config.get_method() if ip6_method == NM.SETTING_IP6_CONFIG_METHOD_AUTO: self.ipv6 = "auto" elif ip6_method == NM.SETTING_IP6_CONFIG_METHOD_IGNORE: self.ipv6 = "ignore" elif ip6_method == NM.SETTING_IP6_CONFIG_METHOD_DHCP: self.ipv6 = "dhcp" elif ip6_method == NM.SETTING_IP6_CONFIG_METHOD_MANUAL: if ip6_config.get_num_addresses() > 0: addr = ip6_config.get_address(0) self.ipv6 = "{}/{}".format(addr.get_address(), addr.get_prefix()) else: log.error("No ip6 address found for manual method in %s", connection_uuid) else: log.error("Unexpected ipv6 method %s found in connection %s", ip6_method, connection_uuid) self.ipv6 = "auto" self.ipv6gateway = ip6_config.get_gateway() or "" nameservers = [] for i in range(0, ip4_config.get_num_dns()): nameservers.append(ip4_config.get_dns(i)) for i in range(0, ip6_config.get_num_dns()): nameservers.append(ip6_config.get_dns(i)) self.nameserver = ",".join(nameservers) self.onboot = connection.get_setting_connection().get_autoconnect()
def test_prefix2netmask2prefix(self): """Test netmask and prefix conversion.""" lore = [ (0, "0.0.0.0"), (1, "128.0.0.0"), (2, "192.0.0.0"), (3, "224.0.0.0"), (4, "240.0.0.0"), (5, "248.0.0.0"), (6, "252.0.0.0"), (7, "254.0.0.0"), (8, "255.0.0.0"), (9, "255.128.0.0"), (10, "255.192.0.0"), (11, "255.224.0.0"), (12, "255.240.0.0"), (13, "255.248.0.0"), (14, "255.252.0.0"), (15, "255.254.0.0"), (16, "255.255.0.0"), (17, "255.255.128.0"), (18, "255.255.192.0"), (19, "255.255.224.0"), (20, "255.255.240.0"), (21, "255.255.248.0"), (22, "255.255.252.0"), (23, "255.255.254.0"), (24, "255.255.255.0"), (25, "255.255.255.128"), (26, "255.255.255.192"), (27, "255.255.255.224"), (28, "255.255.255.240"), (29, "255.255.255.248"), (30, "255.255.255.252"), (31, "255.255.255.254"), (32, "255.255.255.255"), ] for prefix, netmask in lore: assert network.prefix_to_netmask(prefix) == netmask assert network.netmask_to_prefix(netmask) == prefix assert network.prefix_to_netmask(33) == "255.255.255.255"
def prefix2netmask2prefix_test(self): lore = [ (0, "0.0.0.0"), (1, "128.0.0.0"), (2, "192.0.0.0"), (3, "224.0.0.0"), (4, "240.0.0.0"), (5, "248.0.0.0"), (6, "252.0.0.0"), (7, "254.0.0.0"), (8, "255.0.0.0"), (9, "255.128.0.0"), (10, "255.192.0.0"), (11, "255.224.0.0"), (12, "255.240.0.0"), (13, "255.248.0.0"), (14, "255.252.0.0"), (15, "255.254.0.0"), (16, "255.255.0.0"), (17, "255.255.128.0"), (18, "255.255.192.0"), (19, "255.255.224.0"), (20, "255.255.240.0"), (21, "255.255.248.0"), (22, "255.255.252.0"), (23, "255.255.254.0"), (24, "255.255.255.0"), (25, "255.255.255.128"), (26, "255.255.255.192"), (27, "255.255.255.224"), (28, "255.255.255.240"), (29, "255.255.255.248"), (30, "255.255.255.252"), (31, "255.255.255.254"), (32, "255.255.255.255"), ] for prefix, netmask in lore: self.assertEqual(network.prefix_to_netmask(prefix), netmask) self.assertEqual(network.netmask_to_prefix(netmask), prefix) self.assertEqual(network.prefix_to_netmask(33), "255.255.255.255")
def set_from_connection(self, connection): """Set the object from NM RemoteConnection. :param connection: connection to be used to set the object :type connection: NM.RemoteConnection """ connection_uuid = connection.get_uuid() ip4_config = connection.get_setting_ip4_config() ip4_method = ip4_config.get_method() if ip4_method == NM.SETTING_IP4_CONFIG_METHOD_AUTO: self.ip = "dhcp" elif ip4_method == NM.SETTING_IP4_CONFIG_METHOD_MANUAL: if ip4_config.get_num_addresses() > 0: addr = ip4_config.get_address(0) self.ip = addr.get_address() self.netmask = network.prefix_to_netmask(addr.get_prefix()) else: log.error("No ip4 address found for manual method in %s", connection_uuid) elif ip4_method == NM.SETTING_IP4_CONFIG_METHOD_DISABLED: self.ip = "" else: log.error("Unexpected ipv4 method %s found in connection %s", ip4_method, connection_uuid) self.ip = "dhcp" self.gateway = ip4_config.get_gateway() or "" ip6_config = connection.get_setting_ip6_config() ip6_method = ip6_config.get_method() if ip6_method == NM.SETTING_IP6_CONFIG_METHOD_AUTO: self.ipv6 = "auto" elif ip6_method == NM.SETTING_IP6_CONFIG_METHOD_IGNORE: self.ipv6 = "ignore" elif ip6_method == NM.SETTING_IP6_CONFIG_METHOD_DHCP: self.ipv6 = "dhcp" elif ip6_method == NM.SETTING_IP6_CONFIG_METHOD_MANUAL: if ip6_config.get_num_addresses() > 0: addr = ip6_config.get_address(0) self.ipv6 = "{}/{}".format(addr.get_address(), addr.get_prefix()) else: log.error("No ip6 address found for manual method in %s", connection_uuid) else: log.error("Unexpected ipv6 method %s found in connection %s", ip6_method, connection_uuid) self.ipv6 = "auto" self.ipv6gateway = ip6_config.get_gateway() or "" nameservers = [] for i in range(0, ip4_config.get_num_dns()): nameservers.append(ip4_config.get_dns(i)) for i in range(0, ip6_config.get_num_dns()): nameservers.append(ip6_config.get_dns(i)) self.nameserver = ",".join(nameservers) self.onboot = connection.get_setting_connection().get_autoconnect()