Skip to content

Commit

Permalink
Adding get_interfaces_ip and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
gjim83 committed Jan 18, 2018
1 parent 0aae5ed commit f92e680
Show file tree
Hide file tree
Showing 3 changed files with 252 additions and 0 deletions.
62 changes: 62 additions & 0 deletions napalm_panos/panos.py
Original file line number Diff line number Diff line change
Expand Up @@ -534,3 +534,65 @@ def get_interfaces(self):
interface_dict[name] = interface

return interface_dict

def get_interfaces_ip(self):
def extract_ip_info(parsed_intf_dict):
intf = parsed_intf_dict['name']
_ip_info = {intf: {}}

v4_ip = parsed_intf_dict.get('ip')
secondary_v4_ip = parsed_intf_dict.get('addr')
v6_ip = parsed_intf_dict.get('addr6')

if v4_ip != 'N/A':
address, pref = v4_ip.split('/')
_ip_info[intf].setdefault('ipv4', {})[address] = {'prefix_length': int(pref)}

if secondary_v4_ip:
members = secondary_v4_ip['member']
if isinstance(members, str):
# If only 1 secondary IP is present, xmltodict converts field to a string, else
# it converts it to a list of strings.
members = [members]
for address in members:
address, pref = address.split('/')
_ip_info[intf].setdefault('ipv4', {})[address] = {'prefix_length': int(pref)}

if v6_ip is not None:
members = v6_ip['member']
if isinstance(members, str):
# Same "1 vs many -> string vs list" comment.
members = [members]
for address in members:
address, pref = address.split('/')
_ip_info[intf].setdefault('ipv6', {})[address] = {'prefix_length': int(pref)}

# Reset dict if no addresses were found.
if _ip_info == {intf: {}}:
_ip_info = {}

return _ip_info

ip_interfaces = {}
cmd = "<show><interface>all</interface></show>"

try:
self.device.op(cmd=cmd)
interface_info_xml = xmltodict.parse(self.device.xml_root())
interface_info_json = json.dumps(
interface_info_xml['response']['result']['ifnet']['entry']
)
interface_info = json.loads(interface_info_json)
except KeyError:
return ip_interfaces

if isinstance(interface_info, dict):
# Same "1 vs many -> string vs list" comment.
interface_info = [interface_info]

for interface_dict in interface_info:
ip_info = extract_ip_info(interface_dict)
if ip_info:
ip_interfaces = {**ip_interfaces, **ip_info}

