Saturday, 19 May 2018

Network Device Config Tool (Python)


As a network engineer, we are often asked to extract the output of a set of commands to a set of network devices. Quite a simple task to do, but not if the number of network devices is at least hundreds or maybe thousands. This would be an easy task if you do have a monitoring system like Solarwinds that can automate the configuration for you, but what if you don't have this kind of tools or you want doing it your own way, automated?

Hopefully this blog post will be able to help you learn or start your journey in network automation the way how I started doing it.

I have used the following tools to create & simulate this script in a lab environment prior deploying it in production networks:

  • Eve-NG
  • Cisco IOL
  • CentOS
  • Netmiko

Eve-NG is the network device simulator engine here. This is just my preference, but you may use other network simulator that you know of or comfortable with.

Cisco IOL is the network device image that we will use in this case.

CentOS is my preferred Linux distribution which will then serve here as the server that will execute the python script to login to every network devices via SSH and execute the set of commands specified.

Netmiko is a python module that I have used here to perform SSH login to the network device.

Let's begin.

I have created the following files to complete the script:


Node Database - contains the list of network devices that will be accessed in the script

This file needs at least the following data per line:
  1. Device Hostname
  2. Device Management IP
  3. Device Type

Login Credential - contains the common SSH login credentials of the network devices

This file needs at least the following data per line:
  1. Username
  2. Password
CLI Commands - contains the list of "Cisco" CLI commands

Config Tool - contains the main python script that will automate the SSH login and CLI command execution to the network devices

List of files below are the sample output logs generated from the actual script in the lab environment:


I will try to break down below on how the python script is doing it.

  1. "extract" function is created to read the data inside the supplied file name
  2. "current_time" variable is declared which will then be used to include the time stamp in the log file naming
  3. "node_db_filename" variable is declared to hold the value of node database file name
  4. "node_login_filename" variable is declared to hold the value of login credentials filename
  5. "cli_commands_filename"  variable is declared to hold the value of CLI commands filename
  6. "node_db_raw" variable will contain the raw data of  node database file name
  7. "node_login_raw"  variable will contain the raw data of  login credentials file name
  8. "cli_commands_raw"  variable will contain the raw data of  CLI commands file name
  9. "node_db" ordereddict variable will then contain the parsed values of the node database data
  10. "node_login"  ordereddict variable will then contain the parsed values of the login credential data
  11. "cli_commands" ordereddict variable will then contain the parsed values of the CLI commands data
  12. "device_params" ordereddict variable will contain all the necessary network device information needed by netmiko module to access the network devices supplied in the script
  13. "netmiko_params" ordereddict variable will contain only a network device information which will then be stored and consolidated in "device_params" variable
  14. The succeeding loops will then iterate all information supplied in "device_params" variable and will login via SSH using the netmiko module. Every CLI commands specified in the script will then be issued and logged into a file with this name formatting - "[Hostname]_[IP]_[Timestamp].txt"
This is the first version of this script so please bear with me if you happen to see some errors along the way. I would be happy to hear from you for any suggestions, questions or comments you may have.

Cheers!

No comments:

Post a Comment