DevNet 106: Network Automation Using Genie/PyATS


                             Network Automation Using Genie/PyATS






Create testbed yaml file:
devices:
    IOSv-L2-1:
        type: "IOSv"
        os: "ios"
        alias: ios-1
        tacacs:
            login_prompt: 'login:'
            password_prompt: 'Password:'
            username: admin
        passwords:
            tacacs: cisco
            enable: cisco
            line: cisco
        connections:
            defaults:
                class: 'unicon.Unicon'
            ssh:
                protocol: ssh
                ip: 192.168.17.3
        custom:
            abstraction:
                order: [os] 
    IOSv-L2-2:
        type: "IOSv"
        os: "ios"
        alias: ios-2
        tacacs:
            login_prompt: 'login:'
            password_prompt: 'Password:'
            username: admin
        passwords:
            tacacs: cisco
            enable: cisco
            line: cisco
        connections:
            defaults:
                class: 'unicon.Unicon'
            ssh:
                protocol: ssh
                ip: 192.168.17.4
        custom:
            abstraction:
                order: [os]

Lab 1:  Using IPython to interaction with testbed file:
In [1]: from genie.conf import Genie                                                                                            

In [2]: testbed = Genie.init("testbed.yaml")                                                                                    

In [3]: testbed                                                                                                                 
Out[3]: <Testbed object  at 0x7f23ea836588> 


In [5]: testbed.devices                                                                                                            
Out[5]: TopologyDict({'IOSv-L2-1': <Device IOSv-L2-1 at 0x7ff50b9d52b0>, 'IOSv-L2-2': <Device IOSv-L2-2 at 0x7ff509128748>})


In [6]: IOSv_L2_1 = testbed.devices["IOSv-L2-1"]


In [8]: IOSv_L2_1.connect()
In [9]: IOSv_L2_1.connected                                               
Out[9]: True
In [10]: interfaces = IOSv_L2_1.parse("show interfaces")
In [10]: interfaces = IOSv_L2_1.parse("show interfaces")
[2019-07-09 13:45:38,827] +++ IOSv-L2-1: executing command 'show interfaces' +++
show interfaces
GigabitEthernet0/2 is up, line protocol is up (connected) 
  Hardware is iGbE, address is 5000.0002.0002 (bia 5000.0002.0002)
  MTU 1500 bytes, BW 1000000 Kbit/sec, DLY 10 usec, 
     reliability 255/255, txload 1/255, rxload 1/255
  Encapsulation ARPA, loopback not set
  Keepalive set (10 sec)
  Unknown, Unknown, link type is auto, media type is unknown media type
  output flow-control is unsupported, input flow-control is unsupported
  Auto-duplex, Auto-speed, link type is auto, media type is unknown
  input flow-control is off, output flow-control is unsupported 
  ARP type: ARPA, ARP Timeout 04:00:00
  Last input never, output 00:00:01, output hang never
  Last clearing of "show interface" counters never
  Input queue: 0/75/0/0 (size/max/drops/flushes); Total output drops: 0
  Queueing strategy: fifo
  Output queue: 0/0 (size/max)
  5 minute input rate 0 bits/sec, 0 packets/sec
  5 minute output rate 0 bits/sec, 0 packets/sec
     0 packets input, 0 bytes, 0 no buffer
     Received 0 broadcasts (0 multicasts)
     0 runts, 0 giants, 0 throttles 
     0 input errors, 0 CRC, 0 frame, 0 overrun, 0 ignored
     0 watchdog, 0 multicast, 0 pause input
     6757 packets output, 494301 bytes, 0 underruns
     0 output errors, 0 collisions, 2 interface resets
     0 unknown protocol drops
     0 babbles, 0 late collision, 0 deferred
     0 lost carrier, 0 no carrier, 0 pause output
     0 output buffer failures, 0 output buffers swapped out
GigabitEthernet0/3 is up, line protocol is up (connected) 
  Hardware is iGbE, address is 5000.0002.0003 (bia 5000.0002.0003)
  MTU 1500 bytes, BW 1000000 Kbit/sec, DLY 10 usec, 
     reliability 255/255, txload 1/255, rxload 1/255
  Encapsulation ARPA, loopback not set
  Keepalive set (10 sec)
  Unknown, Unknown, link type is auto, media type is unknown media type
  output flow-control is unsupported, input flow-control is unsupported
  Auto-duplex, Auto-speed, link type is auto, media type is unknown
  input flow-control is off, output flow-control is unsupported 
  ARP type: ARPA, ARP Timeout 04:00:00
  Last input never, output 00:00:01, output hang never
  Last clearing of "show interface" counters never
  Input queue: 0/75/0/0 (size/max/drops/flushes); Total output drops: 0
  Queueing strategy: fifo
  Output queue: 0/0 (size/max)
  5 minute input rate 0 bits/sec, 0 packets/sec
  5 minute output rate 0 bits/sec, 0 packets/sec
     0 packets input, 0 bytes, 0 no buffer
     Received 0 broadcasts (0 multicasts)
     0 runts, 0 giants, 0 throttles 
     0 input errors, 0 CRC, 0 frame, 0 overrun, 0 ignored
     0 watchdog, 0 multicast, 0 pause input
     6756 packets output, 493828 bytes, 0 underruns
     0 output errors, 0 collisions, 2 interface resets
     0 unknown protocol drops
     0 babbles, 0 late collision, 0 deferred
     0 lost carrier, 0 no carrier, 0 pause output
     0 output buffer failures, 0 output buffers swapped out
GigabitEthernet0/1 is up, line protocol is up (connected) 
  Hardware is GigabitEthernet, address is 5000.0002.0001 (bia 5000.0002.0001)
  Internet address is 192.168.17.3/24
  MTU 1500 bytes, BW 1000000 Kbit/sec, DLY 10 usec, 
     reliability 255/255, txload 1/255, rxload 1/255
  Encapsulation ARPA, loopback not set
  Keepalive set (10 sec)
  Unknown, Unknown, link type is auto, media type is unknown media type
  output flow-control is unsupported, input flow-control is unsupported
  Auto-duplex, Auto-speed, link type is auto, media type is unknown
  input flow-control is off, output flow-control is unsupported 
  ARP type: ARPA, ARP Timeout 04:00:00
  Last input 00:00:00, output 00:00:00, output hang never
  Last clearing of "show interface" counters never
  Input queue: 1/75/0/0 (size/max/drops/flushes); Total output drops: 0
  Queueing strategy: fifo
  Output queue: 0/40 (size/max)
  5 minute input rate 1000 bits/sec, 1 packets/sec
  5 minute output rate 0 bits/sec, 0 packets/sec
     2270 packets input, 222169 bytes, 0 no buffer
     Received 0 broadcasts (0 IP multicasts)
     0 runts, 0 giants, 0 throttles 
     0 input errors, 0 CRC, 0 frame, 0 overrun, 0 ignored
     0 watchdog, 0 multicast, 0 pause input
     375 packets output, 110214 bytes, 0 underruns
     0 output errors, 0 collisions, 5 interface resets
     0 unknown protocol drops
     0 babbles, 0 late collision, 0 deferred
     0 lost carrier, 0 no carrier, 0 pause output
     0 output buffer failures, 0 output buffers swapped out
GigabitEthernet0/0 is up, line protocol is up (connected) 
  Hardware is GigabitEthernet, address is 5000.0002.0000 (bia 5000.0002.0000)
  Internet address is 172.17.7.3/24
  MTU 1500 bytes, BW 1000000 Kbit/sec, DLY 10 usec, 
     reliability 255/255, txload 1/255, rxload 1/255
  Encapsulation ARPA, loopback not set
  Keepalive set (10 sec)
  Unknown, Unknown, link type is auto, media type is unknown media type
  output flow-control is unsupported, input flow-control is unsupported
  Auto-duplex, Auto-speed, link type is auto, media type is unknown
  input flow-control is off, output flow-control is unsupported 
  ARP type: ARPA, ARP Timeout 04:00:00
  Last input 00:00:15, output 00:00:03, output hang never
  Last clearing of "show interface" counters never
  Input queue: 0/75/0/0 (size/max/drops/flushes); Total output drops: 0
  Queueing strategy: fifo
  Output queue: 0/40 (size/max)
  5 minute input rate 0 bits/sec, 0 packets/sec
  5 minute output rate 0 bits/sec, 0 packets/sec
     194 packets input, 85602 bytes, 0 no buffer
     Received 0 broadcasts (0 IP multicasts)
     0 runts, 0 giants, 0 throttles 
     0 input errors, 0 CRC, 0 frame, 0 overrun, 0 ignored
     0 watchdog, 0 multicast, 0 pause input
     215 packets output, 88753 bytes, 0 underruns
     0 output errors, 0 collisions, 5 interface resets
     0 unknown protocol drops
     0 babbles, 0 late collision, 0 deferred
     0 lost carrier, 0 no carrier, 0 pause output
     0 output buffer failures, 0 output buffers swapped out
Loopback0 is up, line protocol is up 
  Hardware is Loopback
  Internet address is 3.3.3.3/32
  MTU 1514 bytes, BW 8000000 Kbit/sec, DLY 5000 usec, 
     reliability 255/255, txload 1/255, rxload 1/255
  Encapsulation LOOPBACK, loopback not set
  Keepalive set (10 sec)
  Last input never, output never, output hang never
  Last clearing of "show interface" counters never
  Input queue: 0/75/0/0 (size/max/drops/flushes); Total output drops: 0
  Queueing strategy: fifo
  Output queue: 0/0 (size/max)
  5 minute input rate 0 bits/sec, 0 packets/sec
  5 minute output rate 0 bits/sec, 0 packets/sec
     0 packets input, 0 bytes, 0 no buffer
     Received 0 broadcasts (0 IP multicasts)
     0 runts, 0 giants, 0 throttles 
     0 input errors, 0 CRC, 0 frame, 0 overrun, 0 ignored, 0 abort
     0 packets output, 0 bytes, 0 underruns
     0 output errors, 0 collisions, 0 interface resets
     0 unknown protocol drops
     0 output buffer failures, 0 output buffers swapped out
IOSv-L2-1#
In [11]: interfaces["GigabitEthernet0/0"]                                                                                          
Out[11]: 
{'port_channel': {'port_channel_member': False},
 'enabled': True,
 'line_protocol': 'up',
 'oper_status': 'up',
 'connected': True,
 'type': 'GigabitEthernet',
 'mac_address': '5000.0002.0000',
 'phys_address': '5000.0002.0000',
 'ipv4': {'172.17.7.3/24': {'ip': '172.17.7.3', 'prefix_length': '24'}},
 'delay': 10,
 'mtu': 1500,
 'bandwidth': 1000000,
 'reliability': '255/255',
 'txload': '1/255',
 'rxload': '1/255',
 'encapsulations': {'encapsulation': 'arpa'},
 'keepalive': 10,
 'flow_control': {'receive': False, 'send': False},
 'duplex_mode': 'auto',
 'port_speed': 'auto',
 'link_type': 'auto',
 'auto_negotiate': True,
 'media_type': 'unknown',
 'arp_type': 'arpa',
 'arp_timeout': '04:00:00',
 'last_input': '00:00:15',
 'last_output': '00:00:03',
 'output_hang': 'never',
 'queues': {'input_queue_size': 0,
  'input_queue_max': 75,
  'input_queue_drops': 0,
  'input_queue_flushes': 0,
  'total_output_drop': 0,
  'queue_strategy': 'fifo',
  'output_queue_size': 0,
  'output_queue_max': 40},
 'counters': {'rate': {'load_interval': 300,
   'in_rate': 0,
   'in_rate_pkts': 0,
   'out_rate': 0,
   'out_rate_pkts': 0},
  'last_clear': 'never',
  'in_pkts': 194,
  'in_octets': 85602,
  'in_no_buffer': 0,
  'in_multicast_pkts': 0,
  'in_broadcast_pkts': 0,
  'in_runts': 0,
  'in_giants': 0,
  'in_throttles': 0,
  'in_errors': 0,
  'in_crc_errors': 0,
  'in_frame': 0,
  'in_overrun': 0,
  'in_ignored': 0,
  'in_watchdog': 0,
  'in_mac_pause_frames': 0,
  'out_pkts': 215,
  'out_octets': 88753,
  'out_underruns': 0,
  'out_errors': 0,
  'out_interface_resets': 5,
  'out_collision': 0,
  'out_unknown_protocl_drops': 0,
  'out_babble': 0,
  'out_late_collision': 0,
  'out_deferred': 0,
  'out_lost_carrier': 0,
  'out_no_carrier': 0,
  'out_mac_pause_frames': 0,
  'out_buffer_failure': 0,
  'out_buffers_swapped': 0}}


In [12]: interfaces["GigabitEthernet0/0"]["ipv4"]                                                                                  
Out[12]: {'172.17.7.3/24': {'ip': '172.17.7.3', 'prefix_length': '24'}}
In [13]: version = IOSv_L2_1.parse("show version")


In [14]: version                            
Out[14]: 
{'version': {'version_short': '15.0',
  'platform': 'vios_l2',
  'version': '15.0(TTC',
  'image_id': 'vios_l2-ADVENTERPRISEK9-M',
  'os': 'IOSv',
  'image_type': 'production image',
  'rom': 'Bootstrap program is IOSv',
  'hostname': 'IOSv-L2-1',
  'uptime': '2 hours, 48 minutes',
  'system_image': 'flash0:/vios_l2-adventerprisek9-m',
  'last_reload_reason': 'Unknown reason',
  'number_of_intfs': {'Gigabit Ethernet': '5'},
  'mem_size': {'non-volatile configuration': '256'},
  'curr_config_register': '0x0'}}

In [15]: version["version"]["uptime"]                                     
Out[15]: '2 hours, 48 minutes'


In [16]: with open(interface_file, "w") as f: 
    ...:     writer = csv.DictWriter(f, report_fields) 
    ...:     writer.writeheader() 
    ...:     for interface, details in interfaces.items(): 
    ...:         writer.writerow( 
    ...:            {"Interface": interface, 
    ...:             "MAC Address": details["mac_address"]} 
    ...:             ) 
    ...:  
root@kali:/DevNet# cat interface.csv 
Interface,  MAC Address
GigabitEthernet0/2,  5000.0002.0002
GigabitEthernet0/3,  5000.0002.0003
GigabitEthernet0/1,  5000.0002.0001
GigabitEthernet0/0,  5000.0002.0000

In [18]: IOSv_L2_1.disconnect()
--------------------------------------------------------------------------------------------------------

Lab 2: Create Script:
#! /usr/bin/env python
# interface_report.py 

from genie.conf import Genie
testbed = Genie.init("testbed.yaml")
device_interface_details = {}
for device in testbed.devices:
 testbed.devices[device].connect()
 interface_details = testbed.devices[device].parse("show interfaces")
 device_interface_details[device] =  interface_details


