Ever though about an easier way to configure tons of network devices easily? Python is definitely the answer to get started, and as long as you have a basic understanding of programming, it’s pretty simple, and tons of fun!
Requirements
- Python version 3.9 or higher
- An IDE that supports the Python language
- Some sort of appliance you can connect to via SSH
- I will be using a Cisco IOS appliance here running in GNS3
- A Linux VM will also work
In this post, I’ll be using Visual Studio Code as my IDE, you can download that here if you are interested.
You can find all of my scripts, and other projects on my GitHub page.
Before you Start
Note: This is only if you are using Visual Studio Code as your IDE
VS Code gets a little funky when it comes to installed python libraries. First, make sure you install the latest version of Python from the Microsoft Store… Code will look to the installation path of this by default.
After installation:
- Open VS Code
- Open or create a Python file (use the extension .py to tell Code to use Python)
- On the bottom-left of the window, click the Python version number, this should open a box at the top of the screen
- Select “Entire Workspace”
- Choose the following option from the list:
Now you should be all goood to go!
Installing Netmiko
Netmiko is, in my opinion, the best all-in-one network automation tool to use in Python. Netmiko has a multitude of supported devices; including Linux, Cisco (IOS, NX-OS, etc), HP Enterprise, and TONS more.
To install Netmiko on your computer, open a terminal and type:
|
|
Now, open your IDE and create a new Python file, enter the following code and run it to test installation:
|
|
If you run this, and it doesn’t do anything, that’s good! This just means that the library imported with no problems.
Setting up Netmiko
Network automation is all about doing the similar tasks on tons of devices from a single place. That doesn’t mean you have to have a hundred devices to do some automation work.
In my case, I will be using the Cisco Modeling Labs IOSv router image (in GNS3), so my commands will be IOS specific. I’ll have some information down below so you can set up a Linux machine if you want as well :)
First, we’ll need to import the ConnectHandler module from Netmiko:
|
|
Then let’s get some username and password information from the user:
|
|
Now we’ll want to set up information that Netmiko will use for the actual connection. Type the following code and note the comments:
|
|
This code creates a Python dictionary that contains the items device_type, host, password, secret, and use_keys, and then sets a variable to the string ‘ip address’.
Most of these are pretty self explanatory, use_keys will bypass the prompt SSH gives back when connecting to an unknown device for the first time.
If you wanted to try this on multiple devices, tweak your code to use a list for hosts and a for loop as such:
|
|
Sending your first command
Now we’re ready to send a command to your device / devices! Let’s get started by creating a Netmiko ConnectHandler
variable by adding this line:
|
|
Now, we can act on the variable ssh to send commands to our device. Using send_command, which is used specifically for sending a single command via Netmiko (this will utilize the command variable we set in the previous step):
|
|
Now go ahead and run your program! If you put everything in correctly, you should see the output of the Linux command ip addr
in your terminal.
Sending multiple commands
Automation isn’t just about sending a single command to devices. While this can be pretty useful, especially for gathering some information (like switch configs), sending multiple commands can prove to be extremely useful and time-saving, especially when you’re configuring tons of devices.
In the previous step, we used the function send_command(), which only sends one command. For multiple configuration commands, there is a function called send_config_set(), which accepts a list in its parameters.
So first, let’s create a list:
|
|
Now, instead of send_command(), we can use the following to send our config variable instead:
|
|