Preparing Python Scripting for Network Automation
Network automation seriously has been talking since 2010. Cisco and other vendor are trying to bring to compliance their product to get data from their product with various scripts. The methods are changing but the goal is the same; to get useful data from network devices. Receiving data on network devices has been done for a long time if you know to script. But today it's piece of cake with python language.
Python has a large library also to learn is pretty easy. Let's prepare our windows machine to write and run a python script.
Installing Python Program on Windows
I will install python 3.7 or 3.8 version on my computer because the latest netmiko works with 3.7and 3.8 python versions.
Download the python 3.7 version from this link and Install from .exe. After completed installation, open the command prompt or open from windows programs like below.
Installing PIP
What is pip? pip is the standard package manager for Python. It allows you to install and manage additional packages that are not part of the Python standard library.
a-) Download get-pip.py from this Link.
b-) Run below command
Installing Netmiko
Netmiko Multi-vendor library to simplify Paramiko SSH connections to network devices.
Everything is ready to write and run the first script for network automation.
Before running the python script, I have to prepare the eve-ng lab for the test. My simple eve-ng lab is below. You can run every network script on this topology.
You don't need to be a good programmer or smart person. There is lots of script on the internet or you can write or modify your script. Let's try an example. Let's see if it will work. I took the below script from this link
I only changed the IP address and user/pass.
from netmiko import Netmiko
from getpass import getpass
cisco = {
'host': '192.168.252.131',
'username': 'cisco',
'password': 'cisco',
'device_type': 'cisco_ios',
}
net_connect = Netmiko(**cisco)
command = 'show ip int brief'
print()
print(net_connect.find_prompt())
output = net_connect.send_command(command)
net_connect.disconnect()
print(output)
print()
I only took data from a router. Now I will read the router's addresses from a file. I will write the router's addresses to a .txt file. Like below
192.168.252.131
192.168.252.132
192.168.252.133
Then I will modify my script.
from netmiko import Netmiko
from getpass import getpass
f= open("rtr-ips.txt","r")
for IP in f:
cisco = {
'host': IP,
'username': 'cisco',
'password': 'cisco',
'device_type': 'cisco_ios',
}
net_connect = Netmiko(**cisco)
command = 'show ip int brief'
print()
print(net_connect.find_prompt())
output = net_connect.send_command(command)
net_connect.disconnect()
print(output)
print()
If you have a lot of router/switch on your own network. Automation will make your life easier by getting useful data for you.
Thanks for Reading
Referance: https://github.com/ktbyers/netmiko
Comments