def get_host_interfaces(self, include_namespaces=False): """Returns a list of all interfaces on the host including ones found in namespaces created with ip netns. """ _interfaces = [] _interfaces += self._get_interface_names(cli_helpers.get_ip_addr()) if not include_namespaces: return _interfaces for ns in cli_helpers.get_ip_netns(): ns_name = ns.partition(" ")[0] _interfaces += self._get_interface_names( cli_helpers.get_ip_addr(namespace=ns_name)) return _interfaces
def host_interface_exists(self, iface, check_namespaces=True): _interfaces = self._get_interface_names(cli_helpers.get_ip_addr()) if iface in _interfaces: return True if not check_namespaces: return False for ns in cli_helpers.get_ip_netns(): interfaces = self._get_interface_names( cli_helpers.get_ip_addr(namespace=ns)) if iface in interfaces: return True return False
def get_network_info(self): ip_addr_output = cli_helpers.get_ip_addr() if not ip_addr_output: sys.exit(0) cni_type = "flannel" iface = None for i, line in enumerate(ip_addr_output): if cni_type in line: if cni_type not in NETWORK_INFO: NETWORK_INFO[cni_type] = {} ret = re.compile( r".+({}\.[0-9]+):".format(cni_type)).match(line) if ret: iface = ret[1] NETWORK_INFO[cni_type][iface] = {} continue if iface: ret = re.compile(r".+\s+([0-9\.]+/[0-9]+).+\s+{}$".format( iface)).match(line) if iface in ip_addr_output[i - 3] and ret: NETWORK_INFO[cni_type][iface]["addr"] = ret[1] iface = None ret = re.compile( r"^\s+vxlan id .+\s+(\S+)\s+dev\s+([0-9a-z]+).+").match(line) if cni_type in NETWORK_INFO and ret: iface_info = "{}@{}".format(ret[1], ret[2]) NETWORK_INFO[cni_type][iface]["vxlan"] = iface_info
def test_get_ip_addr(self, mock_subprocess): path = os.path.join(os.environ["DATA_ROOT"], "sos_commands/networking/ip_-d_address") with open(path, 'r') as fd: out = fd.readlines() ret = cli_helpers.get_ip_addr() self.assertEquals(ret, out) self.assertFalse(mock_subprocess.called)
def _find_interface_name_by_ip_address(self, ip_address): """Lookup interface by name in ip addr show""" iface = None lines = cli_helpers.get_ip_addr() a = self._find_line(r".+{}.+".format(ip_address), lines) while True: ret = re.compile(r"^[0-9]+:\s+(\S+):\s+.+").match(lines[a]) if ret: iface = ret[1] break a = a - 1 return iface