root@kali:/DevNet# python3 -i  interface_report.py
>>> device_interface_details
{'IOSv-L2-1': {'GigabitEthernet0/2': {'port_channel': {'port_channel_member': False}, 'enabled': True, 'line_protocol': 'up', 'oper_status': 'up', 'connected': True, 'type': 'iGbE', 'mac_address': '5000.0002.0002', 'phys_address': '5000.0002.0002', 'delay': 10, 'mtu': 1500, 'bandwidth': 1000000, 'reliability': '255/255', 'txload': '1/255', 'rxload': '1/255', 'encapsulations': {'encapsulation': 'arpa'}, 'keepalive': 10, 'flow_control': {'receive': False, 'send': False}, 'duplex_mode': 'auto', 'port_speed': 'auto', 'link_type': 'auto', 'auto_negotiate': True, 'media_type': 'unknown', 'arp_type': 'arpa', 'arp_timeout': '04:00:00', 'last_input': 'never', 'last_output': '00:00:01', 'output_hang': 'never', 'queues': {'input_queue_size': 0, 'input_queue_max': 75, 'input_queue_drops': 0, 'input_queue_flushes': 0, 'total_output_drop': 0, 'queue_strategy': 'fifo', 'output_queue_size': 0, 'output_queue_max': 0}, 'counters': {'rate': {'load_interval': 300, 'in_rate': 0, 'in_rate_pkts': 0, 'out_rate': 0, 'out_rate_pkts': 0}, 'last_clear': 'never', 'in_pkts': 0, 'in_octets': 0, 'in_no_buffer': 0, 'in_multicast_pkts': 0, 'in_broadcast_pkts': 0, 'in_runts': 0, 'in_giants': 0, 'in_throttles': 0, 'in_errors': 0, 'in_crc_errors': 0, 'in_frame': 0, 'in_overrun': 0, 'in_ignored': 0, 'in_watchdog': 0, 'in_mac_pause_frames': 0, 'out_pkts': 12520, 'out_octets': 912734, 'out_underruns': 0, 'out_errors': 0, 'out_interface_resets': 2, 'out_collision': 0, 'out_unknown_protocl_drops': 0, 'out_babble': 0, 'out_late_collision': 0, 'out_deferred': 0, 'out_lost_carrier': 0, 'out_no_carrier': 0, 'out_mac_pause_frames': 0, 'out_buffer_failure': 0, 'out_buffers_swapped': 0}}, 
               'GigabitEthernet0/3': {'port_channel': {'port_channel_member': False}, 'enabled': True, 'line_protocol': 'up', 'oper_status': 'up', 'connected': True, 'type': 'iGbE', 'mac_address': '5000.0002.0003', 'phys_address': '5000.0002.0003', 'delay': 10, 'mtu': 1500, 'bandwidth': 1000000, 'reliability': '255/255', 'txload': '1/255', 'rxload': '1/255', 'encapsulations': {'encapsulation': 'arpa'}, 'keepalive': 10, 'flow_control': {'receive': False, 'send': False}, 'duplex_mode': 'auto', 'port_speed': 'auto', 'link_type': 'auto', 'auto_negotiate': True, 'media_type': 'unknown', 'arp_type': 'arpa', 'arp_timeout': '04:00:00', 'last_input': 'never', 'last_output': '00:00:00', 'output_hang': 'never', 'queues': {'input_queue_size': 0, 'input_queue_max': 75, 'input_queue_drops': 0, 'input_queue_flushes': 0, 'total_output_drop': 0, 'queue_strategy': 'fifo', 'output_queue_size': 0, 'output_queue_max': 0}, 'counters': {'rate': {'load_interval': 300, 'in_rate': 0, 'in_rate_pkts': 0, 'out_rate': 1000, 'out_rate_pkts': 1}, 'last_clear': 'never', 'in_pkts': 0, 'in_octets': 0, 'in_no_buffer': 0, 'in_multicast_pkts': 0, 'in_broadcast_pkts': 0, 'in_runts': 0, 'in_giants': 0, 'in_throttles': 0, 'in_errors': 0, 'in_crc_errors': 0, 'in_frame': 0, 'in_overrun': 0, 'in_ignored': 0, 'in_watchdog': 0, 'in_mac_pause_frames': 0, 'out_pkts': 12518, 'out_octets': 911788, 'out_underruns': 0, 'out_errors': 0, 'out_interface_resets': 2, 'out_collision': 0, 'out_unknown_protocl_drops': 0, 'out_babble': 0, 'out_late_collision': 0, 'out_deferred': 0, 'out_lost_carrier': 0, 'out_no_carrier': 0, 'out_mac_pause_frames': 0, 'out_buffer_failure': 0, 'out_buffers_swapped': 0}}, 
                 'GigabitEthernet0/1': {'port_channel': {'port_channel_member': False}, 'enabled': True, 'line_protocol': 'up', 'oper_status': 'up', 'connected': True, 'type': 'GigabitEthernet', 'mac_address': '5000.0002.0001', 'phys_address': '5000.0002.0001', 'ipv4': {'192.168.17.3/24': {'ip': '192.168.17.3', 'prefix_length': '24'}}, 'delay': 10, 'mtu': 1500, 'bandwidth': 1000000, 'reliability': '255/255', 'txload': '1/255', 'rxload': '1/255', 'encapsulations': {'encapsulation': 'arpa'}, 'keepalive': 10, 'flow_control': {'receive': False, 'send': False}, 'duplex_mode': 'auto', 'port_speed': 'auto', 'link_type': 'auto', 'auto_negotiate': True, 'media_type': 'unknown', 'arp_type': 'arpa', 'arp_timeout': '04:00:00', 'last_input': '00:00:00', 'last_output': '00:00:00', 'output_hang': 'never', 'queues': {'input_queue_size': 0, 'input_queue_max': 75, 'input_queue_drops': 0, 'input_queue_flushes': 0, 'total_output_drop': 0, 'queue_strategy': 'fifo', 'output_queue_size': 0, 'output_queue_max': 40}, 'counters': {'rate': {'load_interval': 300, 'in_rate': 1000, 'in_rate_pkts': 1, 'out_rate': 1000, 'out_rate_pkts': 1}, 'last_clear': 'never', 'in_pkts': 4179, 'in_octets': 406361, 'in_no_buffer': 0, 'in_multicast_pkts': 0, 'in_broadcast_pkts': 0, 'in_runts': 0, 'in_giants': 0, 'in_throttles': 0, 'in_errors': 0, 'in_crc_errors': 0, 'in_frame': 0, 'in_overrun': 0, 'in_ignored': 0, 'in_watchdog': 0, 'in_mac_pause_frames': 0, 'out_pkts': 736, 'out_octets': 214245, 'out_underruns': 0, 'out_errors': 0, 'out_interface_resets': 5, 'out_collision': 0, 'out_unknown_protocl_drops': 0, 'out_babble': 0, 'out_late_collision': 0, 'out_deferred': 0, 'out_lost_carrier': 0, 'out_no_carrier': 0, 'out_mac_pause_frames': 0, 'out_buffer_failure': 0, 'out_buffers_swapped': 0}},
               'GigabitEthernet0/0': {'port_channel': {'port_channel_member': False}, 'enabled': True, 'line_protocol': 'up', 'oper_status': 'up', 'connected': True, 'type': 'GigabitEthernet', 'mac_address': '5000.0002.0000', 'phys_address': '5000.0002.0000', 'ipv4': {'172.17.7.3/24': {'ip': '172.17.7.3', 'prefix_length': '24'}}, 'delay': 10, 'mtu': 1500, 'bandwidth': 1000000, 'reliability': '255/255', 'txload': '1/255', 'rxload': '1/255', 'encapsulations': {'encapsulation': 'arpa'}, 'keepalive': 10, 'flow_control': {'receive': False, 'send': False}, 'duplex_mode': 'auto', 'port_speed': 'auto', 'link_type': 'auto', 'auto_negotiate': True, 'media_type': 'unknown', 'arp_type': 'arpa', 'arp_timeout': '04:00:00', 'last_input': '00:00:36', 'last_output': '00:00:33', 'output_hang': 'never', 'queues': {'input_queue_size': 0, 'input_queue_max': 75, 'input_queue_drops': 0, 'input_queue_flushes': 0, 'total_output_drop': 0, 'queue_strategy': 'fifo', 'output_queue_size': 0, 'output_queue_max': 40}, 'counters': {'rate': {'load_interval': 300, 'in_rate': 0, 'in_rate_pkts': 0, 'out_rate': 0, 'out_rate_pkts': 0}, 'last_clear': 'never', 'in_pkts': 349, 'in_octets': 155662, 'in_no_buffer': 0, 'in_multicast_pkts': 0, 'in_broadcast_pkts': 0, 'in_runts': 0, 'in_giants': 0, 'in_throttles': 0, 'in_errors': 0, 'in_crc_errors': 0, 'in_frame': 0, 'in_overrun': 0, 'in_ignored': 0, 'in_watchdog': 0, 'in_mac_pause_frames': 0, 'out_pkts': 383, 'out_octets': 159439, 'out_underruns': 0, 'out_errors': 0, 'out_interface_resets': 5, 'out_collision': 0, 'out_unknown_protocl_drops': 0, 'out_babble': 0, 'out_late_collision': 0, 'out_deferred': 0, 'out_lost_carrier': 0, 'out_no_carrier': 0, 'out_mac_pause_frames': 0, 'out_buffer_failure': 0, 'out_buffers_swapped': 0}}, 
        'Loopback0': {'port_channel': {'port_channel_member': False}, 'enabled': True, 'line_protocol': 'up', 'oper_status': 'up', 'type': 'Loopback', 'ipv4': {'3.3.3.3/32': {'ip': '3.3.3.3', 'prefix_length': '32'}}, 'delay': 5000, 'mtu': 1514, 'bandwidth': 8000000, 'reliability': '255/255', 'txload': '1/255', 'rxload': '1/255', 'encapsulations': {'encapsulation': 'loopback'}, 'keepalive': 10, 'last_input': 'never', 'last_output': 'never', 'output_hang': 'never', 'queues': {'input_queue_size': 0, 'input_queue_max': 75, 'input_queue_drops': 0, 'input_queue_flushes': 0, 'total_output_drop': 0, 'queue_strategy': 'fifo', 'output_queue_size': 0, 'output_queue_max': 0}, 'counters': {'rate': {'load_interval': 300, 'in_rate': 0, 'in_rate_pkts': 0, 'out_rate': 0, 'out_rate_pkts': 0}, 'last_clear': 'never', 'in_pkts': 0, 'in_octets': 0, 'in_no_buffer': 0, 'in_multicast_pkts': 0, 'in_broadcast_pkts': 0, 'in_runts': 0, 'in_giants': 0, 'in_throttles': 0, 'in_errors': 0, 'in_crc_errors': 0, 'in_frame': 0, 'in_overrun': 0, 'in_ignored': 0, 'in_abort': 0, 'out_pkts': 0, 'out_octets': 0, 'out_underruns': 0, 'out_errors': 0, 'out_interface_resets': 0, 'out_collision': 0, 'out_unknown_protocl_drops': 0, 'out_buffer_failure': 0, 'out_buffers_swapped': 0}}}, 'IOSv-L2-2': {'GigabitEthernet0/2': {'port_channel': {'port_channel_member': False}, 'enabled': True, 'line_protocol': 'up', 'oper_status': 'up', 'connected': True, 'type': 'iGbE', 'mac_address': '5000.0001.0002', 'phys_address': '5000.0001.0002', 'delay': 10, 'mtu': 1500, 'bandwidth': 1000000, 'reliability': '255/255', 'txload': '1/255', 'rxload': '1/255', 'encapsulations': {'encapsulation': 'arpa'}, 'keepalive': 10, 'flow_control': {'receive': False, 'send': False}, 'duplex_mode': 'auto', 'port_speed': 'auto', 'link_type': 'auto', 'auto_negotiate': True, 'media_type': 'unknown', 'arp_type': 'arpa', 'arp_timeout': '04:00:00', 'last_input': 'never', 'last_output': '00:00:00', 'output_hang': 'never', 'queues': {'input_queue_size': 0, 'input_queue_max': 75, 'input_queue_drops': 0, 'input_queue_flushes': 0, 'total_output_drop': 0, 'queue_strategy': 'fifo', 'output_queue_size': 0, 'output_queue_max': 0}, 'counters': {'rate': {'load_interval': 300, 'in_rate': 0, 'in_rate_pkts': 0, 'out_rate': 0, 'out_rate_pkts': 0}, 'last_clear': 'never', 'in_pkts': 0, 'in_octets': 0, 'in_no_buffer': 0, 'in_multicast_pkts': 0, 'in_broadcast_pkts': 0, 'in_runts': 0, 'in_giants': 0, 'in_throttles': 0, 'in_errors': 0, 'in_crc_errors': 0, 'in_frame': 0, 'in_overrun': 0, 'in_ignored': 0, 'in_watchdog': 0, 'in_mac_pause_frames': 0, 'out_pkts': 12485, 'out_octets': 910127, 'out_underruns': 0, 'out_errors': 0, 'out_interface_resets': 2, 'out_collision': 0, 'out_unknown_protocl_drops': 0, 'out_babble': 0, 'out_late_collision': 0, 'out_deferred': 0, 'out_lost_carrier': 0, 'out_no_carrier': 0, 'out_mac_pause_frames': 0, 'out_buffer_failure': 0, 'out_buffers_swapped': 0}}, 'GigabitEthernet0/3': {'port_channel': {'port_channel_member': False}, 'enabled': True, 'line_protocol': 'up', 'oper_status': 'up', 'connected': True, 'type': 'iGbE', 'mac_address': '5000.0001.0003', 'phys_address': '5000.0001.0003', 'delay': 10, 'mtu': 1500, 'bandwidth': 1000000, 'reliability': '255/255', 'txload': '1/255', 'rxload': '1/255', 'encapsulations': {'encapsulation': 'arpa'}, 'keepalive': 10, 'flow_control': {'receive': False, 'send': False}, 'duplex_mode': 'auto', 'port_speed': 'auto', 'link_type': 'auto', 'auto_negotiate': True, 'media_type': 'unknown', 'arp_type': 'arpa', 'arp_timeout': '04:00:00', 'last_input': 'never', 'last_output': '00:00:00', 'output_hang': 'never', 'queues': {'input_queue_size': 0, 'input_queue_max': 75, 'input_queue_drops': 0, 'input_queue_flushes': 0, 'total_output_drop': 0, 'queue_strategy': 'fifo', 'output_queue_size': 0, 'output_queue_max': 0}, 'counters': {'rate': {'load_interval': 300, 'in_rate': 0, 'in_rate_pkts': 0, 'out_rate': 0, 'out_rate_pkts': 0}, 'last_clear': 'never', 'in_pkts': 0, 'in_octets': 0, 'in_no_buffer': 0, 'in_multicast_pkts': 0, 'in_broadcast_pkts': 0, 'in_runts': 0, 'in_giants': 0, 'in_throttles': 0, 'in_errors': 0, 'in_crc_errors': 0, 'in_frame': 0, 'in_overrun': 0, 'in_ignored': 0, 'in_watchdog': 0, 'in_mac_pause_frames': 0, 'out_pkts': 12485, 'out_octets': 910127, 'out_underruns': 0, 'out_errors': 0, 'out_interface_resets': 2, 'out_collision': 0, 'out_unknown_protocl_drops': 0, 'out_babble': 0, 'out_late_collision': 0, 'out_deferred': 0, 'out_lost_carrier': 0, 'out_no_carrier': 0, 'out_mac_pause_frames': 0, 'out_buffer_failure': 0, 'out_buffers_swapped': 0}}, 'GigabitEthernet0/1': {'port_channel': {'port_channel_member': False}, 'enabled': True, 'line_protocol': 'up', 'oper_status': 'up', 'connected': True, 'type': 'GigabitEthernet', 'mac_address': '5000.0001.0001', 'phys_address': '5000.0001.0001', 'ipv4': {'192.168.17.4/24': {'ip': '192.168.17.4', 'prefix_length': '24'}}, 'delay': 10, 'mtu': 1500, 'bandwidth': 1000000, 'reliability': '255/255', 'txload': '1/255', 'rxload': '1/255', 'encapsulations': {'encapsulation': 'arpa'}, 'keepalive': 10, 'flow_control': {'receive': False, 'send': False}, 'duplex_mode': 'auto', 'port_speed': 'auto', 'link_type': 'auto', 'auto_negotiate': True, 'media_type': 'unknown', 'arp_type': 'arpa', 'arp_timeout': '04:00:00', 'last_input': '00:00:00', 'last_output': '00:00:00', 'output_hang': 'never', 'queues': {'input_queue_size': 1, 'input_queue_max': 75, 'input_queue_drops': 0, 'input_queue_flushes': 0, 'total_output_drop': 0, 'queue_strategy': 'fifo', 'output_queue_size': 0, 'output_queue_max': 40}, 'counters': {'rate': {'load_interval': 300, 'in_rate': 1000, 'in_rate_pkts': 1, 'out_rate': 1000, 'out_rate_pkts': 1}, 'last_clear': 'never', 'in_pkts': 4039, 'in_octets': 393162, 'in_no_buffer': 0, 'in_multicast_pkts': 0, 'in_broadcast_pkts': 0, 'in_runts': 0, 'in_giants': 0, 'in_throttles': 0, 'in_errors': 0, 'in_crc_errors': 0, 'in_frame': 0, 'in_overrun': 0, 'in_ignored': 0, 'in_watchdog': 0, 'in_mac_pause_frames': 0, 'out_pkts': 542, 'out_octets': 180414, 'out_underruns': 0, 'out_errors': 0, 'out_interface_resets': 5, 'out_collision': 0, 'out_unknown_protocl_drops': 0, 'out_babble': 0, 'out_late_collision': 0, 'out_deferred': 0, 'out_lost_carrier': 0, 'out_no_carrier': 0, 'out_mac_pause_frames': 0, 'out_buffer_failure': 0, 'out_buffers_swapped': 0}}, 'GigabitEthernet0/0': {'port_channel': {'port_channel_member': False}, 'enabled': True, 'line_protocol': 'up', 'oper_status': 'up', 'connected': True, 'type': 'GigabitEthernet', 'mac_address': '5000.0001.0000', 'phys_address': '5000.0001.0000', 'ipv4': {'172.17.7.4/24': {'ip': '172.17.7.4', 'prefix_length': '24'}}, 'delay': 10, 'mtu': 1500, 'bandwidth': 1000000, 'reliability': '255/255', 'txload': '1/255', 'rxload': '1/255', 'encapsulations': {'encapsulation': 'arpa'}, 'keepalive': 10, 'flow_control': {'receive': False, 'send': False}, 'duplex_mode': 'auto', 'port_speed': 'auto', 'link_type': 'auto', 'auto_negotiate': True, 'media_type': 'unknown', 'arp_type': 'arpa', 'arp_timeout': '04:00:00', 'last_input': '00:00:35', 'last_output': '00:00:38', 'output_hang': 'never', 'queues': {'input_queue_size': 0, 'input_queue_max': 75, 'input_queue_drops': 0, 'input_queue_flushes': 0, 'total_output_drop': 0, 'queue_strategy': 'fifo', 'output_queue_size': 0, 'output_queue_max': 40}, 'counters': {'rate': {'load_interval': 300, 'in_rate': 0, 'in_rate_pkts': 0, 'out_rate': 0, 'out_rate_pkts': 0}, 'last_clear': 'never', 'in_pkts': 349, 'in_octets': 156477, 'in_no_buffer': 0, 'in_multicast_pkts': 0, 'in_broadcast_pkts': 0, 'in_runts': 0, 'in_giants': 0, 'in_throttles': 0, 'in_errors': 0, 'in_crc_errors': 0, 'in_frame': 0, 'in_overrun': 0, 'in_ignored': 0, 'in_watchdog': 0, 'in_mac_pause_frames': 0, 'out_pkts': 379, 'out_octets': 157989, 'out_underruns': 0, 'out_errors': 0, 'out_interface_resets': 5, 'out_collision': 0, 'out_unknown_protocl_drops': 0, 'out_babble': 0, 'out_late_collision': 0, 'out_deferred': 0, 'out_lost_carrier': 0, 'out_no_carrier': 0, 'out_mac_pause_frames': 0, 'out_buffer_failure': 0, 'out_buffers_swapped': 0}}, 'Loopback0': {'port_channel': {'port_channel_member': False}, 'enabled': True, 'line_protocol': 'up', 'oper_status': 'up', 'type': 'Loopback', 'ipv4': {'4.4.4.4/32': {'ip': '4.4.4.4', 'prefix_length': '32'}}, 'delay': 5000, 'mtu': 1514, 'bandwidth': 8000000, 'reliability': '255/255', 'txload': '1/255', 'rxload': '1/255', 'encapsulations': {'encapsulation': 'loopback'}, 'keepalive': 10, 'last_input': 'never', 'last_output': 'never', 'output_hang': 'never', 'queues': {'input_queue_size': 0, 'input_queue_max': 75, 'input_queue_drops': 0, 'input_queue_flushes': 0, 'total_output_drop': 0, 'queue_strategy': 'fifo', 'output_queue_size': 0, 'output_queue_max': 0}, 'counters': {'rate': {'load_interval': 300, 'in_rate': 0, 'in_rate_pkts': 0, 'out_rate': 0, 'out_rate_pkts': 0}, 'last_clear': 'never', 'in_pkts': 0, 'in_octets': 0, 'in_no_buffer': 0, 'in_multicast_pkts': 0, 'in_broadcast_pkts': 0, 'in_runts': 0, 'in_giants': 0, 'in_throttles': 0, 'in_errors': 0, 'in_crc_errors': 0, 'in_frame': 0, 'in_overrun': 0, 'in_ignored': 0, 'in_abort': 0, 'out_pkts': 0, 'out_octets': 0, 'out_underruns': 0, 'out_errors': 0, 'out_interface_resets': 0, 'out_collision': 0, 'out_unknown_protocl_drops': 0, 'out_buffer_failure': 0, 'out_buffers_swapped': 0}}}}
>>> 

>>> device_interface_details["IOSv-L2-1"].keys()
dict_keys(['GigabitEthernet0/2', 'GigabitEthernet0/3', 'GigabitEthernet0/1', 'GigabitEthernet0/0', 'Loopback0'])

>>> device_interface_details["IOSv-L2-1"]["GigabitEthernet0/0"]
{'port_channel': {'port_channel_member': False}, 'enabled': True, 'line_protocol': 'up', 'oper_status': 'up', 'connected': True, 'type': 'GigabitEthernet', 'mac_address': '5000.0002.0000', 'phys_address': '5000.0002.0000', 'ipv4': {'172.17.7.3/24': {'ip': '172.17.7.3', 'prefix_length': '24'}}, 'delay': 10, 'mtu': 1500, 'bandwidth': 1000000, 'reliability': '255/255', 'txload': '1/255', 'rxload': '1/255', 'encapsulations': {'encapsulation': 'arpa'}, 'keepalive': 10, 'flow_control': {'receive': False, 'send': False}, 'duplex_mode': 'auto', 'port_speed': 'auto', 'link_type': 'auto', 'auto_negotiate': True, 'media_type': 'unknown', 'arp_type': 'arpa', 'arp_timeout': '04:00:00', 'last_input': '00:00:08', 'last_output': '00:00:56', 'output_hang': 'never', 'queues': {'input_queue_size': 0, 'input_queue_max': 75, 'input_queue_drops': 0, 'input_queue_flushes': 0, 'total_output_drop': 0, 'queue_strategy': 'fifo', 'output_queue_size': 0, 'output_queue_max': 40}, 'counters': {'rate': {'load_interval': 300, 'in_rate': 0, 'in_rate_pkts': 0, 'out_rate': 0, 'out_rate_pkts': 0}, 'last_clear': 'never', 'in_pkts': 20, 'in_octets': 6971, 'in_no_buffer': 0, 'in_multicast_pkts': 0, 'in_broadcast_pkts': 0, 'in_runts': 0, 'in_giants': 0, 'in_throttles': 0, 'in_errors': 0, 'in_crc_errors': 0, 'in_frame': 0, 'in_overrun': 0, 'in_ignored': 0, 'in_watchdog': 0, 'in_mac_pause_frames': 0, 'out_pkts': 21, 'out_octets': 6673, 'out_underruns': 0, 'out_errors': 0, 'out_interface_resets': 5, 'out_collision': 0, 'out_unknown_protocl_drops': 0, 'out_babble': 0, 'out_late_collision': 0, 'out_deferred': 0, 'out_lost_carrier': 0, 'out_no_carrier': 0, 'out_mac_pause_frames': 0, 'out_buffer_failure': 0, 'out_buffers_swapped': 0}}


