From 7a14a129c30f96b4cf53c29798765fff9cf5767c Mon Sep 17 00:00:00 2001 From: David Barroso Date: Tue, 28 Apr 2015 16:57:22 +0200 Subject: [PATCH 01/29] Adding some utils to parse information from the CLI --- utils/__init__.py | 0 utils/string_parsers.py | 54 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+) create mode 100644 utils/__init__.py create mode 100644 utils/string_parsers.py diff --git a/utils/__init__.py b/utils/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/utils/string_parsers.py b/utils/string_parsers.py new file mode 100644 index 0000000..5e9b140 --- /dev/null +++ b/utils/string_parsers.py @@ -0,0 +1,54 @@ +def colon_separated_string_to_dict(string): + ''' + Converts a string in the format: + + Name: Et3 + Switchport: Enabled + Administrative Mode: trunk + Operational Mode: trunk + MAC Address Learning: enabled + Access Mode VLAN: 3 (VLAN0003) + Trunking Native Mode VLAN: 1 (default) + Administrative Native VLAN tagging: disabled + Administrative private VLAN mapping: ALL + Trunking VLANs Enabled: 2-3,5-7,20-21,23,100-200 + Trunk Groups: + + into a dictionary + + ''' + dictionary = dict() + + for line in string.splitlines(): + line_data = line.split(':') + + if len(line_data) > 1: + dictionary[line_data[0].strip()] = ''.join(line_data[1:]) + elif len(line_data) == 1: + dictionary[line_data[0].strip()] = None + else: + raise Exception('Something went wrong parsing the colo separated string {}'.format(line)) + + return dictionary + + +def hyphen_range(string): + ''' + Expands a string of numbers separated by commas and hyphens into a list of integers. + For example: 2-3,5-7,20-21,23,100-200 + ''' + list_numbers = list() + temporary_list = string.split(',') + + for element in temporary_list: + sub_element = element.split('-') + + if len(sub_element) == 1: + list_numbers.append(int(sub_element[0])) + elif len(sub_element) == 2: + for x in range(int(sub_element[0]), int(sub_element[1])+1): + list_numbers.append(x) + else: + raise Exception('Something went wrong expanding the range'.format(string)) + + return list_numbers From 4f3f5ca69f7b99bc6b2e9a02ccf94c545612b821 Mon Sep 17 00:00:00 2001 From: Elisa Jasinska Date: Thu, 30 Apr 2015 16:47:34 +0200 Subject: [PATCH 02/29] email address change --- AUTHORS | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/AUTHORS b/AUTHORS index 0b76d1e..5f80284 100644 --- a/AUTHORS +++ b/AUTHORS @@ -1,2 +1,2 @@ David Barroso -Elisa Jasinska +Elisa Jasinska From 4a8884c91abf073b8b81a529cddd2092e6897168 Mon Sep 17 00:00:00 2001 From: Elisa Jasinska Date: Wed, 6 May 2015 17:16:28 +0200 Subject: [PATCH 03/29] added junos facts() and interfaces() and basic bgp and lldp neigh tables/views (<- untested) --- utils/junos_views.py | 8 ++++++ utils/junos_views.yml | 62 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 70 insertions(+) create mode 100755 utils/junos_views.py create mode 100644 utils/junos_views.yml diff --git a/utils/junos_views.py b/utils/junos_views.py new file mode 100755 index 0000000..56533e0 --- /dev/null +++ b/utils/junos_views.py @@ -0,0 +1,8 @@ +""" +Load tables/views +""" +from jnpr.junos.factory import loadyaml +from os.path import splitext +_YAML_ = splitext(__file__)[0] + '.yml' +print _YAML_ +globals().update(loadyaml(_YAML_)) diff --git a/utils/junos_views.yml b/utils/junos_views.yml new file mode 100644 index 0000000..2348a5f --- /dev/null +++ b/utils/junos_views.yml @@ -0,0 +1,62 @@ +--- + +#### +#### Interface table +#### + +junos_iface_table: + rpc: get-interface-information + args: + extensive: True + args_key: interface_name + item: physical-interface + view: junos_iface_view + +junos_iface_view: + fields: + is_up: { oper-status: True=up } + is_enabled: { admin-status: True=up } + description: description + last_flapped: { interface-flapped/@seconds: int } + mode: logical-interface/address-family/address-family-name + speed: speed + mac_address: current-physical-address + + +#### +#### BGP table +#### + +junos_bgp_table: + rpc: get-bgp-neighbor-information + item: bgp-peer + key: peer-address + view: junos_bgp_view + +junos_bgp_view: + fields: + is_up: { peer-state: True=Established } + is_enabled: { shutdown: True=False } + uptime: { elapsed-time/@seconds: int } + description: description + received_prefixes: { bgp-rib/received-prefix-count: int } + accepted_prefixes: { bgp-rib/accepted-prefix-count: int } + remote_as: { peer-as: int } + + +#### +#### LLDP table +#### + +junos_lldp_table: + rpc: get-lldp-neighbors-information + item: lldp-neighbor-information + key: lldp-local-interface | lldp-local-port-id + view: junos_lldp_view + +junos_lldp_view: + fields: + hostname: lldp-remote-system-name + port: lldp-remote-port-description + + From 951883c6c6f012ea5e0cfb944f69cc01f1e244d2 Mon Sep 17 00:00:00 2001 From: David Barroso Date: Wed, 6 May 2015 22:21:50 +0200 Subject: [PATCH 04/29] Strip moved to the parser as it is more useful there --- utils/string_parsers.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/utils/string_parsers.py b/utils/string_parsers.py index 5e9b140..5b14871 100644 --- a/utils/string_parsers.py +++ b/utils/string_parsers.py @@ -23,7 +23,7 @@ def colon_separated_string_to_dict(string): line_data = line.split(':') if len(line_data) > 1: - dictionary[line_data[0].strip()] = ''.join(line_data[1:]) + dictionary[line_data[0].strip()] = ''.join(line_data[1:]).strip() elif len(line_data) == 1: dictionary[line_data[0].strip()] = None else: From b856cbea7df360969141a64d4d1f03be5c1f940f Mon Sep 17 00:00:00 2001 From: Elisa Jasinska Date: Sun, 17 May 2015 14:15:18 +0200 Subject: [PATCH 05/29] fixing yaml file import for junos views --- Manifest.in | 3 ++- utils/junos_views.py | 1 - 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Manifest.in b/Manifest.in index 540b720..16c0d1a 100644 --- a/Manifest.in +++ b/Manifest.in @@ -1 +1,2 @@ -include requirements.txt \ No newline at end of file +include requirements.txt +include utils/*.yml diff --git a/utils/junos_views.py b/utils/junos_views.py index 56533e0..a8f014d 100755 --- a/utils/junos_views.py +++ b/utils/junos_views.py @@ -4,5 +4,4 @@ Load tables/views from jnpr.junos.factory import loadyaml from os.path import splitext _YAML_ = splitext(__file__)[0] + '.yml' -print _YAML_ globals().update(loadyaml(_YAML_)) From 62f1b635a1bf88a175781efd4aa3fd0ccd156a5e Mon Sep 17 00:00:00 2001 From: Elisa Jasinska Date: Sun, 17 May 2015 20:50:31 +0200 Subject: [PATCH 06/29] bgp output now looks like davids --- utils/junos_views.yml | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/utils/junos_views.yml b/utils/junos_views.yml index 2348a5f..0de5ab3 100644 --- a/utils/junos_views.yml +++ b/utils/junos_views.yml @@ -24,9 +24,19 @@ junos_iface_view: #### -#### BGP table +#### BGP tables #### +junos_route_instance_table: + rpc: get-instance-information + item: instance-core + key: instance-name + view: junos_route_instance_view + +junos_route_instance_view: + fields: + name: instance-name + junos_bgp_table: rpc: get-bgp-neighbor-information item: bgp-peer From 72fc287330ad69484aff2d715e0b27dec4830765 Mon Sep 17 00:00:00 2001 From: Elisa Jasinska Date: Sun, 17 May 2015 23:14:35 +0200 Subject: [PATCH 07/29] fixing up peer ip and session uptime --- utils/junos_views.yml | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/utils/junos_views.yml b/utils/junos_views.yml index 0de5ab3..561cb7f 100644 --- a/utils/junos_views.yml +++ b/utils/junos_views.yml @@ -38,21 +38,20 @@ junos_route_instance_view: name: instance-name junos_bgp_table: - rpc: get-bgp-neighbor-information + rpc: get-bgp-summary-information item: bgp-peer key: peer-address view: junos_bgp_view junos_bgp_view: fields: + remote_as: { peer-as: int } + description: description is_up: { peer-state: True=Established } is_enabled: { shutdown: True=False } uptime: { elapsed-time/@seconds: int } - description: description received_prefixes: { bgp-rib/received-prefix-count: int } accepted_prefixes: { bgp-rib/accepted-prefix-count: int } - remote_as: { peer-as: int } - #### #### LLDP table From 554ab966085deece6dd3c3bd2e7d125f4c7771cd Mon Sep 17 00:00:00 2001 From: Elisa Jasinska Date: Sun, 17 May 2015 23:28:44 +0200 Subject: [PATCH 08/29] ocd --- utils/junos_views.yml | 1 - 1 file changed, 1 deletion(-) diff --git a/utils/junos_views.yml b/utils/junos_views.yml index 561cb7f..e40c229 100644 --- a/utils/junos_views.yml +++ b/utils/junos_views.yml @@ -22,7 +22,6 @@ junos_iface_view: speed: speed mac_address: current-physical-address - #### #### BGP tables #### From e0aa9d4bb90afa20fe0fa9043a7e2075f91224e9 Mon Sep 17 00:00:00 2001 From: David Barroso Date: Mon, 18 May 2015 15:15:08 +0200 Subject: [PATCH 09/29] more parsers --- utils/string_parsers.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/utils/string_parsers.py b/utils/string_parsers.py index 5b14871..08ac9a9 100644 --- a/utils/string_parsers.py +++ b/utils/string_parsers.py @@ -1,4 +1,4 @@ -def colon_separated_string_to_dict(string): +def colon_separated_string_to_dict(string, separator=':'): ''' Converts a string in the format: @@ -20,7 +20,7 @@ def colon_separated_string_to_dict(string): dictionary = dict() for line in string.splitlines(): - line_data = line.split(':') + line_data = line.split(separator) if len(line_data) > 1: dictionary[line_data[0].strip()] = ''.join(line_data[1:]).strip() From 0dd1c3c1d2ac55a8f2789fa6d455b9148f8922c8 Mon Sep 17 00:00:00 2001 From: David Barroso Date: Mon, 18 May 2015 15:15:25 +0200 Subject: [PATCH 10/29] bla --- napalm/fortios.py | 145 +++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 144 insertions(+), 1 deletion(-) diff --git a/napalm/fortios.py b/napalm/fortios.py index 11efd2c..579aee3 100644 --- a/napalm/fortios.py +++ b/napalm/fortios.py @@ -18,6 +18,55 @@ from pyFG.exceptions import FailedCommit, CommandExecutionException from base import NetworkDriver from exceptions import ReplaceConfigException, MergeConfigException +from utils.string_parsers import colon_separated_string_to_dict + +import re + + +def execute_get(device, cmd, separator=':', auto=False): + output = device.execute_command(cmd) + + if auto: + if ':' in output[0]: + separator=':' + elif '\t' in output[0]: + separator='\t' + else: + raise Exception('Unknown separator for block:\n{}'.format(output)) + + return colon_separated_string_to_dict('\n'.join(output), separator) + + +def convert_uptime_string_seconds(uptime): + regex_1 = re.compile(r"((?P\d+) days,\s+)?((?P\d+) hours,\s+)?((?P\d+) minutes)") + regex_2 = re.compile(r"((?P\d+)):((?P\d+)):((?P\d+))") + + regex_list = [regex_1, regex_2] + + uptime_dict = dict() + for regex in regex_list: + uptime_dict = regex.match(uptime) + if uptime_dict is not None: + uptime_dict = uptime_dict.groupdict() + break + uptime_dict = dict() + + uptime_seconds = 0 + + for unit, value in uptime_dict.iteritems(): + if unit == 'days': + uptime_seconds += int(value) * 86400 + elif unit == 'hours': + uptime_seconds += int(value) * 3600 + elif unit == 'minutes': + uptime_seconds += int(value) * 60 + elif unit == 'seconds': + uptime_seconds += int(value) + else: + raise Exception('Unrecognized unit "{}" in uptime:{}'.format(unit, uptime)) + + return uptime_seconds + class FortiOSDriver(NetworkDriver): @@ -96,4 +145,98 @@ class FortiOSDriver(NetworkDriver): self.device.candidate_config['vpn certificate local']['Fortinet_CA_SSLProxy'].del_param('certificate') self.device.candidate_config['vpn certificate local']['Fortinet_SSLProxy'].del_param('private-key') self.device.candidate_config['vpn certificate local']['Fortinet_SSLProxy'].del_param('certificate') - self.device.commit() \ No newline at end of file + self.device.commit() + + def get_facts(self): + system_status = execute_get(self.device, 'get system status') + performance_status = execute_get(self.device, 'get system performance status') + + interfaces = execute_get(self.device, 'get system interface | grep ==') + interface_list = [x.split()[2] for x in interfaces.keys()] + + domain = execute_get(self.device, 'get system dns | grep domain')['domain'] + + return { + 'vendor': 'Fortigate', + 'os_version': system_status['Version'].split(',')[0].split()[1], + 'uptime': convert_uptime_string_seconds(performance_status['Uptime']), + 'serial_number': system_status['Serial-Number'], + 'model': system_status['Version'].split(',')[0].split()[0], + 'hostname': system_status['Hostname'], + 'fqdn': '{}.{}'.format(system_status['Hostname'], domain), + 'interface_list': interface_list + } + + @staticmethod + def _get_tab_separated_interfaces(output): + interface_statistics = { + 'is_up': ('up' in output['State'] and 'up' or 'down'), + 'speed': output['Speed'], + 'mac_adddress': output['Current_HWaddr'] + } + return interface_statistics + + @staticmethod + def _get_unsupported_interfaces(): + return { + 'is_up': None, + 'is_enabled': None, + 'description': None, + 'last_flapped': None, + 'mode': None, + 'speed': None, + 'mac_address': None + } + + def get_interfaces(self): + interface_statistics = dict() + self.device.load_config('system interface') + for iface in self.get_facts()['interface_list']: + try: + hw_output = execute_get(self.device, 'diagnose hardware deviceinfo nic {}'.format(iface), auto=True) + ifs = self._get_tab_separated_interfaces(hw_output) + + ifs['is_enabled'] = self.device.running_config['system interface'][iface].get_param('status') != 'down' + ifs['description'] = self.device.running_config['system interface'][iface].get_param('description') + ifs['last_flapped'] = None + ifs['mode'] = 'routed' + ifs['last_flapped'] = None + interface_statistics[iface] = ifs + except CommandExecutionException: + interface_statistics[iface] = self._get_unsupported_interfaces() + + return interface_statistics + + def get_bgp_neighbors(self): + bgp_sum = self.device.execute_command('get router info bgp sum') + re_neigh = re.compile("^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+") + neighbors = {n.split()[0]: n.split()[1:] for n in bgp_sum if re.match(re_neigh, n)} + + peers = dict() + + self.device.load_config('router bgp') + + for neighbor, parameters in neighbors.iteritems(): + neigh_conf = self.device.running_config['router bgp']['neighbor']['{}'.format(neighbor)] + peers[neighbor] = dict() + peers[neighbor]['remote_as'] = int(neigh_conf.get_param('remote-as')) + peers[neighbor]['is_enabled'] = neigh_conf.get_param('shutdown') != 'enable' or False + peers[neighbor]['is_up'] = 'never' != parameters[7] or False + peers[neighbor]['uptime'] = convert_uptime_string_seconds(parameters[7]) + peers[neighbor]['accepted_prefixes'] = int(self.device.execute_command('get router info bgp neighbor {} | grep "accepted prefixes"'.format(neighbor))[0].split()[0]) + peers[neighbor]['sent_prefixes'] = int(self.device.execute_command('get router info bgp neighbor {} | grep "announced prefixes"'.format(neighbor))[0].split()[0]) + + received = self.device.execute_command('get router info bgp neighbors {} received-routes | grep prefixes'.format(neighbor))[0].split() + if len(received) > 0: + peers[neighbor]['received_prefixes'] = int(received[-1]) + else: + # Soft-reconfig is not enabled + peers[neighbor]['received_prefixes'] = 0 + + return { + 'default': { + 'local_as': int(bgp_sum[0].split()[7]), + 'router_id': bgp_sum[0].split()[3], + 'peers': peers + } + } \ No newline at end of file From c4ffc6dd01f76068bcb529ba5973494e7669d14f Mon Sep 17 00:00:00 2001 From: Elisa Jasinska Date: Wed, 20 May 2015 16:50:06 +0200 Subject: [PATCH 11/29] iosxr facts --- utils/junos_views.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/utils/junos_views.yml b/utils/junos_views.yml index e40c229..263bdab 100644 --- a/utils/junos_views.yml +++ b/utils/junos_views.yml @@ -47,7 +47,7 @@ junos_bgp_view: remote_as: { peer-as: int } description: description is_up: { peer-state: True=Established } - is_enabled: { shutdown: True=False } + is_enabled: { peer-state: False=True } uptime: { elapsed-time/@seconds: int } received_prefixes: { bgp-rib/received-prefix-count: int } accepted_prefixes: { bgp-rib/accepted-prefix-count: int } From a3b1634f2de21dcb6cdea0a5101837b7898366e8 Mon Sep 17 00:00:00 2001 From: David Barroso Date: Sat, 23 May 2015 09:36:30 +0200 Subject: [PATCH 12/29] Interfaces are sorted in get_facts --- utils/string_parsers.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/utils/string_parsers.py b/utils/string_parsers.py index 08ac9a9..1d71cd4 100644 --- a/utils/string_parsers.py +++ b/utils/string_parsers.py @@ -1,3 +1,11 @@ +import re + +def sorted_nicely( l ): + """ Sort the given iterable in the way that humans expect.""" + convert = lambda text: int(text) if text.isdigit() else text + alphanum_key = lambda key: [ convert(c) for c in re.split('([0-9]+)', key) ] + return sorted(l, key = alphanum_key) + def colon_separated_string_to_dict(string, separator=':'): ''' Converts a string in the format: From 8ab55587d2db2e39827bcc90a39d7986a675af0a Mon Sep 17 00:00:00 2001 From: Elisa Jasinska Date: Wed, 6 May 2015 17:16:28 +0200 Subject: [PATCH 13/29] added junos facts() and interfaces() and basic bgp and lldp neigh tables/views (<- untested) --- utils/junos_views.py | 8 ++++++ utils/junos_views.yml | 62 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 70 insertions(+) create mode 100755 utils/junos_views.py create mode 100644 utils/junos_views.yml diff --git a/utils/junos_views.py b/utils/junos_views.py new file mode 100755 index 0000000..56533e0 --- /dev/null +++ b/utils/junos_views.py @@ -0,0 +1,8 @@ +""" +Load tables/views +""" +from jnpr.junos.factory import loadyaml +from os.path import splitext +_YAML_ = splitext(__file__)[0] + '.yml' +print _YAML_ +globals().update(loadyaml(_YAML_)) diff --git a/utils/junos_views.yml b/utils/junos_views.yml new file mode 100644 index 0000000..2348a5f --- /dev/null +++ b/utils/junos_views.yml @@ -0,0 +1,62 @@ +--- + +#### +#### Interface table +#### + +junos_iface_table: + rpc: get-interface-information + args: + extensive: True + args_key: interface_name + item: physical-interface + view: junos_iface_view + +junos_iface_view: + fields: + is_up: { oper-status: True=up } + is_enabled: { admin-status: True=up } + description: description + last_flapped: { interface-flapped/@seconds: int } + mode: logical-interface/address-family/address-family-name + speed: speed + mac_address: current-physical-address + + +#### +#### BGP table +#### + +junos_bgp_table: + rpc: get-bgp-neighbor-information + item: bgp-peer + key: peer-address + view: junos_bgp_view + +junos_bgp_view: + fields: + is_up: { peer-state: True=Established } + is_enabled: { shutdown: True=False } + uptime: { elapsed-time/@seconds: int } + description: description + received_prefixes: { bgp-rib/received-prefix-count: int } + accepted_prefixes: { bgp-rib/accepted-prefix-count: int } + remote_as: { peer-as: int } + + +#### +#### LLDP table +#### + +junos_lldp_table: + rpc: get-lldp-neighbors-information + item: lldp-neighbor-information + key: lldp-local-interface | lldp-local-port-id + view: junos_lldp_view + +junos_lldp_view: + fields: + hostname: lldp-remote-system-name + port: lldp-remote-port-description + + From 0673999342c513d1737964bb182d6f0c4cc1dad3 Mon Sep 17 00:00:00 2001 From: Elisa Jasinska Date: Sun, 17 May 2015 14:15:18 +0200 Subject: [PATCH 14/29] fixing yaml file import for junos views --- Manifest.in | 3 ++- utils/junos_views.py | 1 - 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Manifest.in b/Manifest.in index 540b720..16c0d1a 100644 --- a/Manifest.in +++ b/Manifest.in @@ -1 +1,2 @@ -include requirements.txt \ No newline at end of file +include requirements.txt +include utils/*.yml diff --git a/utils/junos_views.py b/utils/junos_views.py index 56533e0..a8f014d 100755 --- a/utils/junos_views.py +++ b/utils/junos_views.py @@ -4,5 +4,4 @@ Load tables/views from jnpr.junos.factory import loadyaml from os.path import splitext _YAML_ = splitext(__file__)[0] + '.yml' -print _YAML_ globals().update(loadyaml(_YAML_)) From fbbbd76c987681023f67b468acb381a9a1054b06 Mon Sep 17 00:00:00 2001 From: Elisa Jasinska Date: Sun, 17 May 2015 20:50:31 +0200 Subject: [PATCH 15/29] bgp output now looks like davids --- utils/junos_views.yml | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/utils/junos_views.yml b/utils/junos_views.yml index 2348a5f..0de5ab3 100644 --- a/utils/junos_views.yml +++ b/utils/junos_views.yml @@ -24,9 +24,19 @@ junos_iface_view: #### -#### BGP table +#### BGP tables #### +junos_route_instance_table: + rpc: get-instance-information + item: instance-core + key: instance-name + view: junos_route_instance_view + +junos_route_instance_view: + fields: + name: instance-name + junos_bgp_table: rpc: get-bgp-neighbor-information item: bgp-peer From 74ba2a3b6dfc910651157ca17e4861dfc2b43982 Mon Sep 17 00:00:00 2001 From: Elisa Jasinska Date: Sun, 17 May 2015 23:14:35 +0200 Subject: [PATCH 16/29] fixing up peer ip and session uptime --- utils/junos_views.yml | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/utils/junos_views.yml b/utils/junos_views.yml index 0de5ab3..561cb7f 100644 --- a/utils/junos_views.yml +++ b/utils/junos_views.yml @@ -38,21 +38,20 @@ junos_route_instance_view: name: instance-name junos_bgp_table: - rpc: get-bgp-neighbor-information + rpc: get-bgp-summary-information item: bgp-peer key: peer-address view: junos_bgp_view junos_bgp_view: fields: + remote_as: { peer-as: int } + description: description is_up: { peer-state: True=Established } is_enabled: { shutdown: True=False } uptime: { elapsed-time/@seconds: int } - description: description received_prefixes: { bgp-rib/received-prefix-count: int } accepted_prefixes: { bgp-rib/accepted-prefix-count: int } - remote_as: { peer-as: int } - #### #### LLDP table From 6611e03546188ed7826701545e227204f1117420 Mon Sep 17 00:00:00 2001 From: Elisa Jasinska Date: Sun, 17 May 2015 23:28:44 +0200 Subject: [PATCH 17/29] ocd --- utils/junos_views.yml | 1 - 1 file changed, 1 deletion(-) diff --git a/utils/junos_views.yml b/utils/junos_views.yml index 561cb7f..e40c229 100644 --- a/utils/junos_views.yml +++ b/utils/junos_views.yml @@ -22,7 +22,6 @@ junos_iface_view: speed: speed mac_address: current-physical-address - #### #### BGP tables #### From f1539d507b6f72130b1185262097dcf7f58a2d57 Mon Sep 17 00:00:00 2001 From: Elisa Jasinska Date: Wed, 20 May 2015 16:50:06 +0200 Subject: [PATCH 18/29] iosxr facts --- utils/junos_views.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/utils/junos_views.yml b/utils/junos_views.yml index e40c229..263bdab 100644 --- a/utils/junos_views.yml +++ b/utils/junos_views.yml @@ -47,7 +47,7 @@ junos_bgp_view: remote_as: { peer-as: int } description: description is_up: { peer-state: True=Established } - is_enabled: { shutdown: True=False } + is_enabled: { peer-state: False=True } uptime: { elapsed-time/@seconds: int } received_prefixes: { bgp-rib/received-prefix-count: int } accepted_prefixes: { bgp-rib/accepted-prefix-count: int } From c9d5167fbab3b7314da852a832a080c2f96e95a8 Mon Sep 17 00:00:00 2001 From: Elisa Jasinska Date: Sat, 23 May 2015 23:05:53 +0200 Subject: [PATCH 19/29] making davids time string parser work for iosxr too --- napalm/fortios.py | 35 ++--------------------------------- utils/string_parsers.py | 38 +++++++++++++++++++++++++++++++++++++- 2 files changed, 39 insertions(+), 34 deletions(-) diff --git a/napalm/fortios.py b/napalm/fortios.py index 579aee3..f21807a 100644 --- a/napalm/fortios.py +++ b/napalm/fortios.py @@ -18,7 +18,7 @@ from pyFG.exceptions import FailedCommit, CommandExecutionException from base import NetworkDriver from exceptions import ReplaceConfigException, MergeConfigException -from utils.string_parsers import colon_separated_string_to_dict +from utils.string_parsers import colon_separated_string_to_dict, convert_uptime_string_seconds import re @@ -37,37 +37,6 @@ def execute_get(device, cmd, separator=':', auto=False): return colon_separated_string_to_dict('\n'.join(output), separator) -def convert_uptime_string_seconds(uptime): - regex_1 = re.compile(r"((?P\d+) days,\s+)?((?P\d+) hours,\s+)?((?P\d+) minutes)") - regex_2 = re.compile(r"((?P\d+)):((?P\d+)):((?P\d+))") - - regex_list = [regex_1, regex_2] - - uptime_dict = dict() - for regex in regex_list: - uptime_dict = regex.match(uptime) - if uptime_dict is not None: - uptime_dict = uptime_dict.groupdict() - break - uptime_dict = dict() - - uptime_seconds = 0 - - for unit, value in uptime_dict.iteritems(): - if unit == 'days': - uptime_seconds += int(value) * 86400 - elif unit == 'hours': - uptime_seconds += int(value) * 3600 - elif unit == 'minutes': - uptime_seconds += int(value) * 60 - elif unit == 'seconds': - uptime_seconds += int(value) - else: - raise Exception('Unrecognized unit "{}" in uptime:{}'.format(unit, uptime)) - - return uptime_seconds - - class FortiOSDriver(NetworkDriver): def __init__(self, hostname, username, password): @@ -239,4 +208,4 @@ class FortiOSDriver(NetworkDriver): 'router_id': bgp_sum[0].split()[3], 'peers': peers } - } \ No newline at end of file + } diff --git a/utils/string_parsers.py b/utils/string_parsers.py index 1d71cd4..15e293f 100644 --- a/utils/string_parsers.py +++ b/utils/string_parsers.py @@ -39,7 +39,6 @@ def colon_separated_string_to_dict(string, separator=':'): return dictionary - def hyphen_range(string): ''' Expands a string of numbers separated by commas and hyphens into a list of integers. @@ -60,3 +59,40 @@ def hyphen_range(string): raise Exception('Something went wrong expanding the range'.format(string)) return list_numbers + +def convert_uptime_string_seconds(uptime): + ''' + Convert uptime strings to seconds. The string can be formatted various ways, eg. + 1 hour, 56 minutes + ''' + regex_1 = re.compile(r"((?P\d+) day(s)?,\s+)?((?P\d+) hour(s)?,\s+)?((?P\d+) minute(s)?)") + regex_2 = re.compile(r"((?P\d+)):((?P\d+)):((?P\d+))") + + regex_list = [regex_1, regex_2] + + # say whaaaat!? + uptime_dict = dict() + for regex in regex_list: + uptime_dict = regex.match(uptime) + if uptime_dict is not None: + uptime_dict = uptime_dict.groupdict() + break + uptime_dict = dict() + + uptime_seconds = 0 + + for unit, value in uptime_dict.iteritems(): + if value != None: + if unit == 'days': + uptime_seconds += int(value) * 86400 + elif unit == 'hours': + uptime_seconds += int(value) * 3600 + elif unit == 'minutes': + uptime_seconds += int(value) * 60 + elif unit == 'seconds': + uptime_seconds += int(value) + else: + raise Exception('Unrecognized unit "{}" in uptime:{}'.format(unit, uptime)) + + return uptime_seconds + From eff374c0273e274ba128214c722d8a0fba63b91a Mon Sep 17 00:00:00 2001 From: Elisa Jasinska Date: Tue, 26 May 2015 13:14:07 +0200 Subject: [PATCH 20/29] I found junos sent prefixes!! --- utils/junos_views.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/utils/junos_views.yml b/utils/junos_views.yml index 263bdab..c744706 100644 --- a/utils/junos_views.yml +++ b/utils/junos_views.yml @@ -51,6 +51,7 @@ junos_bgp_view: uptime: { elapsed-time/@seconds: int } received_prefixes: { bgp-rib/received-prefix-count: int } accepted_prefixes: { bgp-rib/accepted-prefix-count: int } + sent_prefixes: { bgp-rib/aadvertised-prefix-count: int } #### #### LLDP table From 67c371ca512a41c290ccccc23f3eff3ea611a0dc Mon Sep 17 00:00:00 2001 From: David Barroso Date: Thu, 28 May 2015 16:07:33 -0400 Subject: [PATCH 21/29] Fixed get_lldp_neighbors --- napalm/fortios.py | 3 +++ utils/junos_views.yml | 4 ++-- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/napalm/fortios.py b/napalm/fortios.py index f21807a..d112b6d 100644 --- a/napalm/fortios.py +++ b/napalm/fortios.py @@ -209,3 +209,6 @@ class FortiOSDriver(NetworkDriver): 'peers': peers } } + + def get_lldp_neighbors(self): + return {} \ No newline at end of file diff --git a/utils/junos_views.yml b/utils/junos_views.yml index c744706..4477a9d 100644 --- a/utils/junos_views.yml +++ b/utils/junos_views.yml @@ -60,12 +60,12 @@ junos_bgp_view: junos_lldp_table: rpc: get-lldp-neighbors-information item: lldp-neighbor-information - key: lldp-local-interface | lldp-local-port-id + key: lldp-local-port-id | lldp-local-interface view: junos_lldp_view junos_lldp_view: fields: hostname: lldp-remote-system-name - port: lldp-remote-port-description + port: lldp-remote-port-description | lldp-remote-port-id From c2b883e8bc1b3eadac552697c9947ad878395001 Mon Sep 17 00:00:00 2001 From: David Barroso Date: Thu, 28 May 2015 16:12:25 -0400 Subject: [PATCH 22/29] Removing get_bgp_neighbors from the scope of 0.2 --- napalm/fortios.py | 66 +++++++++++++++++++++++------------------------ 1 file changed, 33 insertions(+), 33 deletions(-) diff --git a/napalm/fortios.py b/napalm/fortios.py index d112b6d..35bd8b1 100644 --- a/napalm/fortios.py +++ b/napalm/fortios.py @@ -176,39 +176,39 @@ class FortiOSDriver(NetworkDriver): return interface_statistics - def get_bgp_neighbors(self): - bgp_sum = self.device.execute_command('get router info bgp sum') - re_neigh = re.compile("^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+") - neighbors = {n.split()[0]: n.split()[1:] for n in bgp_sum if re.match(re_neigh, n)} - - peers = dict() - - self.device.load_config('router bgp') - - for neighbor, parameters in neighbors.iteritems(): - neigh_conf = self.device.running_config['router bgp']['neighbor']['{}'.format(neighbor)] - peers[neighbor] = dict() - peers[neighbor]['remote_as'] = int(neigh_conf.get_param('remote-as')) - peers[neighbor]['is_enabled'] = neigh_conf.get_param('shutdown') != 'enable' or False - peers[neighbor]['is_up'] = 'never' != parameters[7] or False - peers[neighbor]['uptime'] = convert_uptime_string_seconds(parameters[7]) - peers[neighbor]['accepted_prefixes'] = int(self.device.execute_command('get router info bgp neighbor {} | grep "accepted prefixes"'.format(neighbor))[0].split()[0]) - peers[neighbor]['sent_prefixes'] = int(self.device.execute_command('get router info bgp neighbor {} | grep "announced prefixes"'.format(neighbor))[0].split()[0]) - - received = self.device.execute_command('get router info bgp neighbors {} received-routes | grep prefixes'.format(neighbor))[0].split() - if len(received) > 0: - peers[neighbor]['received_prefixes'] = int(received[-1]) - else: - # Soft-reconfig is not enabled - peers[neighbor]['received_prefixes'] = 0 - - return { - 'default': { - 'local_as': int(bgp_sum[0].split()[7]), - 'router_id': bgp_sum[0].split()[3], - 'peers': peers - } - } + # def get_bgp_neighbors(self): + # bgp_sum = self.device.execute_command('get router info bgp sum') + # re_neigh = re.compile("^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+") + # neighbors = {n.split()[0]: n.split()[1:] for n in bgp_sum if re.match(re_neigh, n)} + # + # peers = dict() + # + # self.device.load_config('router bgp') + # + # for neighbor, parameters in neighbors.iteritems(): + # neigh_conf = self.device.running_config['router bgp']['neighbor']['{}'.format(neighbor)] + # peers[neighbor] = dict() + # peers[neighbor]['remote_as'] = int(neigh_conf.get_param('remote-as')) + # peers[neighbor]['is_enabled'] = neigh_conf.get_param('shutdown') != 'enable' or False + # peers[neighbor]['is_up'] = 'never' != parameters[7] or False + # peers[neighbor]['uptime'] = convert_uptime_string_seconds(parameters[7]) + # peers[neighbor]['accepted_prefixes'] = int(self.device.execute_command('get router info bgp neighbor {} | grep "accepted prefixes"'.format(neighbor))[0].split()[0]) + # peers[neighbor]['sent_prefixes'] = int(self.device.execute_command('get router info bgp neighbor {} | grep "announced prefixes"'.format(neighbor))[0].split()[0]) + # + # received = self.device.execute_command('get router info bgp neighbors {} received-routes | grep prefixes'.format(neighbor))[0].split() + # if len(received) > 0: + # peers[neighbor]['received_prefixes'] = int(received[-1]) + # else: + # # Soft-reconfig is not enabled + # peers[neighbor]['received_prefixes'] = 0 + # + # return { + # 'default': { + # 'local_as': int(bgp_sum[0].split()[7]), + # 'router_id': bgp_sum[0].split()[3], + # 'peers': peers + # } + # } def get_lldp_neighbors(self): return {} \ No newline at end of file From 20d03866939243efafc7ca2ec11a8d194d942141 Mon Sep 17 00:00:00 2001 From: David Barroso Date: Thu, 28 May 2015 16:45:11 -0400 Subject: [PATCH 23/29] Removing unused import --- napalm/fortios.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/napalm/fortios.py b/napalm/fortios.py index 35bd8b1..e67d80b 100644 --- a/napalm/fortios.py +++ b/napalm/fortios.py @@ -20,8 +20,6 @@ from exceptions import ReplaceConfigException, MergeConfigException from utils.string_parsers import colon_separated_string_to_dict, convert_uptime_string_seconds -import re - def execute_get(device, cmd, separator=':', auto=False): output = device.execute_command(cmd) From a155651e9ab5f55620db38d5447b3d8e215d1826 Mon Sep 17 00:00:00 2001 From: David Barroso Date: Thu, 28 May 2015 17:06:10 -0400 Subject: [PATCH 24/29] enhancements --- utils/string_parsers.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/utils/string_parsers.py b/utils/string_parsers.py index 15e293f..161d939 100644 --- a/utils/string_parsers.py +++ b/utils/string_parsers.py @@ -65,15 +65,14 @@ def convert_uptime_string_seconds(uptime): Convert uptime strings to seconds. The string can be formatted various ways, eg. 1 hour, 56 minutes ''' - regex_1 = re.compile(r"((?P\d+) day(s)?,\s+)?((?P\d+) hour(s)?,\s+)?((?P\d+) minute(s)?)") + regex_1 = re.compile(r"((?P\d+) week(s)?,\s+)?((?P\d+) day(s)?,\s+)?((?P\d+) hour(s)?,\s+)?((?P\d+) minute(s)?)") regex_2 = re.compile(r"((?P\d+)):((?P\d+)):((?P\d+))") regex_list = [regex_1, regex_2] - # say whaaaat!? uptime_dict = dict() for regex in regex_list: - uptime_dict = regex.match(uptime) + uptime_dict = regex.search(uptime) if uptime_dict is not None: uptime_dict = uptime_dict.groupdict() break @@ -83,7 +82,9 @@ def convert_uptime_string_seconds(uptime): for unit, value in uptime_dict.iteritems(): if value != None: - if unit == 'days': + if unit == 'weeks': + uptime_seconds += int(value) * 604800 + elif unit == 'days': uptime_seconds += int(value) * 86400 elif unit == 'hours': uptime_seconds += int(value) * 3600 From 6db2a3e9e7f534688ded2051311430bc32e706d7 Mon Sep 17 00:00:00 2001 From: David Barroso Date: Thu, 28 May 2015 18:10:37 -0400 Subject: [PATCH 25/29] removing mode for now --- napalm/fortios.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/napalm/fortios.py b/napalm/fortios.py index e67d80b..e153b97 100644 --- a/napalm/fortios.py +++ b/napalm/fortios.py @@ -166,7 +166,7 @@ class FortiOSDriver(NetworkDriver): ifs['is_enabled'] = self.device.running_config['system interface'][iface].get_param('status') != 'down' ifs['description'] = self.device.running_config['system interface'][iface].get_param('description') ifs['last_flapped'] = None - ifs['mode'] = 'routed' + #ifs['mode'] = 'routed' ifs['last_flapped'] = None interface_statistics[iface] = ifs except CommandExecutionException: From f81c31e60fd774e0b00d9f71f357be83f4653dbf Mon Sep 17 00:00:00 2001 From: David Barroso Date: Mon, 14 Sep 2015 12:26:24 +0200 Subject: [PATCH 26/29] Ignoring environment --- .gitignore | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.gitignore b/.gitignore index aa861ed..f1012bf 100644 --- a/.gitignore +++ b/.gitignore @@ -54,3 +54,5 @@ docs/_build/ .idea .DS_Store + +env From 2a0d5ba12fe823d55cb1206841040b94926c5ecf Mon Sep 17 00:00:00 2001 From: David Barroso Date: Mon, 14 Sep 2015 12:26:40 +0200 Subject: [PATCH 27/29] Implemented get_lldp_neighbors --- napalm/fortios.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/napalm/fortios.py b/napalm/fortios.py index e153b97..f0945e4 100644 --- a/napalm/fortios.py +++ b/napalm/fortios.py @@ -209,4 +209,4 @@ class FortiOSDriver(NetworkDriver): # } def get_lldp_neighbors(self): - return {} \ No newline at end of file + return {} From 604e7fe1d83b03c9328323d716f5546a708b553f Mon Sep 17 00:00:00 2001 From: David Barroso Date: Tue, 22 Sep 2015 13:14:48 +0200 Subject: [PATCH 28/29] junos getters work fine now --- utils/junos_views.yml | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/utils/junos_views.yml b/utils/junos_views.yml index 4477a9d..e3f3be5 100644 --- a/utils/junos_views.yml +++ b/utils/junos_views.yml @@ -1,7 +1,7 @@ --- #### -#### Interface table +#### Interface table #### junos_iface_table: @@ -18,7 +18,7 @@ junos_iface_view: is_enabled: { admin-status: True=up } description: description last_flapped: { interface-flapped/@seconds: int } - mode: logical-interface/address-family/address-family-name + #mode: logical-interface/address-family/address-family-name speed: speed mac_address: current-physical-address @@ -27,7 +27,7 @@ junos_iface_view: #### junos_route_instance_table: - rpc: get-instance-information + rpc: get-instance-information item: instance-core key: instance-name view: junos_route_instance_view @@ -54,7 +54,7 @@ junos_bgp_view: sent_prefixes: { bgp-rib/aadvertised-prefix-count: int } #### -#### LLDP table +#### LLDP table #### junos_lldp_table: @@ -67,5 +67,3 @@ junos_lldp_view: fields: hostname: lldp-remote-system-name port: lldp-remote-port-description | lldp-remote-port-id - - From 6a2fa1750bbeaf69f35227feb104d07e2d61e247 Mon Sep 17 00:00:00 2001 From: David Barroso Date: Thu, 1 Oct 2015 12:40:41 +0200 Subject: [PATCH 29/29] Added get_facts --- ansible/napalm_get_facts | 66 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 ansible/napalm_get_facts diff --git a/ansible/napalm_get_facts b/ansible/napalm_get_facts new file mode 100644 index 0000000..3f5933b --- /dev/null +++ b/ansible/napalm_get_facts @@ -0,0 +1,66 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +# Copyright 2014 Spotify AB. All rights reserved. +# +# The contents of this file are licensed under the Apache License, Version 2.0 +# (the "License"); you may not use this file except in compliance with the +# License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations under +# the License. + + +""" +""" + +import ast +import logging + +logger = logging.getLogger('eos_install_config') + +DOCUMENTATION = ''' +''' + +EXAMPLES = ''' +''' + + +def main(): + module = AnsibleModule( + argument_spec=dict( + hostname=dict(required=True), + username=dict(required=True), + password=dict(required=True), + dev_os=dict(required=True), + ), + supports_check_mode=True + ) + + hostname = module.params['hostname'] + username = module.params['username'] + dev_os = module.params['dev_os'] + password = module.params['password'] + + + network_driver = get_network_driver(dev_os) + + device = network_driver(hostname, username, password) + device.open() + + facts = device.get_facts() + facts['interface_details'] = device.get_interfaces() + + device.close() + + module.exit_json(ansible_facts=facts) + +from ansible.module_utils.basic import * +from napalm import get_network_driver + +main()