def get_listening(self, listen=['0.0.0.0']): """Returns a list of addresses SSH can list on Turns input into a sensible list of IPs SSH can listen on. Input must be a python list of interface names, IPs and/or CIDRs. :param listen: list of IPs, CIDRs, interface names :returns: list of IPs available on the host """ if listen == ['0.0.0.0']: return listen value = [] for network in listen: try: ip = get_address_in_network(network=network, fatal=True) except ValueError: if is_ip(network): ip = network else: try: ip = get_iface_addr(iface=network, fatal=False)[0] except IndexError: continue value.append(ip) if value == []: return ['0.0.0.0'] return value
def test_get_iface_addr_exclist(self, _interfaces, _ifaddresses): _interfaces.return_value = DUMMY_ADDRESSES.keys() _ifaddresses.side_effect = DUMMY_ADDRESSES.__getitem__ result = net_ip.get_iface_addr("eth0", inc_aliases=True, exc_list=['192.168.1.55']) self.assertEqual(['192.168.1.56'], result)
def get_all_addresses(): addresses = [] for iface in interfaces(): if not iface == 'lo': for addr in get_iface_addr(iface=iface, inc_aliases=True, fatal=False): addresses.append(addr) return addresses
def health_manager_bind_ip(cls): """IP address health manager process should bind to. The value is configured individually per unit and reflects the IP address assigned to the specific units tunnel port. :param cls: charms_openstack.adapters.ConfigurationAdapter derived class instance. Charm class instance is at cls.charm_instance. :type: cls: charms_openstack.adapters.ConfiguartionAdapter :returns: IP address of unit local Health Manager interface. :rtype: str """ # -> contrail addition return ch_net_ip.get_host_ip(ch_core.hookenv.network_get("public")["ingress-addresses"][0]) # <- contrail addition ip_list = [] for af in ['AF_INET6', 'AF_INET']: try: ip_list.extend( (ip for ip in ch_net_ip.get_iface_addr(iface=OCTAVIA_MGMT_INTF, inet_type=af) if '%' not in ip)) except Exception: # ch_net_ip.get_iface_addr() throws an exception of type # Exception when the requested interface does not exist or if # it has no addresses in the requested address family. pass if ip_list: return ip_list[0]
def get_ip(): network = config.get("control-network") if network: # try to get ip from CIDR try: return get_address_in_network(network) except Exception: pass # try to get ip from interface name try: return get_iface_addr(network)[0] except Exception: pass return _get_default_ip()
def replace_tmpl(value): # Building here rather than using get_all_addresses # because we need to return a dict and specifically, # ifaces need to have: "interface:NAME" format # Therefore, not very useful for rest of the code addresses = {} for iface in interfaces(): for addr in get_iface_addr(iface=iface, inc_aliases=True, fatal=False): addresses["interface_{}".format(iface)] = addr env = Environment() t = env.from_string(str(value)) # For the moment, we only replace interface names return t.render(**addresses)
def get_ip(config_param="control-network", fallback=None): network = config.get(config_param) if network: # try to get ip from CIDR try: ip = get_address_in_network(network, fatal=True) return ip except Exception: pass # try to get ip from interface name try: return get_iface_addr(network, fatal=True)[0] except Exception: pass return fallback if fallback else _get_default_ip()
def get_mgmt_interface(): ''' Returns the managment interface. ''' mgmt_interface = config('mgmt-interface') if not mgmt_interface: try: return get_iface_from_addr(unit_get('private-address')) except: for bridge_interface in get_bridges(): if (get_host_ip(unit_get('private-address')) in get_iface_addr(bridge_interface)): return bridge_interface elif interface_exists(mgmt_interface): return mgmt_interface else: log('Provided managment interface %s does not exist' % mgmt_interface) return get_iface_from_addr(unit_get('private-address'))
def test_get_iface_addr_invalid_type(self, _interfaces): _interfaces.return_value = DUMMY_ADDRESSES.keys() with nose.tools.assert_raises(Exception): net_ip.get_iface_addr(iface='eth0', inet_type='AF_BOB')
def test_get_iface_addr_full_interface_path(self, _interfaces, _ifaddresses): _interfaces.return_value = DUMMY_ADDRESSES.keys() _ifaddresses.side_effect = DUMMY_ADDRESSES.__getitem__ result = net_ip.get_iface_addr("/dev/eth0") self.assertEqual(["192.168.1.55"], result)
def test_get_iface_addr_mixedaddr(self, _interfaces, _ifaddresses): _interfaces.return_value = DUMMY_ADDRESSES.keys() _ifaddresses.side_effect = DUMMY_ADDRESSES.__getitem__ result = net_ip.get_iface_addr("eth2", inc_aliases=True) self.assertEqual(["192.168.10.58"], result)