------------------------------------------------------------------------------------------------------
Lab 3: Get Interfaces and mac addresses and export into csv file:

from genie.conf import Genie
import csv 

testbed = Genie.init("testbed.yaml")

device_interface_details = {}

for device in testbed.devices:
    testbed.devices[device].connect()
    interface_details = testbed.devices[device].parse("show interfaces")
    device_interface_details[device] =  interface_details
 
interface_file = "interfaces.csv"
report_fields = ["Interface", "MAC Address"]

with open(interface_file, "w") as f: 
    writer = csv.DictWriter(f, report_fields) 
    writer.writeheader() 
 
    for device, interfaces in device_interface_details.items():
        for interface, details in interfaces.items():
            writer.writerow( 
               {"Interface": interface,
                "MAC Address": details["mac_address"]
                } 
             )   

root@kali:/DevNet# cat interfaces.csv 
Interface,  MAC Address
GigabitEthernet0/2,  5000.0002.0002
GigabitEthernet0/3,  5000.0002.0003
GigabitEthernet0/1,  5000.0002.0001
GigabitEthernet0/0,  5000.0002.0000

-------------------------------------------------------------------------------------------------------------
Lab 4 : add device in header:

from genie.conf import Genie
import csv 

testbed = Genie.init("testbed.yaml")

