Skip to content
This repository has been archived by the owner on Jan 16, 2019. It is now read-only.

Get interfaces ip #205

Merged
merged 3 commits into from
Sep 29, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
126 changes: 46 additions & 80 deletions napalm_ios/ios.py
Original file line number Diff line number Diff line change
Expand Up @@ -1028,7 +1028,7 @@ def get_interfaces_ip(self):
'ipv6': { u'1::1': { 'prefix_length': 64},
u'2001:DB8:1::1': { 'prefix_length': 64},
u'2::': { 'prefix_length': 64},
u'FE80::3': { 'prefix_length': u'N/A'}}},
u'FE80::3': { 'prefix_length': 64}}},
u'Tunnel0': { 'ipv4': { u'10.63.100.9': { 'prefix_length': 24}}},
u'Tunnel1': { 'ipv4': { u'10.63.101.9': { 'prefix_length': 24}}},
u'Vlan100': { 'ipv4': { u'10.40.0.1': { 'prefix_length': 24},
Expand All @@ -1038,86 +1038,52 @@ def get_interfaces_ip(self):
"""
interfaces = {}

command = 'show ip interface brief'
output = self._send_command(command)
for line in output.splitlines():
if 'Interface' in line and 'Status' in line:
command = 'show ip interface'
show_ip_interface = self._send_command(command)
command = 'show ipv6 interface'
show_ipv6_interface = self._send_command(command)

INTERNET_ADDRESS = r'\s+(?:Internet address is|Secondary address)'
INTERNET_ADDRESS += r' (?P<ip>{})/(?P<prefix>\d+)'.format(IPV4_ADDR_REGEX)
LINK_LOCAL_ADDRESS = r'\s+IPv6 is enabled, link-local address is (?P<ip>[a-fA-F0-9:]+)'
GLOBAL_ADDRESS = r'\s+(?P<ip>[a-fA-F0-9:]+), subnet is (?:[a-fA-F0-9:]+)/(?P<prefix>\d+)'

interfaces = {}
for line in show_ip_interface.splitlines():
if(len(line.strip()) == 0):
continue
fields = line.split()
if len(fields) >= 3:
interface = fields[0]
else:
raise ValueError("Unexpected response from the router")
interfaces.update({interface: {}})

# Parse IP Address and Subnet Mask from Interfaces
for interface in interfaces:
show_command = "show run interface {0}".format(interface)
interface_output = self._send_command(show_command)
for line in interface_output.splitlines():
if 'ip address ' in line and 'no ip address' not in line:
fields = line.split()
if len(fields) == 3:
# Check for 'ip address dhcp', convert to ip address and mask
if fields[2] == 'dhcp':
cmd = "show interface {} | in Internet address is".format(interface)
show_int = self._send_command(cmd)
if not show_int:
continue
int_fields = show_int.split()
ip_address, subnet = int_fields[3].split(r'/')
interfaces[interface]['ipv4'] = {ip_address: {}}
try:
val = {'prefix_length': int(subnet)}
except ValueError:
val = {'prefix_length': u'N/A'}
interfaces[interface]['ipv4'][ip_address] = val
elif len(fields) in [4, 5]:
# Check for 'ip address 10.10.10.1 255.255.255.0'
# Check for 'ip address 10.10.11.1 255.255.255.0 secondary'
if 'ipv4' not in interfaces[interface].keys():
interfaces[interface].update({'ipv4': {}})
ip_address = fields[2]

try:
subnet = sum([bin(int(x)).count('1') for x in fields[3].split('.')])
except ValueError:
subnet = u'N/A'

ip_dict = {'prefix_length': subnet}
interfaces[interface]['ipv4'].update({ip_address: {}})
interfaces[interface]['ipv4'][ip_address].update(ip_dict)
else:
raise ValueError(u"Unexpected Response from the device")

# Check IPv6
if 'ipv6 address ' in line:
fields = line.split()
ip_address = fields[2]
if 'ipv6' not in interfaces[interface].keys():
interfaces[interface].update({'ipv6': {}})

try:
if r'/' in ip_address:
# check for 'ipv6 address 1::1/64'
ip_address, subnet = ip_address.split(r'/')
interfaces[interface]['ipv6'].update({ip_address: {}})
ip_dict = {'prefix_length': int(subnet)}
else:
# check for 'ipv6 address FE80::3 link-local'
interfaces[interface]['ipv6'].update({ip_address: {}})
ip_dict = {'prefix_length': u'N/A'}

interfaces[interface]['ipv6'][ip_address].update(ip_dict)
except AttributeError:
raise ValueError(u"Unexpected Response from the device")

# remove keys with no data
new_interfaces = {}
for k, val in interfaces.items():
if val:
new_interfaces[k] = val
return new_interfaces
if(line[0] != ' '):
ipv4 = {}
interfaces[line.split()[0]] = {'ipv4': ipv4}
m = re.match(INTERNET_ADDRESS, line)
if m:
ip, prefix = m.groups()
ipv4.update({ip: {"prefix_length": int(prefix)}})

# Remove interfaces without ip
interfaces = dict(filter(lambda x: x[1]['ipv4'] != {}, interfaces.items()))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might this be more readable as

interfaces = {intf: details for intf, details in interfaces.items() if details['ipv4'] != {}}

?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I think I would probably change this slightly to something like:

            if(line[0] != ' '):
                ipv4 = {}
                interface_name = line.split()[0]
            m = re.match(INTERNET_ADDRESS, line)
            if m:
                ip, prefix = m.groups()
                ipv4.update({ip: {"prefix_length": int(prefix)}})
                interfaces[interface_name] = {'ipv4': ipv4}

i.e. only add it to 'interfaces' if there is a positive match


for line in show_ipv6_interface.splitlines():
if(len(line.strip()) == 0):
continue
if(line[0] != ' '):
ifname = line.split()[0]
ipv6 = {}
if ifname not in interfaces:
interfaces[ifname] = {'ipv6': ipv6}
else:
interfaces[ifname].update({'ipv6': ipv6})
m = re.match(LINK_LOCAL_ADDRESS, line)
if m:
ip = m.group(1)
ipv6.update({ip: {"prefix_length": 64}})
m = re.match(GLOBAL_ADDRESS, line)
if m:
ip, prefix = m.groups()
ipv6.update({ip: {"prefix_length": int(prefix)}})

# Interface without ipv6 doesn't appears in show ipv6 interface
return interfaces

@staticmethod
def bgp_time_conversion(bgp_uptime):
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,34 @@
{
"Vlan20": {
"ipv4": {
"172.29.50.3": {
"prefix_length": 27
}
}
},
"Vlan41": {
"ipv4": {
"172.29.52.34": {
"prefix_length": 27
},
"192.168.81.34": {
"prefix_length": 24
}
}
}
"Vlan20": {
"ipv4": {
"172.29.50.3": {
"prefix_length": 27
}
},
"ipv6": {
"FE80::C009:24FF:FEAC:0": {
"prefix_length": 64
},
"2002::1": {
"prefix_length": 64
}
}
},
"Vlan41": {
"ipv4": {
"172.29.52.34": {
"prefix_length": 27
},
"192.168.81.34": {
"prefix_length": 24
}
}
},
"Loopback0": {
"ipv6": {
"FE80::C009:24FF:FEAC:0": {"prefix_length": 64},
"2001::1": {"prefix_length": 128},
"2001::2": {"prefix_length": 128}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
Vlan20 is up, line protocol is up
Internet address is 172.29.50.3/27
Broadcast address is 255.255.255.255
Address determined by non-volatile memory
MTU is 1500 bytes
Helper addresses are 172.16.0.1
192.168.1.1
Directed broadcast forwarding is enabled
Multicast reserved groups joined: 224.0.0.102 224.0.0.5 224.0.0.6
Outgoing access list is not set
Inbound access list is not set
Proxy ARP is disabled
Local Proxy ARP is disabled
Security level is default
Split horizon is enabled
ICMP redirects are never sent
ICMP unreachables are never sent
ICMP mask replies are never sent
IP fast switching is enabled
IP Flow switching is disabled
IP CEF switching is enabled
IP CEF switching turbo vector
IP Null turbo vector
Associated unicast routing topologies:
Topology "base", operation state is UP
IP multicast fast switching is enabled
IP multicast distributed fast switching is disabled
IP route-cache flags are Fast, CEF
Router Discovery is disabled
IP output packet accounting is disabled
IP access violation accounting is disabled
TCP/IP header compression is disabled
RTP/IP header compression is disabled
Probe proxy name replies are disabled
Policy routing is disabled
Network address translation is disabled
BGP Policy Mapping is disabled
Input features: MCI Check
Output features: IP Post Routing Processing, HW Shortcut Installation
Post encapsulation features: MTU Processing, IP Protocol Output Counter, IP Sendself Check, HW Shortcut Installation
Sampled Netflow is disabled
IP Routed Flow creation is disabled in netflow table
IP Bridged Flow creation is disabled in netflow table
IPv4 WCCP Redirect outbound is disabled
IPv4 WCCP Redirect inbound is disabled
IPv4 WCCP Redirect exclude is disabled
IP multicast multilayer switching is disabled
GigabitEthernet1/1 is down, line protocol is down
GigabitEthernet1/2 is down, line protocol is down
GigabitEthernet1/3 is administratively down, line protocol is down
Internet protocol processing disabled
Vlan41 is up, line protocol is up
Internet address is 172.29.52.34/27
Broadcast address is 255.255.255.255
Address determined by non-volatile memory
MTU is 1514 bytes
Helper address is not set
Directed broadcast forwarding is disabled
Secondary address 192.168.81.34/24
Outgoing access list is not set
Inbound access list is not set
Proxy ARP is enabled
Local Proxy ARP is disabled
Security level is default
Split horizon is enabled
ICMP redirects are always sent
ICMP unreachables are always sent
ICMP mask replies are never sent
IP fast switching is enabled
IP fast switching on the same interface is disabled
IP Flow switching is disabled
IP CEF switching is enabled
IP CEF Fast switching turbo vector
IP multicast fast switching is enabled
IP multicast distributed fast switching is disabled
IP route-cache flags are Fast, CEF
Router Discovery is disabled
IP output packet accounting is disabled
IP access violation accounting is disabled
TCP/IP header compression is disabled
RTP/IP header compression is disabled
Policy routing is disabled
Network address translation is disabled
BGP Policy Mapping is disabled
WCCP Redirect outbound is disabled
WCCP Redirect inbound is disabled
WCCP Redirect exclude is disabled

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
Vlan20 is administratively down, line protocol is down
IPv6 is enabled, link-local address is FE80::C009:24FF:FEAC:0 [TEN]
Global unicast address(es):
2002::1, subnet is 2002::/64 [TEN]
Joined group address(es):
FF02::1
FF02::1:FF00:1
FF02::1:FFAC:0
MTU is 1500 bytes
ICMP error messages limited to one every 100 milliseconds
ICMP redirects are enabled
ND DAD is enabled, number of DAD attempts: 1
ND reachable time is 30000 milliseconds
Loopback0 is up, line protocol is up
IPv6 is enabled, link-local address is FE80::C009:24FF:FEAC:0
Global unicast address(es):
2001::1, subnet is 2001::1/128
2001::2, subnet is 2001::2/128
Joined group address(es):
FF02::1
FF02::2
FF02::1:FF00:1
FF02::1:FF00:2
FF02::1:FFAC:0
MTU is 1514 bytes
ICMP error messages limited to one every 100 milliseconds
ICMP redirects are enabled
ND DAD is not supported
ND reachable time is 30000 milliseconds

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.