diff --git a/napalm_fortios/fortios.py b/napalm_fortios/fortios.py index 30113ba..54957cc 100644 --- a/napalm_fortios/fortios.py +++ b/napalm_fortios/fortios.py @@ -266,6 +266,27 @@ class FortiOSDriver(NetworkDriver): interface_statistics[interface] = parsed_data return interface_statistics + def get_interfaces_ip(self): + cmd = self._execute_command_with_vdom('get system interface physical', vdom='global') + interface_ip_dictionary = {} + if_name = None + for line in cmd: + interface_data = line.strip().split() + if len(interface_data)==0: + continue + if interface_data[0].startswith('==['): + m = re.search("==\[(.+)\]", interface_data[0]) + if_name = m.group(1) + if if_name and interface_data[0]=="ip:" and interface_data[1][0]!='0': + interface_ip_dictionary[if_name]={} + interface_ip_dictionary[if_name]["ipv4"]={interface_data[1]: {'prefix_length': sum(bin(int(x)).count('1') for x in interface_data[2].split('.'))}} + if if_name and interface_data[0]=="ipv6:" and interface_data[1][0]!=':': + try: + interface_ip_dictionary[if_name]["ipv6"]={interface_data[1].split('/')[0]: {'prefix_length': interface_data[1].split('/')[1]}} + except: + interface_ip_dictionary[if_name]={"ipv6": {interface_data[1].split('/')[0]: {'prefix_length': interface_data[1].split('/')[1]}}} + return interface_ip_dictionary + @staticmethod def _search_line_in_lines(search, lines): for l in lines: diff --git a/test/unit/mocked_data/test_get_interfaces_ip/normal/expected_result.json b/test/unit/mocked_data/test_get_interfaces_ip/normal/expected_result.json new file mode 100644 index 0000000..3944c18 --- /dev/null +++ b/test/unit/mocked_data/test_get_interfaces_ip/normal/expected_result.json @@ -0,0 +1 @@ +{'mgmt': {'ipv4': {'192.168.1.99': {'prefix_length': 24}}}, 'port1': {'ipv4': {'10.0.96.152': {'prefix_length': 19}}, 'ipv6': {'fd00:d051::10:0:96:152': {'prefix_length': '64'}}}} diff --git a/test/unit/mocked_data/test_get_interfaces_ip/normal/get_system_interface_physical.txt b/test/unit/mocked_data/test_get_interfaces_ip/normal/get_system_interface_physical.txt new file mode 100644 index 0000000..290d8cb --- /dev/null +++ b/test/unit/mocked_data/test_get_interfaces_ip/normal/get_system_interface_physical.txt @@ -0,0 +1,20 @@ +== [onboard] + ==[mgmt] + mode: static + ip: 192.168.1.99 255.255.255.0 + ipv6: ::/0 + status: down + speed: n/a + ==[port1] + mode: static + ip: 10.0.96.152 255.255.224.0 + ipv6: fd00:d051::10:0:96:152/64 + status: up + speed: 1000Mbps (Duplex: full) + ==[port2] + mode: static + ip: 0.0.0.0 0.0.0.0 + ipv6: ::/0 + status: up + speed: 1000Mbps (Duplex: full) +