device_interface_details = {}

for device in testbed.devices:
    testbed.devices[device].connect()
    interface_details = testbed.devices[device].parse("show interfaces")
    device_interface_details[device] =  interface_details
 
interface_file = "interfaces.csv"
report_fields = ["Device", "Interface", "MAC Address"]

with open(interface_file, "w") as f: 
    writer = csv.DictWriter(f, report_fields) 
    writer.writeheader() 
 
    for device, interfaces in device_interface_details.items():
        for interface, details in interfaces.items():
            writer.writerow( 
               {
       "Device": device,
       "Interface": interface,
                "MAC Address": details["mac_address"]
                } 
             )
root@kali:/DevNet# cat interfaces.csv 
Device,Interface,MAC Address
IOSv-L2-1,GigabitEthernet0/2,5000.0002.0002
IOSv-L2-1,GigabitEthernet0/3,5000.0002.0003
IOSv-L2-1,GigabitEthernet0/1,5000.0002.0001
IOSv-L2-1,GigabitEthernet0/0,5000.0002.0000


---------------------------------------------------------------------------------------------------------------

Good Luck https://www.linkedin.com/in/ahmedms/


































Comments

Popular posts from this blog

Kubernetes 104: Create a 2-node k3s cluster with k3sup

DevNet 103: Network Automation Using Python

How to configure OSPF on Palo Alto Networks Firewall?