def test_prep_addr(self, test_case): """Test prep_addr.""" tests = [ ['eth0', 'ipv4', { 'eth0': { 'ipv4': {} } }], ['eth1', 'ipv6', { 'eth1': { 'ipv6': {} } }], ] for t in tests: out = prep_addr({}, t[0], t[1]) assert out == t[2] return {}
def get_interfaces_ip(self): """FTOS implementation of get_interfaces_ip.""" addr = {} # get IPv4 info ip_cmd = "show ip interface" ip_res = self._send_command(ip_cmd) # parse IP addresses iface = None for line in ip_res.splitlines(): # interface line m = re.search(r'^(\w+( \d+(\/\d+)?)?) is \w+', line) if m: # capture interface name and move on to next line iface = m.group(1) continue # look for IPv4 address line m = re.search(r'^Internet address is ([0-9\.]+)(?:\/(\d+))?', line) if not m: continue # prepare address dict for this interface addr = prep_addr(addr, iface) address = ip(m.group(1)) # try to get subnet mask from output as well # otherwise assume /32 mask = 32 if m.group(2): mask = int(m.group(2)) # remove subnet mask from address address = address.replace('/%d' % mask, '') addr[iface][u'ipv4'][address] = { 'prefix_length': mask } ip6_cmd = "show ipv6 interface brief" ip6_res = self._send_command(ip6_cmd) # parse IPv6 addresses iface = None for line in ip6_res.splitlines(): # interface line m = re.search(r'^(\w+( \d+(\/\d+)?)?)\s+', line) if m: # capture interface name and move on to next line iface = m.group(1) continue # look for IPv6 address line m = re.search(r'^\s*([a-f0-9:]+)(?:\/(\d+))?', line) if not m: continue # prepare address dict for this interface addr = prep_addr(addr, iface, u'ipv6') address = ip(m.group(1)) # try to get prefix length from output as well # otherwise assume /128 preflen = 128 if m.group(2): preflen = int(m.group(2)) # remove prefix length from address address = address.replace('/%d' % preflen, '') # for link-local addresses assume prefix length /64 elif re.search(r'^fe80', address): preflen = 64 addr[iface][u'ipv6'][address] = { 'prefix_length': preflen } return addr