cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
350
Views
1
Helpful
1
Comments
cherifi.m85
Spotlight
Spotlight

In the previous blog, we saw how to retrieve OSPF neighbor status for a single device. Now we will apply the same logic but for multiple network devices using Netmiko.

First of all ! the raw case without parsers! then with parsers and we finish with an application case to retrieve some useful data.

We will keep the same topology. Here is the link to the previous blog: https://community.cisco.com/t5/networking-blogs/what-is-the-status-of-the-ospf-neighbors-of-your-devices/ba-p/4923530

Here is the script for the first case:

 

# Import the ConnectHandler Function to create an Object for R1
from netmiko import ConnectHandler

# Parameters of Devices
password = "cisco"

R1 = {
    "device_type": "cisco_ios",
    "host": "192.168.1.251",
    "username": "Admin",
    "password": password,
}

R2 = {
    "device_type": "cisco_ios",
    "host": "192.168.1.252",
    "username": "Admin",
    "password": password,
}

R3 = {
    "device_type": "cisco_ios",
    "host": "192.168.1.253",
    "username": "Admin",
    "password": password,
}

Devices=[R1, R2, R3]

# Connect to R1 via SSH
for Device in Devices:

        net_connect = ConnectHandler(**Device)

        # Retrieve the result of "show ip ospf neighbor" command with the hostname of each device and display it
        print(net_connect.find_prompt()[:-1])
        output = net_connect.send_command('show ip ospf neighbor')
        print(output)
		print(90*'*')
        # End the SSH session for R1
        net_connect.disconnect()

 

Here is the result!

Raw.png

As we have indicated, this display is well adapted for humans! For applications, we need structured data in containers: therefore, the use of parsers!

Here is the modification of the previous script! Note that we use TextFSM

 

# Import the ConnectHandler Function to create an Object for R1
from netmiko import ConnectHandler
from pprint import pprint

# Parameters of Devices
password = "cisco"

R1 = {
    "device_type": "cisco_ios",
    "host": "192.168.1.251",
    "username": "Admin",
    "password": password,
}

R2 = {
    "device_type": "cisco_ios",
    "host": "192.168.1.252",
    "username": "Admin",
    "password": password,
}

R3 = {
    "device_type": "cisco_ios",
    "host": "192.168.1.253",
    "username": "Admin",
    "password": password,
}

Devices=[R1, R2, R3]

# Connect to R1 via SSH
for Device in Devices:

    net_connect = ConnectHandler(**Device)

    # Retrieve the result of "show ip ospf neighbor" command with the hostname of each device and display it
    print(net_connect.find_prompt()[:-1])
    output = net_connect.send_command('show ip ospf neighbor', use_textfsm=True)
    pprint(output)
	print(90*'*')
    # End the SSH session for R1
    net_connect.disconnect()

 

And the result!

Parsed.png

Now, we will try to apply the same resonance as with the previous blog to demonstrate the usefulness of the parsers! For each router, we will retrieve the ID of its OSPF neighbors, the local interfaces directly connected to the neighbors and the IP addresses of the neighbors for their interfaces connected to the target router in question! It's not complicated, here's the script!

 

# Import the ConnectHandler Function to create an Object for R1
from netmiko import ConnectHandler

# Parameters of Devices
password = "cisco"

R1 = {
    "device_type": "cisco_ios",
    "host": "192.168.1.251",
    "username": "Admin",
    "password": password,
}

R2 = {
    "device_type": "cisco_ios",
    "host": "192.168.1.252",
    "username": "Admin",
    "password": password,
}

R3 = {
    "device_type": "cisco_ios",
    "host": "192.168.1.253",
    "username": "Admin",
    "password": password,
}

Devices=[R1, R2, R3]

# Connect to R1 via SSH
for Device in Devices:

    net_connect = ConnectHandler(**Device)

    # Retrieve the result of "show ip ospf neighbor" command with the hostname of each device and display it
    print(120*'*')
    print(net_connect.find_prompt()[:-1])
    output = net_connect.send_command('show ip ospf neighbor', use_textfsm=True)
    # For each neighbor N: access to its ID, Interface, Address and State
    for N in output:
        Id, Interface, Address, State=N.get('neighbor_id'), N.get('interface'), N.get('address'), ((N.get('state')).split('/'))[1]
        print(f"Neighbor {Id}\n     - Connected to it via the interface: {Interface} having the address: {Address}, its state is {State}")
        # End the SSH session for R1
        net_connect.disconnect()

 

And the result!

Use_Case.png

In the following blogs, we will see how to apply the same case with another framework! see you soon !

Here is the directory link on Github: https://github.com/cherifimehdi/OSPF_neighbors_Netmiko_Multi_Devices

Please share and like it! Star it on Github if you found it helpful and helpful! THANKS

1 Comment
Getting Started

Find answers to your questions by entering keywords or phrases in the Search bar above. New here? Use these resources to familiarize yourself with the community: