I recently contributed with 06 Hybrid get APIs (IPv4 and IPv6) for EIGRP. I'm happy to be the first to introduce the get.py file for Cisco pyATS, what a pleasure.
As I indicated in my previous blog, the idea is to exploit two essential points:
- The resemblance between the commands
- The power of Python f-string where we can introduce a condition
Now, I will give you a little demonstration with a simple hybrid function which aims to retrieve information from OSPF neighbors with Netmiko. This is a small modification of the script introduced in a previous blog, here is the link:
what is the status of the ospf neighbors of your devices
In short, here is the Hybrid_OSPF_Neighbors.py script. Very simple, isn't it?!
def hybrid_neighbors(device, ip='ipv4'):
# Connect to the device via SSH
net_connect = ConnectHandler(**device)
# Retrieve the result of "show ip/ipv6 ospf neighbor" command
output = net_connect.send_command(f"show {'ipv6' if ip!='ipv4' else 'ip'} ospf neighbor")
# Display the result
print(output)
# End the SSH session for the device
net_connect.disconnect()
return print(f"This is {ip} data")
if __name__ == "__main__":
# Import the ConnectHandler Function to create an Object for R1
from netmiko import ConnectHandler
# Parameters of R1
password = "cisco"
R1 = {
"device_type": "cisco_ios",
"host": "192.168.1.251",
"username": "Admin",
"password": password,
}
hybrid_neighbors(R1)
As you will notice, by default the hybrid_neighbors() function allows you to retrieve by default the state of OSPF neighbors for IPv4, to modify in case of IPv6 in the last line of the script hybrid_neighbors(R1, 'ipv6').
The difference is in the line: output = net_connect.send_command(f"show {'ipv6' if ip!='ipv4' else 'ip'} ospf neighbor")
Before running the script, here is the output of the two commands:
For IPv4:
For IPv6:
Now let's see if our script will give us the same outputs
Case IPv4 (By default):
Case IP6:
It's the same result!
Note: The return method allows us to indicate whether the case is 'ipv4' or 'ipv6' .
Here is the link to the Github repository: https://github.com/cherifimehdi/Hybrid_OSPF_Neighbors
Don't forget to Rate it if you find it helpful
Thank you so much