-
Notifications
You must be signed in to change notification settings - Fork 373
/
show_interface.py
executable file
·2985 lines (2669 loc) · 131 KB
/
show_interface.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
"""
show_interface.py
IOSXE parsers for the following show commands:
* show interfaces
* show ip interfaces <interface>
* show ip interface brief
* show ip interface brief | include Vlan
* show interfaces switchport
* show ip interface
* show interfaces <interface>
* show ipv6 interface
* show interfaces accounting
"""
import os
import logging
import pprint
import re
import unittest
from genie import parsergen
from collections import defaultdict
from ats.log.utils import banner
import xmltodict
try:
import iptools
from cnetconf import testmodel
except (ImportError, OSError):
pass
try:
from ats import tcl
except Exception:
pass
from genie.metaparser import MetaParser
from genie.metaparser.util import merge_dict, keynames_convert
from genie.metaparser.util.schemaengine import Schema, \
Any, \
Optional, \
Or, \
And, \
Default, \
Use
# import parser utils
from genie.libs.parser.utils.common import Common
logger = logging.getLogger(__name__)
class ShowInterfacesSchema(MetaParser):
"""schema for show interfaces
show interfaces <interface>"""
schema = {
Any(): {
'oper_status': str,
Optional('line_protocol'): str,
'enabled': bool,
Optional('connected'): bool,
Optional('description'): str,
'type': str,
Optional('link_state'): str,
Optional('port_speed'): str,
Optional('duplex_mode'): str,
Optional('link_type'): str,
Optional('media_type'): str,
'mtu': int,
Optional('medium'): str,
Optional('reliability'): str,
Optional('txload'): str,
Optional('rxload'): str,
Optional('mac_address'): str,
Optional('phys_address'): str,
Optional('delay'): int,
Optional('carrier_delay'): int,
Optional('carrier_delay_up'): int,
Optional('carrier_delay_down'): int,
Optional('keepalive'): int,
Optional('auto_negotiate'): bool,
Optional('arp_type'): str,
Optional('arp_timeout'): str,
Optional('last_input'): str,
Optional('last_output'): str,
Optional('output_hang'): str,
Optional('queues'): {
Optional('input_queue_size'): int,
Optional('input_queue_max'): int,
Optional('input_queue_drops'): int,
Optional('input_queue_flushes'): int,
Optional('total_output_drop'): int,
Optional('queue_strategy'): str,
Optional('output_queue_size'): int,
Optional('output_queue_max'): int
},
Optional('flow_control'):
{Optional('receive'): bool,
Optional('send'): bool,
},
Optional('port_channel'):
{Optional('port_channel_member'): bool,
Optional('port_channel_int'): str,
Optional('port_channel_member_intfs'): list,
Optional('active_members'): int,
Optional('num_of_pf_jumbo_supported_members'): int,
},
'bandwidth': int,
Optional('counters'):
{Optional('rate'):
{Optional('load_interval'): int,
Optional('in_rate'): int,
Optional('in_rate_pkts'): int,
Optional('out_rate'): int,
Optional('out_rate_pkts'): int,
Optional('in_rate_bps'): int,
Optional('in_rate_pps'): int,
Optional('out_rate_bps'): int,
Optional('out_rate_pps'): int,
},
Optional('in_multicast_pkts'): int,
Optional('in_broadcast_pkts'): int,
Optional('in_crc_errors'): int,
Optional('in_giants'): int,
Optional('in_pkts'): int,
Optional('in_frame'): int,
Optional('in_runts'): int,
Optional('in_overrun'): int,
Optional('in_ignored'): int,
Optional('in_watchdog'): int,
Optional('in_with_dribble'): int,
Optional('in_octets'): int,
Optional('in_errors'): int,
Optional('in_abort'): int,
Optional('in_no_buffer'): int,
Optional('in_throttles'): int,
Optional('in_mac_pause_frames'): int,
Optional('out_pkts'): int,
Optional('out_octets'): int,
Optional('out_multicast_pkts'): int,
Optional('out_broadcast_pkts'): int,
Optional('out_errors'): int,
Optional('out_collision'): int,
Optional('out_interface_resets'): int,
Optional('out_unknown_protocl_drops'): int,
Optional('out_babbles'): int,
Optional('out_deferred'): int,
Optional('out_underruns'): int,
Optional('out_late_collision'): int,
Optional('out_lost_carrier'): int,
Optional('out_no_carrier'): int,
Optional('out_babble'): int,
Optional('out_mac_pause_frames'): int,
Optional('out_buffer_failure'): int,
Optional('out_buffers_swapped'): int,
Optional('last_clear'): str,
},
Optional('encapsulations'):
{Optional('encapsulation'): str,
Optional('first_dot1q'): str,
Optional('second_dot1q'): str,
Optional('native_vlan'): int,
},
Optional('ipv4'):
{Any():
{Optional('ip'): str,
Optional('prefix_length'): str,
Optional('secondary'): bool
},
Optional('unnumbered'): {
'interface_ref': str,
},
},
},
}
class ShowInterfaces(ShowInterfacesSchema):
"""parser for show interfaces
show interfaces <interface>"""
cli_command = ['show interfaces','show interfaces {interface}']
exclude = ['in_octets', 'in_pkts', 'out_octets', 'out_pkts',
'in_rate', 'in_rate_pkts', 'out_rate', 'out_rate_pkts',
'input_queue_size', 'in_broadcast_pkts', 'in_multicast_pkts',
'last_output', 'out_unknown_protocl_drops', 'last_input',
'input_queue_drops', 'out_interface_resets', 'rxload',
'txload', 'last_clear', 'in_crc_errors', 'in_errors',
'in_giants', 'unnumbered', 'mac_address', 'phys_address',
'out_lost_carrier', '(Tunnel.*)', 'input_queue_flushes',
'reliability']
def cli(self,interface="",output=None):
if output is None:
if interface:
cmd = self.cli_command[1].format(interface=interface)
else:
cmd = self.cli_command[0]
out = self.device.execute(cmd)
else:
out = output
interface_dict = {}
unnumbered_dict = {}
for line in out.splitlines():
line = line.strip()
# GigabitEthernet1 is up, line protocol is up
# Port-channel12 is up, line protocol is up (connected)
# Vlan1 is administratively down, line protocol is down , Autostate Enabled
p1 = re.compile(r'^(?P<interface>[\w\/\.\-]+) +is'
' +(?P<enabled>[\w\s]+),'
' +line +protocol +is +(?P<line_protocol>\w+)'
'( *\((?P<attribute>\S+)\))?$')
p1_1 = re.compile(r'^(?P<interface>[\w\/\.\-]+) +is'
' +(?P<enabled>[\w\s]+),'
' +line +protocol +is +(?P<line_protocol>\w+)'
'( *, *(?P<attribute>[\w\s]+))?$')
m = p1.match(line)
m1 = p1_1.match(line)
m = m if m else m1
if m:
interface = m.groupdict()['interface']
enabled = m.groupdict()['enabled']
line_protocol = m.groupdict()['line_protocol']
connected = m.groupdict()['attribute']
if interface not in interface_dict:
interface_dict[interface] = {}
interface_dict[interface]['port_channel'] = {}
interface_dict[interface]['port_channel']\
['port_channel_member'] = False
if 'administratively down' in enabled or 'delete' in enabled:
interface_dict[interface]['enabled'] = False
else:
interface_dict[interface]['enabled'] = True
if line_protocol:
interface_dict[interface]\
['line_protocol'] = line_protocol
interface_dict[interface]\
['oper_status'] = line_protocol
if connected:
if connected == 'connected':
interface_dict[interface]['connected'] = True
else:
interface_dict[interface]['connected'] = False
continue
# Hardware is Gigabit Ethernet, address is 0057.d228.1a64 (bia 0057.d228.1a64)
# Hardware is Loopback
p2 = re.compile(r'^Hardware +is +(?P<type>[a-zA-Z0-9\-\/\s\+]+)'
'(, *address +is +(?P<mac_address>[a-z0-9\.]+)'
' *\(bia *(?P<phys_address>[a-z0-9\.]+)\))?$')
# Hardware is LTE Adv CAT6 - Multimode LTE/DC-HSPA+/HSPA+/HSPA/UMTS/EDGE/GPRS
p2_2 = re.compile(r'Hardware +is +(?P<type>[a-zA-Z0-9\-\/\+ ]+)(?P<mac_address>.*)(?P<phys_address>.*)')
m = p2.match(line)
m1 = p2_2.match(line)
m = m if m else m1
if m:
types = m.groupdict()['type']
mac_address = m.groupdict()['mac_address']
phys_address = m.groupdict()['phys_address']
interface_dict[interface]['type'] = types
if mac_address:
interface_dict[interface]['mac_address'] = mac_address
if phys_address:
interface_dict[interface]['phys_address'] = phys_address
continue
# Description: desc
# Description: Pim Register Tunnel (Encap) for RP 10.186.1.1
p3 = re.compile(r'^Description: *(?P<description>.*)$')
m = p3.match(line)
if m:
description = m.groupdict()['description']
interface_dict[interface]['description'] = description
continue
# Secondary address 10.2.2.2/24
p4 = re.compile(r'^Secondary +Address +is +(?P<ipv4>(?P<ip>[0-9\.]+)'
'\/(?P<prefix_length>[0-9]+))$')
m = p4.match(line)
if m:
ip_sec = m.groupdict()['ip']
prefix_length_sec = m.groupdict()['prefix_length']
address_sec = m.groupdict()['ipv4']
if 'ipv4' not in interface_dict[interface]:
interface_dict[interface]['ipv4'] = {}
if address_sec not in interface_dict[interface]['ipv4']:
interface_dict[interface]['ipv4'][address_sec] = {}
interface_dict[interface]['ipv4'][address_sec]\
['ip'] = ip_sec
interface_dict[interface]['ipv4'][address_sec]\
['prefix_length'] = prefix_length_sec
interface_dict[interface]['ipv4'][address_sec]\
['secondary'] = True
continue
# Internet Address is 10.4.4.4/24
# Internet address is 10.4.4.4/24
p5 = re.compile(r'^Internet +[A|a]ddress +is +(?P<ipv4>(?P<ip>[0-9\.]+)'
'\/(?P<prefix_length>[0-9]+))$')
m = p5.match(line)
if m:
ip = m.groupdict()['ip']
prefix_length = m.groupdict()['prefix_length']
address = m.groupdict()['ipv4']
if 'ipv4' not in interface_dict[interface]:
interface_dict[interface]['ipv4'] = {}
if address not in interface_dict[interface]['ipv4']:
interface_dict[interface]['ipv4'][address] = {}
interface_dict[interface]['ipv4'][address]\
['ip'] = ip
interface_dict[interface]['ipv4'][address]\
['prefix_length'] = prefix_length
continue
# MTU 1500 bytes, BW 768 Kbit/sec, DLY 3330 usec,
# MTU 1500 bytes, BW 10000 Kbit, DLY 1000 usec,
p6 = re.compile(r'^MTU +(?P<mtu>[0-9]+) +bytes, +BW'
' +(?P<bandwidth>[0-9]+) +Kbit(\/sec)?, +DLY'
' +(?P<delay>[0-9]+) +usec,$')
m = p6.match(line)
if m:
mtu = m.groupdict()['mtu']
bandwidth = m.groupdict()['bandwidth']
if m.groupdict()['delay']:
interface_dict[interface]['delay'] = int(m.groupdict()['delay'])
if mtu:
interface_dict[interface]['mtu'] = int(mtu)
if bandwidth:
interface_dict[interface]['bandwidth'] = int(bandwidth)
continue
# reliability 255/255, txload 1/255, rxload 1/255
p7 = re.compile(r'^reliability +(?P<reliability>[\d\/]+),'
' +txload +(?P<txload>[\d\/]+), +rxload'
' +(?P<rxload>[\d\/]+)$')
m = p7.match(line)
if m:
reliability = m.groupdict()['reliability']
txload = m.groupdict()['txload']
rxload = m.groupdict()['rxload']
interface_dict[interface]['reliability'] = reliability
interface_dict[interface]['txload'] = txload
interface_dict[interface]['rxload'] = rxload
continue
# Encapsulation LOOPBACK, loopback not set
# Encapsulation 802.1Q Virtual LAN, Vlan ID 20, medium is p2p
# Encapsulation ARPA, medium is broadcast
# Encapsulation QinQ Virtual LAN, outer ID 10, inner ID 20
# Encapsulation 802.1Q Virtual LAN, Vlan ID 1., loopback not set
p8 = re.compile(r'^Encapsulation +(?P<encapsulation>[\w\s\.]+),'
' +(?P<rest>.*)$')
m = p8.match(line)
if m:
encapsulation = m.groupdict()['encapsulation']
encapsulation = m.groupdict()['encapsulation'].lower()
encapsulation = encapsulation.replace("802.1q virtual lan","dot1q")
if 'encapsulations' not in interface_dict[interface]:
interface_dict[interface]['encapsulations'] = {}
interface_dict[interface]['encapsulations']\
['encapsulation'] = encapsulation
rest = m.groupdict()['rest']
# Vlan ID 20, medium is p2p
m1 = re.compile(r'(Vlan +ID +(?P<first_dot1q>[0-9]+),)?'
' *medium +is +(?P<medium>[a-z0-9]+)$').match(rest)
# will update key when output is valid
m2 = re.compile(r'loopback +(?P<loopback>[\w\s]+)$').match(rest)
# outer ID 10, inner ID 20
m3 = re.compile(r'outer +ID +(?P<first>[0-9]+), +'
'inner +ID (?P<second>[0-9]+)$').match(rest)
# Vlan ID 1., loopback not set
m4 = re.compile(r'Vlan +ID +(?P<first_dot1q>[0-9]+).?, +'
'(?P<rest>.+)$').match(rest)
if m1:
first_dot1q = m1.groupdict()['first_dot1q']
if first_dot1q:
interface_dict[interface]['encapsulations']\
['first_dot1q'] = first_dot1q
interface_dict[interface]['medium'] = m.groupdict()['medium']
elif m3:
first_dot1q = m3.groupdict()['first']
second_dot1q = m3.groupdict()['second']
interface_dict[interface]['encapsulations']\
['first_dot1q'] = first_dot1q
interface_dict[interface]['encapsulations']\
['second_dot1q'] = second_dot1q
elif m4:
first_dot1q = m4.groupdict()['first_dot1q']
if first_dot1q:
interface_dict[interface]['encapsulations']\
['first_dot1q'] = first_dot1q
continue
# reliability 255/255, txload 1/255, rxload 1/255
p9 = re.compile(r'^reliability +(?P<reliability>[0-9]+),'
' +txload +(?P<txload>[0-9]+), +rxload'
' +(?P<rxload>[0-9]+)$')
m = p9.match(line)
if m:
reliability = m.groupdict()['reliability']
txload = m.groupdict()['txload']
rxload = m.groupdict()['rxload']
interface_dict[interface]['reliability'] = reliability
interface_dict[interface]['txload'] = txload
interface_dict[interface]['rxload'] = rxload
continue
# Keepalive set (10 sec)
p10 = re.compile(r'^Keepalive +set +\((?P<keepalive>[0-9]+)'
' +sec\)$')
m = p10.match(line)
if m:
keepalive = m.groupdict()['keepalive']
if keepalive:
interface_dict[interface]['keepalive'] = int(keepalive)
continue
# Auto-duplex, 1000Mb/s, media type is 10/100/1000BaseTX
# Full-duplex, 1000Mb/s, link type is auto, media type is
# Full Duplex, 1000Mbps, link type is auto, media type is RJ45
# Full Duplex, Auto Speed, link type is auto, media type is RJ45
p11 = re.compile(r'^(?P<duplex_mode>\w+)[\-\s]+[d|D]uplex, +'
'(?P<port_speed>\d+|Auto-(S|s)peed|Auto (S|s)peed)(?: *(Mbps|Mb/s))?,'
'( *link +type +is +(?P<link_type>\w+),)?'
' *media +type +is *(?P<media_type>[\w\/]+)?$')
m = p11.match(line)
if m:
duplex_mode = m.groupdict()['duplex_mode'].lower()
port_speed = m.groupdict()['port_speed'].lower().replace('-speed', '')
link_type = m.groupdict()['link_type']
media_type = m.groupdict()['media_type']
interface_dict[interface]['duplex_mode'] = duplex_mode
interface_dict[interface]['port_speed'] = port_speed
if link_type:
interface_dict[interface]['link_type'] = link_type
if 'auto' in link_type:
interface_dict[interface]['auto_negotiate'] = True
else:
interface_dict[interface]['auto_negotiate'] = False
if media_type:
interface_dict[interface]['media_type'] = media_type
continue
# input flow-control is off, output flow-control is unsupported
p12 = re.compile(r'^(input|output) +flow-control +is +(?P<receive>\w+), +'
'(output|input) +flow-control +is +(?P<send>\w+)$')
m = p12.match(line)
if m:
receive = m.groupdict()['receive'].lower()
send = m.groupdict()['send'].lower()
if 'flow_control' not in interface_dict[interface]:
interface_dict[interface]['flow_control'] = {}
if 'on' in receive:
interface_dict[interface]['flow_control']['receive'] = True
elif 'off' in receive or 'unsupported' in receive:
interface_dict[interface]['flow_control']['receive'] = False
if 'on' in send:
interface_dict[interface]['flow_control']['send'] = True
elif 'off' in send or 'unsupported' in send:
interface_dict[interface]['flow_control']['send'] = False
continue
# Carrier delay is 10 sec
p_cd = re.compile(r'^Carrier +delay +is +(?P<carrier_delay>\d+).*$')
m = p_cd.match(line)
if m:
group = m.groupdict()
sub_dict = interface_dict.setdefault(interface, {})
sub_dict['carrier_delay'] = int(group['carrier_delay'])
# Asymmetric Carrier-Delay Up Timer is 2 sec
# Asymmetric Carrier-Delay Down Timer is 10 sec
p_cd_2 = re.compile(r'^Asymmetric +Carrier-Delay +(?P<type>Down|Up)'
' +Timer +is +(?P<carrier_delay>\d+).*$')
m = p_cd_2.match(line)
if m:
group = m.groupdict()
tp = group['type'].lower()
sub_dict = interface_dict.setdefault(interface, {})
if tp == 'up':
sub_dict['carrier_delay_up'] = int(group['carrier_delay'])
else:
sub_dict['carrier_delay_down'] = int(group['carrier_delay'])
# ARP type: ARPA, ARP Timeout 04:00:00
p13 = re.compile(r'^ARP +type: +(?P<arp_type>\w+), +'
'ARP +Timeout +(?P<arp_timeout>[\w\:\.]+)$')
m = p13.match(line)
if m:
arp_type = m.groupdict()['arp_type'].lower()
arp_timeout = m.groupdict()['arp_timeout']
interface_dict[interface]['arp_type'] = arp_type
interface_dict[interface]['arp_timeout'] = arp_timeout
continue
# Last input never, output 00:01:05, output hang never
p14 = re.compile(r'^Last +input +(?P<last_input>[\w\.\:]+), +'
'output +(?P<last_output>[\w\.\:]+), '
'output +hang +(?P<output_hang>[\w\.\:]+)$')
m = p14.match(line)
if m:
last_input = m.groupdict()['last_input']
last_output = m.groupdict()['last_output']
output_hang = m.groupdict()['output_hang']
interface_dict[interface]['last_input'] = last_input
interface_dict[interface]['last_output'] = last_output
interface_dict[interface]['output_hang'] = output_hang
continue
# Members in this channel: Gi1/0/2
# Members in this channel: Fo1/0/2 Fo1/0/4
p15 = re.compile(r'^Members +in +this +channel: +'
'(?P<port_channel_member_intfs>[\w\/\.\s\,]+)$')
m = p15.match(line)
if m:
interface_dict[interface]['port_channel']\
['port_channel_member'] = True
intfs = m.groupdict()['port_channel_member_intfs'].split(' ')
intfs = [Common.convert_intf_name(i.strip()) for i in intfs]
interface_dict[interface]['port_channel']\
['port_channel_member_intfs'] = intfs
# build connected interface port_channel
for intf in intfs:
if intf not in interface_dict:
interface_dict[intf] = {}
if 'port_channel' not in interface_dict[intf]:
interface_dict[intf]['port_channel'] = {}
interface_dict[intf]['port_channel']['port_channel_member'] = True
interface_dict[intf]['port_channel']['port_channel_int'] = interface
continue
# No. of active members in this channel: 12
p15_1 = re.compile(r'^No\. +of +active +members +in +this +'
'channel: +(?P<active_members>\d+)$')
m = p15_1.match(line)
if m:
group = m.groupdict()
active_members = int(group['active_members'])
interface_dict[interface]['port_channel']\
['port_channel_member'] = True
interface_dict[interface]['port_channel']\
['active_members'] = active_members
continue
# Member 2 : GigabitEthernet0/0/10 , Full-duplex, 900Mb/s
p15_2 = re.compile(r'^Member +\d+ +: +(?P<interface>\S+) +,'
' +\S+, +\S+$')
m = p15_2.match(line)
if m:
group = m.groupdict()
intf = group['interface']
if 'port_channel_member_intfs' not in interface_dict[interface]['port_channel']:
interface_dict[interface]['port_channel']\
['port_channel_member_intfs'] = []
interface_dict[interface]['port_channel']\
['port_channel_member_intfs'].append(intf)
continue
# No. of PF_JUMBO supported members in this channel : 0
p15_3 = re.compile(r'^No\. +of +PF_JUMBO +supported +members +'
'in +this +channel +: +(?P<number>\d+)$')
m = p15_3.match(line)
if m:
group = m.groupdict()
number = int(group['number'])
interface_dict[interface]['port_channel']\
['num_of_pf_jumbo_supported_members'] = number
continue
# Last clearing of "show interface" counters 1d02h
p16 = re.compile(r'^Last +clearing +of +\"show +interface\" +counters +'
'(?P<last_clear>[\w\:\.]+)$')
m = p16.match(line)
if m:
last_clear = m.groupdict()['last_clear']
continue
# Input queue: 0/375/0/0 (size/max/drops/flushes); Total output drops: 0
p17 = re.compile(r'^Input +queue: +(?P<size>\d+)\/(?P<max>\d+)\/'
'(?P<drops>\d+)\/(?P<flushes>\d+) +'
'\(size\/max\/drops\/flushes\); +'
'Total +output +drops: +(?P<output_drop>\d+)$')
m = p17.match(line)
if m:
if 'queues' not in interface_dict[interface]:
interface_dict[interface]['queues'] = {}
interface_dict[interface]['queues']['input_queue_size'] = \
int(m.groupdict()['size'])
interface_dict[interface]['queues']['input_queue_max'] = \
int(m.groupdict()['max'])
interface_dict[interface]['queues']['input_queue_drops'] = \
int(m.groupdict()['drops'])
interface_dict[interface]['queues']['input_queue_flushes'] = \
int(m.groupdict()['flushes'])
interface_dict[interface]['queues']['total_output_drop'] = \
int(m.groupdict()['output_drop'])
continue
# Queueing strategy: fifo
p18 = re.compile(r'^Queueing +strategy: +(?P<queue_strategy>\w+)$')
m = p18.match(line)
if m:
if 'queues' not in interface_dict[interface]:
interface_dict[interface]['queues'] = {}
interface_dict[interface]['queues']['queue_strategy'] = \
m.groupdict()['queue_strategy']
continue
# Output queue: 0/0 (size/max)
p19 = re.compile(r'^Output +queue: +(?P<size>\d+)\/(?P<max>\d+)'
' +\(size\/max\)$')
m = p19.match(line)
if m:
if 'queues' not in interface_dict[interface]:
interface_dict[interface]['queues'] = {}
interface_dict[interface]['queues']['output_queue_size'] = \
int(m.groupdict()['size'])
interface_dict[interface]['queues']['output_queue_max'] = \
int(m.groupdict()['max'])
continue
# 5 minute input rate 0 bits/sec, 0 packets/sec
p20 = re.compile(r'^(?P<load_interval>[0-9\#]+)'
' *(?P<unit>(minute|second|minutes|seconds)) *input *rate'
' *(?P<in_rate>[0-9]+) *bits/sec,'
' *(?P<in_rate_pkts>[0-9]+) *packets/sec$')
m = p20.match(line)
if m:
load_interval = int(m.groupdict()['load_interval'])
in_rate = int(m.groupdict()['in_rate'])
in_rate_pkts = int(m.groupdict()['in_rate_pkts'])
unit = m.groupdict()['unit']
# covert minutes to seconds
if 'minute' in unit:
load_interval = load_interval * 60
if 'counters' not in interface_dict[interface]:
interface_dict[interface]['counters'] = {}
if 'rate' not in interface_dict[interface]['counters']:
interface_dict[interface]['counters']['rate'] = {}
interface_dict[interface]['counters']['rate']\
['load_interval'] = load_interval
interface_dict[interface]['counters']['rate']\
['in_rate'] = in_rate
interface_dict[interface]['counters']['rate']\
['in_rate_pkts'] = in_rate_pkts
if 'last_clear' not in interface_dict[interface]['counters']:
try:
last_clear
except Exception:
pass
else:
interface_dict[interface]['counters']\
['last_clear'] = last_clear
continue
# 5 minute output rate 0 bits/sec, 0 packets/sec
p21 = re.compile(r'^(?P<load_interval>[0-9\#]+)'
' *(minute|second|minutes|seconds) *output *rate'
' *(?P<out_rate>[0-9]+) *bits/sec,'
' *(?P<out_rate_pkts>[0-9]+) *packets/sec$')
m = p21.match(line)
if m:
out_rate = int(m.groupdict()['out_rate'])
out_rate_pkts = int(m.groupdict()['out_rate_pkts'])
interface_dict[interface]['counters']['rate']\
['out_rate'] = out_rate
interface_dict[interface]['counters']['rate']\
['out_rate_pkts'] = out_rate_pkts
continue
# 0 packets input, 0 bytes, 0 no buffer
p22 = re.compile(r'^(?P<in_pkts>[0-9]+) +packets +input,'
' +(?P<in_octets>[0-9]+) +bytes,'
' +(?P<in_no_buffer>[0-9]+) +no +buffer$')
m = p22.match(line)
if m:
if 'counters' not in interface_dict[interface]:
interface_dict[interface]['counters'] = {}
interface_dict[interface]['counters']['in_pkts'] = \
int(m.groupdict()['in_pkts'])
interface_dict[interface]['counters']['in_octets'] = \
int(m.groupdict()['in_octets'])
interface_dict[interface]['counters']['in_no_buffer'] = \
int(m.groupdict()['in_no_buffer'])
continue
# Received 4173 broadcasts (0 IP multicasts)
# Received 535996 broadcasts (535961 multicasts)
p23 = re.compile(r'^Received +(?P<in_broadcast_pkts>\d+) +broadcasts +'
'\((?P<in_multicast_pkts>\d+) *(IP)? *multicasts\)$')
m = p23.match(line)
if m:
interface_dict[interface]['counters']['in_multicast_pkts'] = \
int(m.groupdict()['in_broadcast_pkts'])
interface_dict[interface]['counters']['in_broadcast_pkts'] = \
int(m.groupdict()['in_multicast_pkts'])
continue
# 0 runts, 0 giants, 0 throttles
p24 = re.compile(r'^(?P<in_runts>[0-9]+) *runts,'
' *(?P<in_giants>[0-9]+) *giants,'
' *(?P<in_throttles>[0-9]+) *throttles$')
m = p24.match(line)
if m:
interface_dict[interface]['counters']['in_runts'] = \
int(m.groupdict()['in_runts'])
interface_dict[interface]['counters']['in_giants'] = \
int(m.groupdict()['in_giants'])
interface_dict[interface]['counters']['in_throttles'] = \
int(m.groupdict()['in_throttles'])
continue
# 0 input errors, 0 CRC, 0 frame, 0 overrun, 0 ignored
# 0 input errors, 0 CRC, 0 frame, 0 overrun, 0 ignored, 0 abort
p25 = re.compile(r'^(?P<in_errors>[0-9]+) +input +errors, +'
'(?P<in_crc_errors>[0-9]+) +CRC, +'
'(?P<in_frame>[0-9]+) +frame, +'
'(?P<in_overrun>[0-9]+) +overrun, +'
'(?P<in_ignored>[0-9]+) +ignored'
'(, *(?P<in_abort>[0-9]+) +abort)?$')
m = p25.match(line)
if m:
interface_dict[interface]['counters']['in_errors'] = \
int(m.groupdict()['in_errors'])
interface_dict[interface]['counters']['in_crc_errors'] = \
int(m.groupdict()['in_crc_errors'])
interface_dict[interface]['counters']['in_frame'] = \
int(m.groupdict()['in_frame'])
interface_dict[interface]['counters']['in_overrun'] = \
int(m.groupdict()['in_overrun'])
interface_dict[interface]['counters']['in_ignored'] = \
int(m.groupdict()['in_ignored'])
if m.groupdict()['in_abort']:
interface_dict[interface]['counters']['in_abort'] = \
int(m.groupdict()['in_abort'])
continue
# 0 watchdog, 535961 multicast, 0 pause input
p26 = re.compile(r'^(?P<in_watchdog>[0-9]+) +watchdog, +'
'(?P<in_multicast_pkts>[0-9]+) +multicast, +'
'(?P<in_pause_input>[0-9]+) +pause +input$')
m = p26.match(line)
if m:
interface_dict[interface]['counters']['in_watchdog'] = \
int(m.groupdict()['in_watchdog'])
interface_dict[interface]['counters']['in_multicast_pkts'] = \
int(m.groupdict()['in_multicast_pkts'])
interface_dict[interface]['counters']['in_mac_pause_frames'] = \
int(m.groupdict()['in_pause_input'])
continue
# 0 input packets with dribble condition detected
p27 = re.compile(r'^(?P<in_with_dribble>[0-9]+) +input +packets +with +'
'dribble +condition +detected$')
m = p27.match(line)
if m:
interface_dict[interface]['counters']['in_with_dribble'] = \
int(m.groupdict()['in_with_dribble'])
continue
# 23376 packets output, 3642296 bytes, 0 underruns
p28 = re.compile(r'^(?P<out_pkts>[0-9]+) +packets +output,'
' +(?P<out_octets>[0-9]+) +bytes,'
' +(?P<out_underruns>[0-9]+) +underruns$')
m = p28.match(line)
if m:
interface_dict[interface]['counters']['out_pkts'] = \
int(m.groupdict()['out_pkts'])
interface_dict[interface]['counters']['out_octets'] = \
int(m.groupdict()['out_octets'])
interface_dict[interface]['counters']['out_underruns'] = \
int(m.groupdict()['out_underruns'])
continue
# Received 4173 broadcasts (0 IP multicasts)
# Received 535996 broadcasts (535961 multicasts)
p29 = re.compile(r'^Received +(?P<out_broadcast_pkts>\d+) +broadcasts +'
'\((?P<out_multicast_pkts>\d+) *(IP)? *multicasts\)$')
m = p29.match(line)
if m:
interface_dict[interface]['counters']['out_broadcast_pkts'] = \
int(m.groupdict()['out_broadcast_pkts'])
interface_dict[interface]['counters']['out_multicast_pkts'] = \
int(m.groupdict()['out_multicast_pkts'])
continue
# 0 output errors, 0 collisions, 2 interface resets
# 0 output errors, 0 interface resets
p30 = re.compile(r'^(?P<out_errors>[0-9]+) +output +errors,'
'( *(?P<out_collision>[0-9]+) +collisions,)? +'
'(?P<out_interface_resets>[0-9]+) +interface +resets$')
m = p30.match(line)
if m:
interface_dict[interface]['counters']['out_errors'] = \
int(m.groupdict()['out_errors'])
interface_dict[interface]['counters']['out_interface_resets'] = \
int(m.groupdict()['out_interface_resets'])
if m.groupdict()['out_collision']:
interface_dict[interface]['counters']['out_collision'] = \
int(m.groupdict()['out_collision'])
continue
# 0 unknown protocol drops
p31 = re.compile(r'^(?P<out_unknown_protocl_drops>[0-9]+) +'
'unknown +protocol +drops$')
m = p31.match(line)
if m:
interface_dict[interface]['counters']['out_unknown_protocl_drops'] = \
int(m.groupdict()['out_unknown_protocl_drops'])
continue
# 0 babbles, 0 late collision, 0 deferred
p32 = re.compile(r'^(?P<out_babble>[0-9]+) +babbles, +'
'(?P<out_late_collision>[0-9]+) +late +collision, +'
'(?P<out_deferred>[0-9]+) +deferred$')
m = p32.match(line)
if m:
interface_dict[interface]['counters']['out_babble'] = \
int(m.groupdict()['out_babble'])
interface_dict[interface]['counters']['out_late_collision'] = \
int(m.groupdict()['out_late_collision'])
interface_dict[interface]['counters']['out_deferred'] = \
int(m.groupdict()['out_deferred'])
continue
# 0 lost carrier, 0 no carrier, 0 pause output
p33 = re.compile(r'^(?P<out_lost_carrier>[0-9]+) +lost +carrier, +'
'(?P<out_no_carrier>[0-9]+) +no +carrier, +'
'(?P<out_pause_output>[0-9]+) +pause +output$')
m = p33.match(line)
if m:
interface_dict[interface]['counters']['out_lost_carrier'] = \
int(m.groupdict()['out_lost_carrier'])
interface_dict[interface]['counters']['out_no_carrier'] = \
int(m.groupdict()['out_no_carrier'])
interface_dict[interface]['counters']['out_mac_pause_frames'] = \
int(m.groupdict()['out_pause_output'])
continue
# 0 output buffer failures, 0 output buffers swapped out
p34 = re.compile(r'^(?P<out_buffer_failure>[0-9]+) +output +buffer +failures, +'
'(?P<out_buffers_swapped>[0-9]+) +output +buffers +swapped +out$')
m = p34.match(line)
if m:
interface_dict[interface]['counters']['out_buffer_failure'] = \
int(m.groupdict()['out_buffer_failure'])
interface_dict[interface]['counters']['out_buffers_swapped'] = \
int(m.groupdict()['out_buffers_swapped'])
continue
# Interface is unnumbered. Using address of Loopback0 (10.4.1.1)
# Interface is unnumbered. Using address of GigabitEthernet0/2.1 (192.168.154.1)
p35 = re.compile(r'^Interface +is +unnumbered. +Using +address +of +'
'(?P<unnumbered_intf>[\w\/\.]+) +'
'\((?P<unnumbered_ip>[\w\.\:]+)\)$')
m = p35.match(line)
if m:
unnumbered_dict[interface] = {}
unnumbered_dict[interface]['unnumbered_intf'] = m.groupdict()['unnumbered_intf']
unnumbered_dict[interface]['unnumbered_ip'] = m.groupdict()['unnumbered_ip']
continue
# create strucutre for unnumbered interface
if not unnumbered_dict:
return(interface_dict)
for intf in unnumbered_dict:
unnumbered_intf = unnumbered_dict[intf]['unnumbered_intf']
unnumbered_ip = unnumbered_dict[intf]['unnumbered_ip']
if unnumbered_intf in interface_dict:
if 'ipv4' in interface_dict[unnumbered_intf]:
for ip in interface_dict[unnumbered_intf]['ipv4']:
if unnumbered_ip in ip:
if 'ipv4' not in interface_dict[intf]:
interface_dict[intf]['ipv4'] = {}
if ip not in interface_dict[intf]['ipv4']:
interface_dict[intf]['ipv4'][ip] = {}
m = re.search('([\w\.\:]+)\/(\d+)', ip)
interface_dict[intf]['ipv4'][ip]['ip'] = m.groups()[0]
interface_dict[intf]['ipv4'][ip]['prefix_length'] = m.groups()[1]
interface_dict[intf]['ipv4']['unnumbered'] = {}
interface_dict[intf]['ipv4']['unnumbered']\
['interface_ref'] = unnumbered_intf
return(interface_dict)
# parser using parsergen
# ----------------------
class ShowIpInterfaceBriefSchema(MetaParser):
"""Parser for show ip interface brief"""
schema = {'interface':
{Any():
{Optional('vlan_id'):
{Optional(Any()):
{'ip_address': str,
Optional('interface_is_ok'): str,
Optional('method'): str,
Optional('status'): str,
Optional('protocol'): str}
},
Optional('ip_address'): str,
Optional('interface_is_ok'): str,
Optional('method'): str,
Optional('status'): str,
Optional('protocol'): str}
},
}
class ShowIpInterfaceBrief(ShowIpInterfaceBriefSchema):
"""Parser for:
show ip interface brief
parser class implements detail parsing mechanisms for cli and yang output.
"""
exclude = ['method', '(Tunnel.*)']
#*************************
# schema - class variable
#
# Purpose is to make sure the parser always return the output
# (nested dict) that has the same data structure across all supported
# parsing mechanisms (cli(), yang(), xml()).
def __init__ (self, *args, **kwargs):
super().__init__(*args, **kwargs)
cli_command = ['show ip interface brief {interface}','show ip interface brief']
def cli(self, interface='',output=None):
"""parsing mechanism: cli
Function cli() defines the cli type output parsing mechanism which
typically contains 3 steps: exe
cuting, transforming, returning
"""
parsed_dict = {}
if output is None:
if interface:
cmd = self.cli_command[0].format(interface=interface)
else:
cmd = self.cli_command[1]
out = self.device.execute(cmd)
else:
out = output
if out:
res = parsergen.oper_fill_tabular(device_output=out,
device_os='iosxe',
table_terminal_pattern=r"^\n",
header_fields=
[ "Interface",
"IP-Address",
"OK\?",
"Method",
"Status",
"Protocol" ],
label_fields=
[ "Interface",
"ip_address",
"interface_is_ok",
"method",
"status",
"protocol" ],
index=[0])
# Building the schema out of the parsergen output
if res.entries:
for intf, intf_dict in res.entries.items():
intf = Common.convert_intf_name(intf)
del intf_dict['Interface']
parsed_dict.setdefault('interface', {}).update({intf: intf_dict})