return ip_interfaces
Original file line number Diff line number Diff line change
@@ -0,0 +1,189 @@
<response status="success">
<result>
<ifnet>
<entry>
<name>ethernet1/1</name>
<zone>foo</zone>
<fwd>N/A</fwd>
<vsys>1</vsys>
<dyn-addr/>
<addr6/>
<tag>0</tag>
<ip>N/A</ip>
<id>16</id>
<addr/>
</entry>
<entry>
<name>ethernet1/1.202</name>
<zone>foo</zone>
<fwd>vr:default</fwd>
<vsys>1</vsys>
<dyn-addr/>
<addr6/>
<tag>202</tag>
<ip>157.191.8.17/28</ip>
<id>257</id>
<addr/>
</entry>
<entry>
<name>ethernet1/2</name>
<zone>Internet</zone>
<fwd>vr:default</fwd>
<vsys>1</vsys>
<dyn-addr/>
<addr6/>
<tag>0</tag>
<ip>157.191.6.156/24</ip>
<id>18</id>
<addr>
<member>2.2.2.2/24</member>
</addr>
</entry>
<entry>
<name>ethernet1/3</name>
<zone/>
<fwd>N/A</fwd>
<vsys>1</vsys>
<dyn-addr/>
<addr6/>
<tag>0</tag>
<ip>N/A</ip>
<id>19</id>
<addr/>
</entry>
<entry>
<name>ethernet1/3.301</name>
<zone/>
<fwd>vr:default</fwd>
<vsys>1</vsys>
<dyn-addr/>
<addr6/>
<tag>301</tag>
<ip>172.24.255.1/26</ip>
<id>258</id>
<addr/>
</entry>
<entry>
<name>ethernet1/4</name>
<zone/>
<fwd>N/A</fwd>
<vsys>1</vsys>
<dyn-addr/>
<addr6>
<member>fe80::d61d:71ff:fed8:fe14/64</member>
<member>2001::1234/120</member>
</addr6>
<tag>0</tag>
<ip>169.254.0.1/30</ip>
<id>20</id>
<addr>
<member>1.1.1.1/28</member>
</addr>
</entry>
<entry>
<name>loopback</name>
<zone/>
<fwd>N/A</fwd>
<vsys>1</vsys>
<dyn-addr/>
<addr6/>
<tag>0</tag>
<ip>N/A</ip>
<id>3</id>
<addr/>
</entry>
<entry>
<name>loopback.100</name>
<zone>Mbar</zone>
<fwd>vr:default</fwd>
<vsys>1</vsys>
<dyn-addr/>
<addr6/>
<tag>0</tag>
<ip>N/A</ip>
<id>256</id>
<member>2001::7890/84</member>
</entry>
<entry>
<name>tunnel</name>
<zone/>
<fwd>N/A</fwd>
<vsys>1</vsys>
<dyn-addr/>
<addr6/>
<tag>0</tag>
<ip>N/A</ip>
<id>4</id>
<addr/>
</entry>
</ifnet>
<hw>
<entry>
<name>ethernet1/1</name>
<duplex>full</duplex>
<type>0</type>
<state>up</state>
<st>10000/full/up</st>
<mac>d4:1d:71:d8:fe:10</mac>
<mode>(autoneg)</mode>
<speed>10000</speed>
<id>16</id>
</entry>
<entry>
<name>ethernet1/2</name>
<duplex>full</duplex>
<type>0</type>
<state>up</state>
<st>10000/full/up</st>
<mac>d4:1d:71:d8:fe:11</mac>
<mode>(autoneg)</mode>
<speed>10000</speed>
<id>17</id>
</entry>
<entry>
<name>ethernet1/3</name>
<duplex>full</duplex>
<type>0</type>
<state>up</state>
<st>10000/full/up</st>
<mac>d4:1d:71:d8:fe:12</mac>
<mode>(autoneg)</mode>
<speed>10000</speed>
<id>18</id>
</entry>
<entry>
<name>ethernet1/4</name>
<duplex>full</duplex>
<type>0</type>
<state>up</state>
<st>10000/full/up</st>
<mac>d4:1d:71:d8:fe:13</mac>
<mode>(autoneg)</mode>
<speed>10000</speed>
<id>19</id>
</entry>
<entry>
<name>loopback</name>
<duplex>[n/a]</duplex>
<type>5</type>
<state>up</state>
<st>[n/a]/[n/a]/up</st>
<mac>d4:1d:71:d8:fe:03</mac>
<mode>(unknown)</mode>
<speed>[n/a]</speed>
<id>3</id>
</entry>
<entry>
<name>tunnel</name>
<duplex>[n/a]</duplex>
<type>6</type>
<state>up</state>
<st>[n/a]/[n/a]/up</st>
<mac>d4:1d:71:d8:fe:04</mac>
<mode>(unknown)</mode>
<speed>[n/a]</speed>
<id>4</id>
</entry>
</hw>
</result>
</response>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"ethernet1/3.301": {"ipv4": {"172.24.255.1": {"prefix_length": 26}}}, "ethernet1/2": {"ipv4": {"2.2.2.2": {"prefix_length": 24}, "157.191.6.156": {"prefix_length": 24}}}, "ethernet1/1.202": {"ipv4": {"157.191.8.17": {"prefix_length": 28}}}, "ethernet1/4": {"ipv4": {"1.1.1.1": {"prefix_length": 28}, "169.254.0.1": {"prefix_length": 30}}, "ipv6": {"2001::1234": {"prefix_length": 120}, "fe80::d61d:71ff:fed8:fe14": {"prefix_length": 64}}}}

0 comments on commit f92e680

Please sign in to comment.