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

I will show you in the following blogs how to apply the same case by other frameworks and also in the case of several devices.

We have already installed Netmiko. For more details, visit https://github.com/ktbyers/netmiko (Thank you Sir Kirk Byers!)

  This is our topology in question!

Topologie.png

We will target the OSPF neighbors of R1. All routers have been configured

The script is simple! here it is ! It’s easy to understand all the steps!

 

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

# Parameters of R1
password = xxx

R1 = {
    "device_type": "cisco_ios",
    "host": "192.168.1.251",
    "username": "xxx",
    "password": xxx
}
# Connect to R1 via SSH
net_connect = ConnectHandler(**R1)

# Retrieve the result of "show ip ospf neighbor" command
output = net_connect.send_command('show ip ospf neighbor')

# Display the result
print(output)

# End the SSH session for R1
net_connect.disconnect()

 

Here is the result!

Raw_Result.png

It's simple! Right!

This result is better suited for reading in raw format. It is better suited for humans. But in the application case, it is necessary to pack the result into a container, from where the parsers are used.

Assuming we just want to retrieve the addresses of OSPF neighbors, and their state (DR, BDR or DROTHERS!) with the interfaces connected to them. Maybe even store the result in a separate file. At this level, parsers are very suited!.

First of all, here is the modification of the previous script with the use of a parser. Note that for the case of Netmiko, the result is structured in parsers from external libraries such as: TextFSM, Genie or TTP. The simplest case is to use TextFSM because it is installed by default with Netmiko.

Nothing complicated, here's the modification!

 

# Import the ConnectHandler Function to create an Object for R1
from netmiko import ConnectHandler
from pprint import pprint
# Parameters of R1
password = xxx

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


# Connect to R1 via SSH
net_connect = ConnectHandler(**R1)

# Retrieve the parsed result of "show ip ospf neighbor" command
output = net_connect.send_command('show ip ospf neighbor', use_textfsm=True)

# Display the result
pprint(output)

# End the SSH session for R1
net_connect.disconnect()

 

Here is the result! You notice that everything is structured in a Python list containing two dictionaries

Parsed_Result.png

Now we will see the power of parsers. In what follows, we will try to recover the connected interfaces, their addresses and the state of R1's neighbors, something that cannot be done with raw data. Here is the script:

 

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

# Parameters of R1
password = xxx

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


# Connect to R1 via SSH
net_connect = ConnectHandler(**R1)

# Retrieve the parsed result of "show ip ospf neighbor" command
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!

Modified_Parsed_Result.png

This is an interesting example! In the following blog, we will see how to apply the same case for multiple devices! see you soon !

Here the link for the repo in Github: https://github.com/cherifimehdi/OSPF_neighbors_Netmiko_version_Case_one_device

Please share and like it! Start it in Github if you found it useful and helpful! Thank you!

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: