DevNet 104: Network Automation Using NAPALM


Network Automation Using Napalm:


Topology:



Configure router to using napalm libraries:
## Cisco IOS
hostname ESW1
ip domain name cloud.me
crypto key generate rsa
 How many bits in the modulus [512]: 1024/2048
ip ssh version 2
!
aaa new-model
aaa authentication login default local
aaa authorization exec default local
!
username admin privilege 15 secret cisco
!
line vty 0 4
 transport input ssh
!
! for testing only: no authentication on console
aaa authentication login no_auth none
line con 0
 privilege level 15
 login authentication no_auth

 archive
 path flash:
!
ip scp server enable


Configure OSPF by python library (NAPALM):

1-  Router ESW1:

import json
import  napalm

driver = napalm.get_network_driver('ios')
optional_args = {}
optional_args['secret'] = 'cisco'
device = driver('192.168.23.100', 'admin', 'cisco', optional_args=optional_args)
device.open()

print('Accessing Router: 192.168.23.100')

device.load_merge_candidate(filename='ospf-R1.cfg')

diffs = device.compare_config()
if len(diffs) > 0:
        print(diffs)
        device.commit_config()
else:
        print('No changes required.')
        device.discard_config()

device.close()

Configure file of ospf on ESW1 (ospf-R1.cfg)
router ospf 1
network 3.3.3.3 0.0.0.0 area 0
network 172.24.7.2 0.0.0.0 area 0

Running Script:
Result contain different between previous and merge configure commands on ESW1.
and commit different commands.
root@kali:/home# python3 ospf-R1.py 
Accessing Router: 192.168.23.100
+network 3.3.3.3 0.0.0.0 area 0
+network 172.24.7.2 0.0.0.0 area 0


2-  Router ESW2:

import json
import  napalm

driver = napalm.get_network_driver('ios')
optional_args = {}
optional_args['secret'] = 'cisco'
device = driver('192.168.23.101', 'admin', 'cisco', optional_args=optional_args)
device.open()

print('Accessing Router: 192.168.23.101')

device.load_merge_candidate(filename='ospf-R2.cfg')

diffs = device.compare_config()
if len(diffs) > 0:
        print(diffs)
        device.commit_config()
else:
        print('No changes required.')
        device.discard_config()

device.close()

Configure file of ospf on ESW2 (ospf-R2.cfg)
router ospf 1
network 4.4.4.4 0.0.0.0 area 0
network 172.24.7.1 0.0.0.0 area 0

Running Script:
Result contain different between previous and merge configure commands on ESW2.
and commit different commands.
root@kali:/home# python3 ospf-R2.py  
Accessing Router: 192.168.23.101
+network 4.4.4.4 0.0.0.0 area 0
+network 172.24.7.1 0.0.0.0 area 0

Show running-config on ESW1:

ESW1#show running-config | sec router
router ospf 1
 log-adjacency-changes
 network 3.3.3.3 0.0.0.0 area 0
 network 172.24.7.2 0.0.0.0 area 0

      How to using get method by napalm libraries

Get fact of  ESW1:

import json
import napalm
driver = napalm.get_network_driver('ios')
optional_args = {}
optional_args['secret'] = 'cisco'
device = driver('192.168.23.103', 'admin', 'cisco', optional_args=optional_args)
device.open()

output = device.get_facts()
print(json.dumps(output,indent=4))
device.close()
root@kali:/home# python3 facts.py
{
    "uptime": 6600,
    "vendor": "Cisco",
    "os_version": "3700 Software (C3745-ADVIPSERVICESK9-M), Version 12.4(25d), RELEASE SOFTWARE (fc1)",
    "serial_number": "FTX0945W0MY",
    "model": "3745",
    "hostname": "ESW1",
    "fqdn": "ESW1.cloud.me",
    "interface_list": [
        "FastEthernet0/0",
        "FastEthernet0/1",
        "FastEthernet1/0",
        "FastEthernet1/1",
        "FastEthernet1/2",
        "FastEthernet1/3",
        "FastEthernet1/4",
        "FastEthernet1/5",
        "FastEthernet1/6",
        "FastEthernet1/7",
        "FastEthernet1/8",
        "FastEthernet1/9",
        "FastEthernet1/10",
        "FastEthernet1/11",
        "FastEthernet1/12",
        "FastEthernet1/13",
        "FastEthernet1/14",
        "FastEthernet1/15",
        "Vlan1",
        "Loopback0"
    ]
}

Show ARP Table on ESW1:
import json
import napalm
driver = napalm.get_network_driver('ios')
optional_args = {}
optional_args['secret'] = 'cisco'
device = driver('192.168.23.100', 'admin', 'cisco', optional_args=optional_args)
device.open()

output = device.get_arp_table()
print(json.dumps(output,indent=4))
device.close()
root@kali:/home# python3  arp-table.py 
[
    {
        "interface": "FastEthernet0/0",
        "mac": "C4:02:1C:D0:00:00",
        "ip": "192.168.23.100",
        "age": 0.0
    },
    {
        "interface": "FastEthernet0/0",
        "mac": "00:50:56:C0:00:08",
        "ip": "192.168.23.1",
        "age": 0.0
    },
    {
        "interface": "FastEthernet0/1",
        "mac": "C4:03:26:B8:00:01",
        "ip": "172.24.77.1",
        "age": 58.0
    },
    {
        "interface": "FastEthernet0/0",
        "mac": "00:0C:29:EF:6C:28",
        "ip": "192.168.23.132",
        "age": 79.0
    },
    {
        "interface": "FastEthernet0/1",
        "mac": "C4:02:1C:D0:00:01",
        "ip": "172.24.77.2",
        "age": 0.0
    }
]

Get Interfaces on ESW1:
import json
import napalm
driver = napalm.get_network_driver('ios')
optional_args = {}
optional_args['secret'] = 'cisco'
device = driver('192.168.23.100', 'admin', 'cisco', optional_args=optional_args)
device.open()

output = device.get_interfaces()
print(json.dumps(output,indent=4))
device.close()
root@kali:/home# python3  get_interfaces.py 
{
    "FastEthernet0/0": {
        "is_enabled": true,
        "is_up": true,
        "description": "*** Unused for Layer2 EtherSwitch ***",
        "mac_address": "C4:02:1C:D0:00:00",
        "last_flapped": -1.0,
        "speed": 10
    },
    "FastEthernet0/1": {
        "is_enabled": true,
        "is_up": true,
        "description": "*** Unused for Layer2 EtherSwitch ***",
        "mac_address": "C4:02:1C:D0:00:01",
        "last_flapped": -1.0,
        "speed": 10
    },
    "FastEthernet1/0": {
        "is_enabled": true,
        "is_up": false,
        "description": "",
        "mac_address": "C4:02:1C:D0:F1:00",
        "last_flapped": -1.0,
        "speed": 100
    },
    "FastEthernet1/1": {
        "is_enabled": true,
        "is_up": false,
        "description": "",
        "mac_address": "C4:02:1C:D0:F1:01",
        "last_flapped": -1.0,
        "speed": 100
    },
    "FastEthernet1/2": {
        "is_enabled": true,
        "is_up": false,
        "description": "",
        "mac_address": "C4:02:1C:D0:F1:02",
        "last_flapped": -1.0,
        "speed": 100
    },
    "FastEthernet1/3": {
        "is_enabled": true,
        "is_up": false,
        "description": "",
        "mac_address": "C4:02:1C:D0:F1:03",
        "last_flapped": -1.0,
        "speed": 100
    },
    "FastEthernet1/4": {
        "is_enabled": true,
        "is_up": false,
        "description": "",
        "mac_address": "C4:02:1C:D0:F1:04",
        "last_flapped": -1.0,
        "speed": 100
    },
    "FastEthernet1/5": {
        "is_enabled": true,
        "is_up": false,
        "description": "",
        "mac_address": "C4:02:1C:D0:F1:05",
        "last_flapped": -1.0,
        "speed": 100
    },
    "FastEthernet1/6": {
        "is_enabled": true,
        "is_up": false,
        "description": "",
        "mac_address": "C4:02:1C:D0:F1:06",
        "last_flapped": -1.0,
        "speed": 100
    },
    "FastEthernet1/7": {
        "is_enabled": true,
        "is_up": false,
        "description": "",
        "mac_address": "C4:02:1C:D0:F1:07",
        "last_flapped": -1.0,
        "speed": 100
    },
    "FastEthernet1/8": {
        "is_enabled": true,
        "is_up": false,
        "description": "",
        "mac_address": "C4:02:1C:D0:F1:08",
        "last_flapped": -1.0,
        "speed": 100
    },
    "FastEthernet1/9": {
        "is_enabled": true,
        "is_up": false,
        "description": "",
        "mac_address": "C4:02:1C:D0:F1:09",
        "last_flapped": -1.0,
        "speed": 100
    },
    "FastEthernet1/10": {
        "is_enabled": true,
        "is_up": false,
        "description": "",
        "mac_address": "C4:02:1C:D0:F1:0A",
        "last_flapped": -1.0,
        "speed": 100
    },
    "FastEthernet1/11": {
        "is_enabled": true,
        "is_up": false,
        "description": "",
        "mac_address": "C4:02:1C:D0:F1:0B",
        "last_flapped": -1.0,
        "speed": 100
    },
    "FastEthernet1/12": {
        "is_enabled": true,
        "is_up": false,
        "description": "",
        "mac_address": "C4:02:1C:D0:F1:0C",
        "last_flapped": -1.0,
        "speed": 100
    },
    "FastEthernet1/13": {
        "is_enabled": true,
        "is_up": false,
        "description": "",
        "mac_address": "C4:02:1C:D0:F1:0D",
        "last_flapped": -1.0,
        "speed": 100
    },
    "FastEthernet1/14": {
        "is_enabled": true,
        "is_up": false,
        "description": "",
        "mac_address": "C4:02:1C:D0:F1:0E",
        "last_flapped": -1.0,
        "speed": 100
    },
    "FastEthernet1/15": {
        "is_enabled": true,
        "is_up": false,
        "description": "",
        "mac_address": "C4:02:1C:D0:F1:0F",
        "last_flapped": -1.0,
        "speed": 100
    },
    "Vlan1": {
        "is_enabled": false,
        "is_up": false,
        "description": "",
        "mac_address": "C4:02:1C:D0:00:00",
        "last_flapped": -1.0,
        "speed": 100
    },
    "Loopback0": {
        "is_enabled": true,
        "is_up": true,
        "description": "",
        "mac_address": "",
        "last_flapped": -1.0,
        "speed": 8000
    }
}


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

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?