This repository has been archived by the owner on Sep 17, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 21
/
nxos_ssh.py
1518 lines (1319 loc) · 56.7 KB
/
nxos_ssh.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# -*- coding: utf-8 -*-
# Copyright 2015 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.
from __future__ import unicode_literals
# import stdlib
import re
import os
import time
import uuid
import tempfile
from scp import SCPClient
import paramiko
import hashlib
import socket
from datetime import datetime
# import third party lib
from netaddr import IPAddress
from netaddr.core import AddrFormatError
from netmiko import ConnectHandler
from netmiko.ssh_exception import NetMikoTimeoutException
# import NAPALM Base
import napalm_base.helpers
from napalm_base import NetworkDriver
from napalm_base.utils import py23_compat
from napalm_base.exceptions import ConnectionException
from napalm_base.exceptions import MergeConfigException
from napalm_base.exceptions import CommandErrorException
from napalm_base.exceptions import ReplaceConfigException
import napalm_base.constants as c
# Easier to store these as constants
HOUR_SECONDS = 3600
DAY_SECONDS = 24 * HOUR_SECONDS
WEEK_SECONDS = 7 * DAY_SECONDS
YEAR_SECONDS = 365 * DAY_SECONDS
# STD REGEX PATTERNS
IP_ADDR_REGEX = r"\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}"
IPV4_ADDR_REGEX = IP_ADDR_REGEX
IPV6_ADDR_REGEX_1 = r"::"
IPV6_ADDR_REGEX_2 = r"[0-9a-fA-F:]{1,39}::[0-9a-fA-F:]{1,39}"
IPV6_ADDR_REGEX_3 = r"[0-9a-fA-F]{1,3}:[0-9a-fA-F]{1,3}:[0-9a-fA-F]{1,3}:[0-9a-fA-F]{1,3}:" \
"[0-9a-fA-F]{1,3}:[0-9a-fA-F]{1,3}:[0-9a-fA-F]{1,3}:[0-9a-fA-F]{1,3}"
# Should validate IPv6 address using an IP address library after matching with this regex
IPV6_ADDR_REGEX = "(?:{}|{}|{})".format(IPV6_ADDR_REGEX_1, IPV6_ADDR_REGEX_2, IPV6_ADDR_REGEX_3)
IPV4_OR_IPV6_REGEX = "(?:{}|{})".format(IPV4_ADDR_REGEX, IPV6_ADDR_REGEX)
MAC_REGEX = r"[a-fA-F0-9]{4}\.[a-fA-F0-9]{4}\.[a-fA-F0-9]{4}"
VLAN_REGEX = r"\d{1,4}"
RE_IPADDR = re.compile(r"{}".format(IP_ADDR_REGEX))
RE_MAC = re.compile(r"{}".format(MAC_REGEX))
# Period needed for 32-bit AS Numbers
ASN_REGEX = r"[\d\.]+"
def parse_intf_section(interface):
"""Parse a single entry from show interfaces output.
Different cases:
mgmt0 is up
admin state is up
Ethernet2/1 is up
admin state is up, Dedicated Interface
Vlan1 is down (Administratively down), line protocol is down, autostate enabled
Ethernet154/1/48 is up (with no 'admin state')
"""
interface = interface.strip()
re_protocol = r"^(?P<intf_name>\S+?)\s+is\s+(?P<status>.+?)" \
r",\s+line\s+protocol\s+is\s+(?P<protocol>\S+).*$"
re_intf_name_state = r"^(?P<intf_name>\S+) is (?P<intf_state>\S+).*"
re_is_enabled_1 = r"^admin state is (?P<is_enabled>\S+)$"
re_is_enabled_2 = r"^admin state is (?P<is_enabled>\S+), "
re_is_enabled_3 = r"^.* is down.*Administratively down.*$"
re_mac = r"^\s+Hardware.*address:\s+(?P<mac_address>\S+) "
re_speed = r"^\s+MTU .*, BW (?P<speed>\S+) (?P<speed_unit>\S+), "
re_description = r"^\s+Description:\s+(?P<description>.*)$"
# Check for 'protocol is ' lines
match = re.search(re_protocol, interface, flags=re.M)
if match:
intf_name = match.group('intf_name')
status = match.group('status')
protocol = match.group('protocol')
if 'admin' in status.lower():
is_enabled = False
else:
is_enabled = True
is_up = bool('up' in protocol)
else:
# More standard is up, next line admin state is lines
match = re.search(re_intf_name_state, interface)
intf_name = match.group('intf_name')
intf_state = match.group('intf_state').strip()
is_up = True if intf_state == 'up' else False
admin_state_present = re.search("admin state is", interface)
if admin_state_present:
# Parse cases where 'admin state' string exists
for x_pattern in [re_is_enabled_1, re_is_enabled_2]:
match = re.search(x_pattern, interface, flags=re.M)
if match:
is_enabled = match.group('is_enabled').strip()
is_enabled = True if is_enabled == 'up' else False
break
else:
msg = "Error parsing intf, 'admin state' never detected:\n\n{}".format(interface)
raise ValueError(msg)
else:
# No 'admin state' should be 'is up' or 'is down' strings
# If interface is up; it is enabled
is_enabled = True
if not is_up:
match = re.search(re_is_enabled_3, interface, flags=re.M)
if match:
is_enabled = False
match = re.search(re_mac, interface, flags=re.M)
if match:
mac_address = match.group('mac_address')
mac_address = napalm_base.helpers.mac(mac_address)
else:
mac_address = ""
match = re.search(re_speed, interface, flags=re.M)
speed = int(match.group('speed'))
speed_unit = match.group('speed_unit')
# This was alway in Kbit (in the data I saw)
if speed_unit != "Kbit":
msg = "Unexpected speed unit in show interfaces parsing:\n\n{}".format(interface)
raise ValueError(msg)
speed = int(round(speed / 1000.0))
description = ''
match = re.search(re_description, interface, flags=re.M)
if match:
description = match.group('description')
return {
intf_name: {
'description': description,
'is_enabled': is_enabled,
'is_up': is_up,
'last_flapped': -1.0,
'mac_address': mac_address,
'speed': speed}
}
def convert_hhmmss(hhmmss):
"""Convert hh:mm:ss to seconds."""
fields = hhmmss.split(":")
if len(fields) != 3:
raise ValueError("Received invalid HH:MM:SS data: {}".format(hhmmss))
fields = [int(x) for x in fields]
hours, minutes, seconds = fields
return (hours * 3600) + (minutes * 60) + seconds
def bgp_time_conversion(bgp_uptime):
"""Convert string time to seconds.
Examples
00:14:23
00:13:40
00:00:21
00:00:13
00:00:49
1d11h
1d17h
1w0d
8w5d
1y28w
never
"""
bgp_uptime = bgp_uptime.strip()
uptime_letters = set(['w', 'h', 'd'])
if 'never' in bgp_uptime:
return -1
elif ':' in bgp_uptime:
times = bgp_uptime.split(":")
times = [int(x) for x in times]
hours, minutes, seconds = times
return (hours * 3600) + (minutes * 60) + seconds
# Check if any letters 'w', 'h', 'd' are in the time string
elif uptime_letters & set(bgp_uptime):
form1 = r'(\d+)d(\d+)h' # 1d17h
form2 = r'(\d+)w(\d+)d' # 8w5d
form3 = r'(\d+)y(\d+)w' # 1y28w
match = re.search(form1, bgp_uptime)
if match:
days = int(match.group(1))
hours = int(match.group(2))
return (days * DAY_SECONDS) + (hours * 3600)
match = re.search(form2, bgp_uptime)
if match:
weeks = int(match.group(1))
days = int(match.group(2))
return (weeks * WEEK_SECONDS) + (days * DAY_SECONDS)
match = re.search(form3, bgp_uptime)
if match:
years = int(match.group(1))
weeks = int(match.group(2))
return (years * YEAR_SECONDS) + (weeks * WEEK_SECONDS)
raise ValueError("Unexpected value for BGP uptime string: {}".format(bgp_uptime))
def bgp_normalize_table_data(bgp_table):
"""The 'show bgp all summary vrf all' table can have entries that wrap multiple lines.
2001:db8:4:701::2
4 65535 163664 163693 145 0 0 3w2d 3
2001:db8:e0:dd::1
4 10 327491 327278 145 0 0 3w1d 4
Normalize this so the line wrap doesn't exit.
"""
bgp_table = bgp_table.strip()
bgp_multiline_pattern = r"({})\s*\n".format(IPV4_OR_IPV6_REGEX)
# Strip out the newline
return re.sub(bgp_multiline_pattern, r'\1', bgp_table)
def bgp_table_parser(bgp_table):
"""Generator that parses a line of bgp summary table and returns a dict compatible with NAPALM
Example line:
10.2.1.14 4 10 472516 472238 361 0 0 3w1d 9
"""
bgp_table = bgp_table.strip()
for bgp_entry in bgp_table.splitlines():
bgp_table_fields = bgp_entry.split()
try:
if re.search(r'Shut.*Admin', bgp_entry):
(peer_ip, bgp_version, remote_as, msg_rcvd, msg_sent, _, _, _,
uptime, state_1, state_2) = bgp_table_fields
state_pfxrcd = "{} {}".format(state_1, state_2)
else:
(peer_ip, bgp_version, remote_as, msg_rcvd, msg_sent, _, _, _,
uptime, state_pfxrcd) = bgp_table_fields
except ValueError:
raise ValueError("Unexpected entry ({}) in BGP summary table".format(bgp_table_fields))
is_enabled = True
try:
received_prefixes = int(state_pfxrcd)
is_up = True
except ValueError:
received_prefixes = -1
is_up = False
if re.search(r'Shut.*Admin', state_pfxrcd):
is_enabled = False
if not is_up:
uptime = -1
if uptime != -1:
uptime = bgp_time_conversion(uptime)
yield {
peer_ip: {
"is_enabled": is_enabled,
"uptime": uptime,
"remote_as": napalm_base.helpers.as_number(remote_as),
"is_up": is_up,
"description": "",
"received_prefixes": received_prefixes,
}
}
def bgp_summary_parser(bgp_summary):
"""Parse 'show bgp all summary vrf' output information from NX-OS devices."""
bgp_summary_dict = {}
# Check for BGP summary information lines that have no data
if len(bgp_summary.strip().splitlines()) <= 1:
return {}
allowed_afi = ['ipv4', 'ipv6']
vrf_regex = r"^BGP summary information for VRF\s+(?P<vrf>\S+),"
afi_regex = r"^BGP summary information.*address family (?P<afi>\S+ Unicast)"
local_router_regex = (r"^BGP router identifier\s+(?P<router_id>\S+)"
r",\s+local AS number\s+(?P<local_as>\S+)")
for pattern in [vrf_regex, afi_regex, local_router_regex]:
match = re.search(pattern, bgp_summary, flags=re.M)
if match:
bgp_summary_dict.update(match.groupdict(1))
# Some post regex cleanup and validation
vrf = bgp_summary_dict['vrf']
if vrf.lower() == 'default':
bgp_summary_dict['vrf'] = 'global'
afi = bgp_summary_dict['afi']
afi = afi.split()[0].lower()
if afi not in allowed_afi:
raise ValueError("AFI ({}) is invalid and not supported.".format(afi))
bgp_summary_dict['afi'] = afi
local_as = bgp_summary_dict['local_as']
local_as = napalm_base.helpers.as_number(local_as)
match = re.search(IPV4_ADDR_REGEX, bgp_summary_dict['router_id'])
if not match:
raise ValueError("BGP router_id ({}) is not valid".format(bgp_summary_dict['router_id']))
vrf = bgp_summary_dict['vrf']
bgp_return_dict = {
vrf: {
"router_id": bgp_summary_dict['router_id'],
"peers": {},
}
}
# Extract and process the tabular data
tabular_divider = r"^Neighbor\s+.*PfxRcd$"
tabular_data = re.split(tabular_divider, bgp_summary, flags=re.M)
if len(tabular_data) != 2:
msg = "Unexpected data processing BGP summary information:\n\n{}".format(bgp_summary)
raise ValueError(msg)
tabular_data = tabular_data[1]
bgp_table = bgp_normalize_table_data(tabular_data)
for bgp_entry in bgp_table_parser(bgp_table):
bgp_return_dict[vrf]["peers"].update(bgp_entry)
bgp_new_dict = {}
for neighbor, bgp_data in bgp_return_dict[vrf]["peers"].items():
received_prefixes = bgp_data.pop("received_prefixes")
bgp_data["address_family"] = {}
prefixes_dict = {"sent_prefixes": -1,
"accepted_prefixes": -1,
"received_prefixes": received_prefixes}
bgp_data["address_family"][afi] = prefixes_dict
bgp_data["local_as"] = local_as
# FIX, hard-coding
bgp_data["remote_id"] = "0.0.0.0"
bgp_new_dict[neighbor] = bgp_data
bgp_return_dict[vrf]["peers"] = bgp_new_dict
return bgp_return_dict
class NXOSSSHDriver(NetworkDriver):
def __init__(self, hostname, username, password, timeout=60, optional_args=None):
if optional_args is None:
optional_args = {}
self.hostname = hostname
self.username = username
self.password = password
self.timeout = timeout
self.up = False
self.replace = True
self.loaded = False
self.fc = None
self.changed = False
self.replace_file = None
self.merge_candidate = ''
if optional_args is None:
optional_args = {}
# Netmiko possible arguments
netmiko_argument_map = {
'port': None,
'verbose': False,
'timeout': self.timeout,
'global_delay_factor': 1,
'use_keys': False,
'key_file': None,
'ssh_strict': False,
'system_host_keys': False,
'alt_host_keys': False,
'alt_key_file': '',
'ssh_config_file': None,
'allow_agent': False
}
# Build dict of any optional Netmiko args
self.netmiko_optional_args = {
k: optional_args.get(k, v)
for k, v in netmiko_argument_map.items()
}
self.port = optional_args.get('port', 22)
self.sudo_pwd = optional_args.get('sudo_pwd', self.password)
def open(self):
try:
self.device = ConnectHandler(device_type='cisco_nxos',
host=self.hostname,
username=self.username,
password=self.password,
**self.netmiko_optional_args)
self.device.enable()
except NetMikoTimeoutException:
raise ConnectionException('Cannot connect to {}'.format(self.hostname))
def close(self):
if self.changed:
self._delete_file(self.backup_file)
self.device.disconnect()
self.device = None
@staticmethod
def parse_uptime(uptime_str):
"""
Extract the uptime string from the given Cisco IOS Device.
Return the uptime in seconds as an integer
"""
# Initialize to zero
(years, weeks, days, hours, minutes) = (0, 0, 0, 0, 0)
uptime_str = uptime_str.strip()
time_list = uptime_str.split(',')
for element in time_list:
if re.search("year", element):
years = int(element.split()[0])
elif re.search("week", element):
weeks = int(element.split()[0])
elif re.search("day", element):
days = int(element.split()[0])
elif re.search("hour", element):
hours = int(element.split()[0])
elif re.search("minute", element):
minutes = int(element.split()[0])
elif re.search("second", element):
seconds = int(element.split()[0])
uptime_sec = (years * YEAR_SECONDS) + (weeks * WEEK_SECONDS) + (days * DAY_SECONDS) + \
(hours * 3600) + (minutes * 60) + seconds
return uptime_sec
def is_alive(self):
"""Returns a flag with the state of the SSH connection."""
null = chr(0)
try:
if self.device is None:
return {'is_alive': False}
else:
# Try sending ASCII null byte to maintain the connection alive
self.device.send_command(null)
except (socket.error, EOFError):
# If unable to send, we can tell for sure that the connection is unusable,
# hence return False.
return {'is_alive': False}
return {
'is_alive': self.device.remote_conn.transport.is_active()
}
def load_replace_candidate(self, filename=None, config=None):
self._replace_candidate(filename, config)
self.replace = True
self.loaded = True
def _get_flash_size(self):
command = 'dir {}'.format('bootflash:')
output = self.device.send_command(command)
match = re.search(r'(\d+) bytes free', output)
bytes_free = match.group(1)
return int(bytes_free)
def _enough_space(self, filename):
flash_size = self._get_flash_size()
file_size = os.path.getsize(filename)
if file_size > flash_size:
return False
return True
def _verify_remote_file_exists(self, dst, file_system='bootflash:'):
command = 'dir {0}/{1}'.format(file_system, dst)
output = self.device.send_command(command)
if 'No such file' in output:
raise ReplaceConfigException('Could not transfer file.')
def _replace_candidate(self, filename, config):
if not filename:
filename = self._create_tmp_file(config)
else:
if not os.path.isfile(filename):
raise ReplaceConfigException("File {} not found".format(filename))
self.replace_file = filename
if not self._enough_space(self.replace_file):
msg = 'Could not transfer file. Not enough space on device.'
raise ReplaceConfigException(msg)
self._check_file_exists(self.replace_file)
dest = os.path.basename(self.replace_file)
full_remote_path = 'bootflash:{}'.format(dest)
with paramiko.SSHClient() as ssh:
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(hostname=self.hostname, username=self.username, password=self.password)
try:
with SCPClient(ssh.get_transport()) as scp_client:
scp_client.put(self.replace_file, full_remote_path)
except Exception:
time.sleep(10)
file_size = os.path.getsize(filename)
temp_size = self._verify_remote_file_exists(dest)
if int(temp_size) != int(file_size):
msg = ('Could not transfer file. There was an error '
'during transfer. Please make sure remote '
'permissions are set.')
raise ReplaceConfigException(msg)
self.config_replace = True
if config and os.path.isfile(self.replace_file):
os.remove(self.replace_file)
def _file_already_exists(self, dst):
dst_hash = self._get_remote_md5(dst)
src_hash = self._get_local_md5(dst)
if src_hash == dst_hash:
return True
return False
def _check_file_exists(self, cfg_file):
command = 'dir {}'.format(cfg_file)
output = self.device.send_command(command)
if 'No such file' in output:
return False
else:
return self._file_already_exists(cfg_file)
def _get_remote_md5(self, dst):
command = 'show file {0} md5sum'.format(dst)
return self.device.send_command(command).strip()
def _get_local_md5(self, dst, blocksize=2**20):
md5 = hashlib.md5()
local_file = open(dst, 'rb')
buf = local_file.read(blocksize)
while buf:
md5.update(buf)
buf = local_file.read(blocksize)
local_file.close()
return md5.hexdigest()
def load_merge_candidate(self, filename=None, config=None):
self.replace = False
self.loaded = True
if not filename and not config:
raise MergeConfigException('filename or config param must be provided.')
self.merge_candidate += '\n' # insert one extra line
if filename is not None:
with open(filename, "r") as f:
self.merge_candidate += f.read()
else:
self.merge_candidate += config
@staticmethod
def _create_tmp_file(config):
tmp_dir = tempfile.gettempdir()
rand_fname = py23_compat.text_type(uuid.uuid4())
filename = os.path.join(tmp_dir, rand_fname)
with open(filename, 'wt') as fobj:
fobj.write(config)
return filename
def _create_sot_file(self):
"""Create Source of Truth file to compare."""
commands = ['terminal dont-ask', 'checkpoint file sot_file']
self._send_config_commands(commands)
def _get_diff(self):
"""Get a diff between running config and a proposed file."""
diff = []
self._create_sot_file()
command = ('show diff rollback-patch file {0} file {1}'.format(
'sot_file', self.replace_file.split('/')[-1]))
diff_out = self.device.send_command(command)
try:
diff_out = diff_out.split(
'#Generating Rollback Patch')[1].replace(
'Rollback Patch is Empty', '').strip()
for line in diff_out.splitlines():
if line:
if line[0].strip() != '!' and line[0].strip() != '.':
diff.append(line.rstrip(' '))
except (AttributeError, KeyError):
raise ReplaceConfigException(
'Could not calculate diff. It\'s possible the given file doesn\'t exist.')
return '\n'.join(diff)
def _get_merge_diff(self):
diff = []
running_config = self.get_config(retrieve='running')['running']
running_lines = running_config.splitlines()
for line in self.merge_candidate.splitlines():
if line not in running_lines and line:
if line[0].strip() != '!':
diff.append(line)
return '\n'.join(diff)
# the merge diff is not necessarily what needs to be loaded
# for example under NTP, as the `ntp commit` command might be
# alread configured, it is mandatory to be sent
# otherwise it won't take the new configuration - see #59
# https://github.com/napalm-automation/napalm-nxos/issues/59
# therefore this method will return the real diff
# but the merge_candidate will remain unchanged
# previously: self.merge_candidate = '\n'.join(diff)
def compare_config(self):
if self.loaded:
if not self.replace:
return self._get_merge_diff()
# return self.merge_candidate
diff = self._get_diff()
return diff
return ''
def _save(self, filename='startup-config'):
command = 'copy run %s' % filename
output = self.device.send_command(command)
if 'complete' in output.lower():
return True
return False
def _commit_merge(self):
commands = [command for command in self.merge_candidate.splitlines() if command]
output = self.device.send_config_set(commands)
if 'Invalid command' in output:
raise MergeConfigException('Error while applying config!')
if not self._save():
raise CommandErrorException('Unable to save running-config to startup!')
def _save_to_checkpoint(self, filename):
"""Save the current running config to the given file."""
command = 'checkpoint file {}'.format(filename)
self.device.send_command(command)
def _disable_confirmation(self):
self._send_config_commands(['terminal dont-ask'])
def _load_cfg_from_checkpoint(self):
command = 'rollback running file {0}'.format(self.replace_file.split('/')[-1])
self._disable_confirmation()
rollback_result = self.device.send_command(command)
if 'Rollback failed.' in rollback_result or 'ERROR' in rollback_result:
raise ReplaceConfigException(rollback_result)
elif rollback_result == []:
return False
return True
def commit_config(self):
if self.loaded:
self.backup_file = 'config_' + str(datetime.now()).replace(' ', '_')
# Create Checkpoint from current running-config
self._save_to_checkpoint(self.backup_file)
if self.replace:
cfg_replace_status = self._load_cfg_from_checkpoint()
if not cfg_replace_status:
raise ReplaceConfigException
else:
try:
self._commit_merge()
self.merge_candidate = '' # clear the merge buffer
except Exception as e:
raise MergeConfigException(str(e))
self.changed = True
self.loaded = False
else:
raise ReplaceConfigException('No config loaded.')
def _delete_file(self, filename):
commands = [
'terminal dont-ask',
'delete {}'.format(filename),
'no terminal dont-ask'
]
for command in commands:
self.device.send_command(command)
def discard_config(self):
if self.loaded:
self.merge_candidate = '' # clear the buffer
if self.loaded and self.replace:
self._delete_file(self.replace_file)
self.loaded = False
def rollback(self):
if self.changed:
command = 'rollback running-config file {}'.format(self.backup_file)
result = self.device.send_command(command)
if 'completed' not in result.lower():
raise ReplaceConfigException(result)
if not self._save():
raise CommandErrorException('Unable to save running-config to startup!')
self.changed = False
def _apply_key_map(self, key_map, table):
new_dict = {}
for key, value in table.items():
new_key = key_map.get(key)
if new_key:
new_dict[new_key] = str(value)
return new_dict
def _convert_uptime_to_seconds(self, uptime_facts):
seconds = int(uptime_facts['up_days']) * 24 * 60 * 60
seconds += int(uptime_facts['up_hours']) * 60 * 60
seconds += int(uptime_facts['up_mins']) * 60
seconds += int(uptime_facts['up_secs'])
return seconds
def get_facts(self):
"""Return a set of facts from the devices."""
# default values.
vendor = u'Cisco'
uptime = -1
serial_number, fqdn, os_version, hostname, domain_name = ('',) * 5
# obtain output from device
show_ver = self.device.send_command('show version')
show_hosts = self.device.send_command('show hosts')
show_int_status = self.device.send_command('show interface status')
show_hostname = self.device.send_command('show hostname')
# uptime/serial_number/IOS version
for line in show_ver.splitlines():
if ' uptime is ' in line:
_, uptime_str = line.split(' uptime is ')
uptime = self.parse_uptime(uptime_str)
if 'Processor Board ID' in line:
_, serial_number = line.split("Processor Board ID ")
serial_number = serial_number.strip()
if 'system: ' in line:
line = line.strip()
os_version = line.split()[2]
os_version = os_version.strip()
if 'cisco' in line and 'Chassis' in line:
_, model = line.split()[:2]
model = model.strip()
hostname = show_hostname.strip()
# Determine domain_name and fqdn
for line in show_hosts.splitlines():
if 'Default domain' in line:
_, domain_name = re.split(r".*Default domain.*is ", line)
domain_name = domain_name.strip()
break
if hostname.count(".") >= 2:
fqdn = hostname
elif domain_name:
fqdn = '{}.{}'.format(hostname, domain_name)
# interface_list filter
interface_list = []
show_int_status = show_int_status.strip()
for line in show_int_status.splitlines():
if line.startswith(' ') or line.startswith('-') or line.startswith('Port '):
continue
interface = line.split()[0]
interface_list.append(interface)
return {
'uptime': int(uptime),
'vendor': vendor,
'os_version': py23_compat.text_type(os_version),
'serial_number': py23_compat.text_type(serial_number),
'model': py23_compat.text_type(model),
'hostname': py23_compat.text_type(hostname),
'fqdn': fqdn,
'interface_list': interface_list
}
def get_interfaces(self):
"""
Get interface details.
last_flapped is not implemented
Example Output:
{ u'Vlan1': { 'description': u'',
'is_enabled': True,
'is_up': True,
'last_flapped': -1.0,
'mac_address': u'a493.4cc1.67a7',
'speed': 100},
u'Vlan100': { 'description': u'Data Network',
'is_enabled': True,
'is_up': True,
'last_flapped': -1.0,
'mac_address': u'a493.4cc1.67a7',
'speed': 100},
u'Vlan200': { 'description': u'Voice Network',
'is_enabled': True,
'is_up': True,
'last_flapped': -1.0,
'mac_address': u'a493.4cc1.67a7',
'speed': 100}}
"""
interfaces = {}
command = 'show interface'
output = self.device.send_command(command)
if not output:
return {}
# Break output into per-interface sections (note, separator text is retained)
separator1 = r"^\S+\s+is \S+.*\nadmin state is.*$"
separator2 = r"^.* is .*, line protocol is .*$"
separator3 = r"^.* is (?:down|up).*$"
separators = r"({}|{}|{})".format(separator1, separator2, separator3)
interface_lines = re.split(separators, output, flags=re.M)
if len(interface_lines) == 1:
msg = "Unexpected output data in '{}':\n\n{}".format(command, interface_lines)
raise ValueError(msg)
# Get rid of the blank data at the beginning
interface_lines.pop(0)
# Must be pairs of data (the separator and section corresponding to it)
if len(interface_lines) % 2 != 0:
msg = "Unexpected output data in '{}':\n\n{}".format(command, interface_lines)
raise ValueError(msg)
# Combine the separator and section into one string
intf_iter = iter(interface_lines)
try:
new_interfaces = [line + next(intf_iter, '') for line in intf_iter]
except TypeError:
raise ValueError()
for entry in new_interfaces:
interfaces.update(parse_intf_section(entry))
return interfaces
def get_lldp_neighbors(self):
results = {}
command = 'show lldp neighbors'
output = self.device.send_command(command)
lldp_neighbors = napalm_base.helpers.textfsm_extractor(
self, 'lldp_neighbors', output)
for neighbor in lldp_neighbors:
local_iface = neighbor.get('local_interface')
if neighbor.get(local_iface) is None:
if local_iface not in results:
results[local_iface] = []
neighbor_dict = {}
neighbor_dict['hostname'] = py23_compat.text_type(neighbor.get('neighbor'))
neighbor_dict['port'] = py23_compat.text_type(neighbor.get('neighbor_interface'))
results[local_iface].append(neighbor_dict)
return results
def get_bgp_neighbors(self):
"""BGP neighbor information.
Supports VRFs and IPv4 and IPv6 AFIs
{
"global": {
"router_id": "1.1.1.103",
"peers": {
"10.99.99.2": {
"is_enabled": true,
"uptime": -1,
"remote_as": 22,
"address_family": {
"ipv4": {
"sent_prefixes": -1,
"accepted_prefixes": -1,
"received_prefixes": -1
}
},
"remote_id": "0.0.0.0",
"local_as": 22,
"is_up": false,
"description": ""
}
}
}
"""
bgp_dict = {}
# get summary output from device
cmd_bgp_all_sum = 'show bgp all summary vrf all'
bgp_summary_output = self.device.send_command(cmd_bgp_all_sum).strip()
section_separator = r"BGP summary information for "
bgp_summary_sections = re.split(section_separator, bgp_summary_output)
if len(bgp_summary_sections):
bgp_summary_sections.pop(0)
for bgp_section in bgp_summary_sections:
bgp_section = section_separator + bgp_section
bgp_dict.update(bgp_summary_parser(bgp_section))
# FIX -- look up logical or behavior we did in Cisco IOS bgp parser (make consistent here)
# FIX -- need to merge IPv6 and IPv4 AFI for same neighbor
return bgp_dict
def _send_config_commands(self, commands):
for command in commands:
self.device.send_command(command)
def _set_checkpoint(self, filename):
commands = ['terminal dont-ask', 'checkpoint file {0}'.format(filename)]
self._send_config_commands(commands)
def _get_checkpoint_file(self):
filename = 'temp_cp_file_from_napalm'
self._set_checkpoint(filename)
command = 'show file {0}'.format(filename)
output = self.device.send_command(command)
self._delete_file(filename)
return output
def get_lldp_neighbors_detail(self, interface=''):
lldp_neighbors = {}
filter = ''
if interface:
filter = 'interface {name} '.format(name=interface)
command = 'show lldp neighbors {filter}detail'.format(filter=filter)
# seems that some old devices may not return JSON output...
output = self.device.send_command(command)
# thus we need to take the raw text output
lldp_neighbors_list = output.splitlines()
if not lldp_neighbors_list:
return lldp_neighbors # empty dict
CHASSIS_REGEX = '^(Chassis id:)\s+([a-z0-9\.]+)$'
PORT_REGEX = '^(Port id:)\s+([0-9]+)$'
LOCAL_PORT_ID_REGEX = '^(Local Port id:)\s+(.*)$'
PORT_DESCR_REGEX = '^(Port Description:)\s+(.*)$'
SYSTEM_NAME_REGEX = '^(System Name:)\s+(.*)$'
SYSTEM_DESCR_REGEX = '^(System Description:)\s+(.*)$'
SYST_CAPAB_REEGX = '^(System Capabilities:)\s+(.*)$'
ENABL_CAPAB_REGEX = '^(Enabled Capabilities:)\s+(.*)$'
VLAN_ID_REGEX = '^(Vlan ID:)\s+(.*)$'
lldp_neighbor = {}
interface_name = None
for line in lldp_neighbors_list:
chassis_rgx = re.search(CHASSIS_REGEX, line, re.I)
if chassis_rgx:
lldp_neighbor = {
'remote_chassis_id': napalm_base.helpers.mac(chassis_rgx.groups()[1])
}
continue
lldp_neighbor['parent_interface'] = ''
port_rgx = re.search(PORT_REGEX, line, re.I)
if port_rgx:
lldp_neighbor['parent_interface'] = py23_compat.text_type(port_rgx.groups()[1])
continue
local_port_rgx = re.search(LOCAL_PORT_ID_REGEX, line, re.I)
if local_port_rgx:
interface_name = local_port_rgx.groups()[1]
continue
port_descr_rgx = re.search(PORT_DESCR_REGEX, line, re.I)
if port_descr_rgx:
lldp_neighbor['remote_port'] = py23_compat.text_type(port_descr_rgx.groups()[1])
lldp_neighbor['remote_port_description'] = py23_compat.text_type(
port_descr_rgx.groups()[1])
continue
syst_name_rgx = re.search(SYSTEM_NAME_REGEX, line, re.I)
if syst_name_rgx: