def test_ip_info(self, nic0): nic0_interface = Interface.from_existing_dev_name(nic0) with waitfor.waitfor_ipv4_addr(nic0, address=IPV4_ADDR1_CIDR): nic0_interface.add_ip(IPV4_ADDR1, IPV4_PREFIX_LENGTH, IpFamily.IPv4) with waitfor.waitfor_ipv4_addr(nic0, address=IPV4_ADDR2_CIDR): nic0_interface.add_ip(IPV4_ADDR2, IPV4_PREFIX_LENGTH, IpFamily.IPv4) with waitfor.waitfor_ipv6_addr(nic0, address=IPV6_ADDR_CIDR): nic0_interface.add_ip(IPV6_ADDR, IPV6_PREFIX_LENGTH, IpFamily.IPv6) # 32 bit addresses are reported slashless by netlink with waitfor.waitfor_ipv4_addr(nic0, address=IPV4_ADDR3): nic0_interface.add_ip(IPV4_ADDR3, 32, IpFamily.IPv4) assert addresses.getIpInfo(nic0) == ( IPV4_ADDR1, IPV4_NETMASK, [IPV4_ADDR1_CIDR, IPV4_ADDR2_CIDR, IPV4_ADDR3_CIDR], [IPV6_ADDR_CIDR], ) assert addresses.getIpInfo(nic0, ipv4_gateway=IPV4_GATEWAY1) == ( IPV4_ADDR1, IPV4_NETMASK, [IPV4_ADDR1_CIDR, IPV4_ADDR2_CIDR, IPV4_ADDR3_CIDR], [IPV6_ADDR_CIDR], ) assert addresses.getIpInfo(nic0, ipv4_gateway=IPV4_GATEWAY2) == ( IPV4_ADDR2, IPV4_NETMASK, [IPV4_ADDR1_CIDR, IPV4_ADDR2_CIDR, IPV4_ADDR3_CIDR], [IPV6_ADDR_CIDR], )
def test_ip_info(self, nic0): with waitfor.waitfor_ipv4_addr(nic0, address=IPV4_ADDR1_CIDR): ipwrapper.addrAdd(nic0, IPV4_ADDR1, IPV4_PREFIX_LENGTH) with waitfor.waitfor_ipv4_addr(nic0, address=IPV4_ADDR2_CIDR): ipwrapper.addrAdd(nic0, IPV4_ADDR2, IPV4_PREFIX_LENGTH) with waitfor.waitfor_ipv6_addr(nic0, address=IPV6_ADDR_CIDR): ipwrapper.addrAdd(nic0, IPV6_ADDR, IPV6_PREFIX_LENGTH, family=6) # 32 bit addresses are reported slashless by netlink with waitfor.waitfor_ipv4_addr(nic0, address=IPV4_ADDR3): ipwrapper.addrAdd(nic0, IPV4_ADDR3, 32) assert addresses.getIpInfo(nic0) == ( IPV4_ADDR1, IPV4_NETMASK, [IPV4_ADDR1_CIDR, IPV4_ADDR2_CIDR, IPV4_ADDR3_CIDR], [IPV6_ADDR_CIDR], ) assert addresses.getIpInfo(nic0, ipv4_gateway=IPV4_GATEWAY1) == ( IPV4_ADDR1, IPV4_NETMASK, [IPV4_ADDR1_CIDR, IPV4_ADDR2_CIDR, IPV4_ADDR3_CIDR], [IPV6_ADDR_CIDR], ) assert addresses.getIpInfo(nic0, ipv4_gateway=IPV4_GATEWAY2) == ( IPV4_ADDR2, IPV4_NETMASK, [IPV4_ADDR1_CIDR, IPV4_ADDR2_CIDR, IPV4_ADDR3_CIDR], [IPV6_ADDR_CIDR], )
def test_ip_info(self): IPV4_ADDR1 = '192.168.99.2' IPV4_GATEWAY1 = '192.168.99.1' IPV4_ADDR2 = '192.168.199.2' IPV4_GATEWAY2 = '192.168.199.1' IPV4_ADDR3 = '192.168.200.2' IPV4_NETMASK = '255.255.255.0' IPV4_PREFIX_LENGTH = 24 IPV6_ADDR = '2607:f0d0:1002:51::4' IPV6_PREFIX_LENGTH = 64 IPV4_ADDR1_CIDR = self._cidr_form(IPV4_ADDR1, IPV4_PREFIX_LENGTH) IPV4_ADDR2_CIDR = self._cidr_form(IPV4_ADDR2, IPV4_PREFIX_LENGTH) IPV4_ADDR3_CIDR = self._cidr_form(IPV4_ADDR3, 32) IPV6_ADDR_CIDR = self._cidr_form(IPV6_ADDR, IPV6_PREFIX_LENGTH) with dummy_device() as device: with waitfor.waitfor_ipv4_addr(device, address=IPV4_ADDR1_CIDR): ipwrapper.addrAdd(device, IPV4_ADDR1, IPV4_PREFIX_LENGTH) with waitfor.waitfor_ipv4_addr(device, address=IPV4_ADDR2_CIDR): ipwrapper.addrAdd(device, IPV4_ADDR2, IPV4_PREFIX_LENGTH) with waitfor.waitfor_ipv6_addr(device, address=IPV6_ADDR_CIDR): ipwrapper.addrAdd(device, IPV6_ADDR, IPV6_PREFIX_LENGTH, family=6) # 32 bit addresses are reported slashless by netlink with waitfor.waitfor_ipv4_addr(device, address=IPV4_ADDR3): ipwrapper.addrAdd(device, IPV4_ADDR3, 32) self.assertEqual( addresses.getIpInfo(device), ( IPV4_ADDR1, IPV4_NETMASK, [IPV4_ADDR1_CIDR, IPV4_ADDR2_CIDR, IPV4_ADDR3_CIDR], [IPV6_ADDR_CIDR], ), ) self.assertEqual( addresses.getIpInfo(device, ipv4_gateway=IPV4_GATEWAY1), ( IPV4_ADDR1, IPV4_NETMASK, [IPV4_ADDR1_CIDR, IPV4_ADDR2_CIDR, IPV4_ADDR3_CIDR], [IPV6_ADDR_CIDR], ), ) self.assertEqual( addresses.getIpInfo(device, ipv4_gateway=IPV4_GATEWAY2), ( IPV4_ADDR2, IPV4_NETMASK, [IPV4_ADDR1_CIDR, IPV4_ADDR2_CIDR, IPV4_ADDR3_CIDR], [IPV6_ADDR_CIDR], ), )
def test_netinfo_ignoring_link_scope_ip(self): v4_link = { 'family': 'inet', 'address': '169.254.0.0/16', 'scope': 'link', 'prefixlen': 16, 'flags': ['permanent'] } v4_global = { 'family': 'inet', 'address': '192.0.2.2/24', 'scope': 'global', 'prefixlen': 24, 'flags': ['permanent'] } v6_link = { 'family': 'inet6', 'address': 'fe80::5054:ff:fea3:f9f3/64', 'scope': 'link', 'prefixlen': 64, 'flags': ['permanent'] } v6_global = { 'family': 'inet6', 'address': 'ee80::5054:ff:fea3:f9f3/64', 'scope': 'global', 'prefixlen': 64, 'flags': ['permanent'] } ipaddrs = {'eth0': (v4_link, v4_global, v6_link, v6_global)} ipv4addr, ipv4netmask, ipv4addrs, ipv6addrs = \ addresses.getIpInfo('eth0', ipaddrs=ipaddrs) self.assertEqual(ipv4addrs, ['192.0.2.2/24']) self.assertEqual(ipv6addrs, ['ee80::5054:ff:fea3:f9f3/64'])
def test_ip_info(self): IPV4_ADDR1 = '192.168.99.2' IPV4_GATEWAY1 = '192.168.99.1' IPV4_ADDR2 = '192.168.199.2' IPV4_GATEWAY2 = '192.168.199.1' IPV4_ADDR3 = '192.168.200.2' IPV4_NETMASK = '255.255.255.0' IPV4_PREFIX_LENGTH = 24 IPV6_ADDR = '2607:f0d0:1002:51::4' IPV6_PREFIX_LENGTH = 64 IPV4_ADDR1_CIDR = self._cidr_form(IPV4_ADDR1, IPV4_PREFIX_LENGTH) IPV4_ADDR2_CIDR = self._cidr_form(IPV4_ADDR2, IPV4_PREFIX_LENGTH) IPV4_ADDR3_CIDR = self._cidr_form(IPV4_ADDR3, 32) IPV6_ADDR_CIDR = self._cidr_form(IPV6_ADDR, IPV6_PREFIX_LENGTH) with dummy_device() as device: with waitfor.waitfor_ipv4_addr(device, address=IPV4_ADDR1_CIDR): ipwrapper.addrAdd(device, IPV4_ADDR1, IPV4_PREFIX_LENGTH) with waitfor.waitfor_ipv4_addr(device, address=IPV4_ADDR2_CIDR): ipwrapper.addrAdd(device, IPV4_ADDR2, IPV4_PREFIX_LENGTH) with waitfor.waitfor_ipv6_addr(device, address=IPV6_ADDR_CIDR): ipwrapper.addrAdd( device, IPV6_ADDR, IPV6_PREFIX_LENGTH, family=6) # 32 bit addresses are reported slashless by netlink with waitfor.waitfor_ipv4_addr(device, address=IPV4_ADDR3): ipwrapper.addrAdd(device, IPV4_ADDR3, 32) self.assertEqual( addresses.getIpInfo(device), (IPV4_ADDR1, IPV4_NETMASK, [IPV4_ADDR1_CIDR, IPV4_ADDR2_CIDR, IPV4_ADDR3_CIDR], [IPV6_ADDR_CIDR])) self.assertEqual( addresses.getIpInfo(device, ipv4_gateway=IPV4_GATEWAY1), (IPV4_ADDR1, IPV4_NETMASK, [IPV4_ADDR1_CIDR, IPV4_ADDR2_CIDR, IPV4_ADDR3_CIDR], [IPV6_ADDR_CIDR])) self.assertEqual( addresses.getIpInfo(device, ipv4_gateway=IPV4_GATEWAY2), (IPV4_ADDR2, IPV4_NETMASK, [IPV4_ADDR1_CIDR, IPV4_ADDR2_CIDR, IPV4_ADDR3_CIDR], [IPV6_ADDR_CIDR]))
def _get_iface_info(iface, addresses, routes): ipv4gateway = get_gateway(routes, iface, family=4) ipv4addr, ipv4netmask, ipv4addrs, ipv6addrs = getIpInfo( iface, addresses, ipv4gateway) is_dhcpv4, is_dhcpv6 = dhcp_status(iface, addresses) return {'mtu': getMtu(iface), 'addr': ipv4addr, 'ipv4addrs': ipv4addrs, 'gateway': ipv4gateway, 'netmask': ipv4netmask, 'dhcpv4': is_dhcpv4, 'ipv6addrs': ipv6addrs, 'ipv6gateway': get_gateway(routes, iface, family=6), 'ipv6autoconf': is_ipv6_local_auto(iface), 'dhcpv6': is_dhcpv6}
def _get_iface_info(iface, addresses, routes): ipv4gateway = get_gateway(routes, iface, family=4) ipv4addr, ipv4netmask, ipv4addrs, ipv6addrs = getIpInfo( iface, addresses, ipv4gateway) is_dhcpv4 = dhclient.is_active(iface, family=4) is_dhcpv6 = dhclient.is_active(iface, family=6) mtu = iflink(iface).mtu() return {'mtu': mtu, 'addr': ipv4addr, 'ipv4addrs': ipv4addrs, 'gateway': ipv4gateway, 'netmask': ipv4netmask, 'ipv4defaultroute': is_default_route(ipv4gateway, routes), 'dhcpv4': is_dhcpv4, 'ipv6addrs': ipv6addrs, 'ipv6gateway': get_gateway(routes, iface, family=6), 'ipv6autoconf': is_ipv6_local_auto(iface), 'dhcpv6': is_dhcpv6}
def _get_iface_info(iface, addresses, routes): ipv4gateway = get_gateway(routes, iface, family=4) ipv4addr, ipv4netmask, ipv4addrs, ipv6addrs = getIpInfo( iface, addresses, ipv4gateway) is_dhcpv4 = dhclient.is_active(iface, family=4) is_dhcpv6 = dhclient.is_active(iface, family=6) return {'mtu': get_mtu(iface), 'addr': ipv4addr, 'ipv4addrs': ipv4addrs, 'gateway': ipv4gateway, 'netmask': ipv4netmask, 'ipv4defaultroute': is_default_route(ipv4gateway), 'dhcpv4': is_dhcpv4, 'ipv6addrs': ipv6addrs, 'ipv6gateway': get_gateway(routes, iface, family=6), 'ipv6autoconf': is_ipv6_local_auto(iface), 'dhcpv6': is_dhcpv6}
def test_netinfo_ignoring_link_scope_ip(self): v4_link = {'family': 'inet', 'address': '169.254.0.0/16', 'scope': 'link', 'prefixlen': 16, 'flags': ['permanent']} v4_global = {'family': 'inet', 'address': '192.0.2.2/24', 'scope': 'global', 'prefixlen': 24, 'flags': ['permanent']} v6_link = {'family': 'inet6', 'address': 'fe80::5054:ff:fea3:f9f3/64', 'scope': 'link', 'prefixlen': 64, 'flags': ['permanent']} v6_global = {'family': 'inet6', 'address': 'ee80::5054:ff:fea3:f9f3/64', 'scope': 'global', 'prefixlen': 64, 'flags': ['permanent']} ipaddrs = {'eth0': (v4_link, v4_global, v6_link, v6_global)} ipv4addr, ipv4netmask, ipv4addrs, ipv6addrs = \ addresses.getIpInfo('eth0', ipaddrs=ipaddrs) self.assertEqual(ipv4addrs, ['192.0.2.2/24']) self.assertEqual(ipv6addrs, ['ee80::5054:ff:fea3:f9f3/64'])
def _get_iface_info(iface, addresses, routes): ipv4gateway = get_gateway(routes, iface, family=4) ipv4addr, ipv4netmask, ipv4addrs, ipv6addrs = getIpInfo( iface, addresses, ipv4gateway) is_dhcpv4, is_dhcpv6 = dhcp_status(iface, addresses) return { 'mtu': getMtu(iface), 'addr': ipv4addr, 'ipv4addrs': ipv4addrs, 'gateway': ipv4gateway, 'netmask': ipv4netmask, 'dhcpv4': is_dhcpv4, 'ipv6addrs': ipv6addrs, 'ipv6gateway': get_gateway(routes, iface, family=6), 'ipv6autoconf': is_ipv6_local_auto(iface), 'dhcpv6': is_dhcpv6 }
def _get_net_info(interface, routes): mtu = mtus.getMtu(interface) ipaddrs = addresses.getIpAddrs() addr, netmask, ipv4addrs, ipv6addrs = addresses.getIpInfo(interface, ipaddrs) dhcpv4, dhcpv6 = dhcp.dhcp_status(interface, ipaddrs) gateway = netinfo_routes.get_gateway(routes, interface) ipv6gateway = netinfo_routes.get_gateway(routes, interface, family=6) return { 'mtu': str(mtu), 'addr': addr, 'gateway': gateway, 'netmask': netmask, 'dhcpv4': dhcpv4, 'ipv4addrs': ipv4addrs, 'ipv6addrs': ipv6addrs, 'ipv6autoconf': addresses.is_ipv6_local_auto(interface), 'ipv6gateway': ipv6gateway, 'dhcpv6': dhcpv6}
def _get_net_info(interface, routes): mtu = mtus.getMtu(interface) ipaddrs = addresses.getIpAddrs() addr, netmask, ipv4addrs, ipv6addrs = addresses.getIpInfo(interface, ipaddrs) dhcpv4 = dhclient.is_active(interface, family=4) dhcpv6 = dhclient.is_active(interface, family=6) gateway = netinfo_routes.get_gateway(routes, interface) ipv6gateway = netinfo_routes.get_gateway(routes, interface, family=6) return { 'mtu': str(mtu), 'addr': addr, 'gateway': gateway, 'netmask': netmask, 'dhcpv4': dhcpv4, 'ipv4addrs': ipv4addrs, 'ipv6addrs': ipv6addrs, 'ipv6autoconf': addresses.is_ipv6_local_auto(interface), 'ipv6gateway': ipv6gateway, 'dhcpv6': dhcpv6, 'ipv4defaultroute': netinfo_routes.is_default_route(gateway)}
def _get_net_info(interface, routes): mtu = mtus.getMtu(interface) ipaddrs = addresses.getIpAddrs() addr, netmask, ipv4addrs, ipv6addrs = addresses.getIpInfo( interface, ipaddrs) dhcpv4 = dhclient.is_active(interface, family=4) dhcpv6 = dhclient.is_active(interface, family=6) gateway = netinfo_routes.get_gateway(routes, interface) ipv6gateway = netinfo_routes.get_gateway(routes, interface, family=6) return { 'mtu': str(mtu), 'addr': addr, 'gateway': gateway, 'netmask': netmask, 'dhcpv4': dhcpv4, 'ipv4addrs': ipv4addrs, 'ipv6addrs': ipv6addrs, 'ipv6autoconf': addresses.is_ipv6_local_auto(interface), 'ipv6gateway': ipv6gateway, 'dhcpv6': dhcpv6 }
def addrs_info(dev, ipaddrs=None, ipv4_gateway=None): return addresses.getIpInfo(dev, ipaddrs, ipv4_gateway)
def get_ip_info(*a, **kw): """filter away ipv6 link local addresses that may or may not exist on the device depending on OS configuration""" ipv4addr, ipv4netmask, ipv4addrs, ipv6addrs = \ addresses.getIpInfo(*a, **kw) return ipv4addr, ipv4netmask, ipv4addrs, ipv6addrs