Network Automation With Python!

My DevNet journey is going on! 💥

Code Mission

With this code I want to help all the engineers who have to backup configurations, store or retrieve data from multiple devices into a single or multiple devices-named files. Thanks to this code you’ll be able to push a defined list of commands to a static or dynamic list of IP addresses.
I created it when, during a datacenter outage, the TAM asked me to provide him the FAN/PSU status of several devices every day. I immediately thought:

“Am I (or my colleagues) supposed to login to 30/40 devices one by one every day for the next 5/6 weeks?!”

Well, the answer was NO, of course.
So here we are! 💥
First, I created the “static” version where I put all the devices' IP Addresses into a file and use it through the python script. Then I thought about scalability so I decided to add the “networkscan” module in order to not add the single IP into the external file, but use the code to discover the devices in the provided network! Cool, isn’t it? 😊

Code details

There are two versions:

  • Static: The code will use the list of the IP Addresses defined inside the file called “IPAddressList.txt”. Please put one IP per line.
  • Dynamic: The code will ask you which network you want to scan and he creates a list with all the reachable devices (via ICMP) in the provided network.

Each version has two different ways to store the data:

  • Create: The code will create one file for each device
  • Append: The code will create a single file with all the outputs

It’s up to you!

Static code list of operations:

  • Open the file called “IPAddressList.txt” and store it in a list
  • Open the file called “Commands.txt” and store it in a list
  • Verify if the devices in the IPAddressList are reachable via ICMP
  • SSH connection to each IP
  • Push commands (stored in the Commands list)
  • Store outputs in dedicated files (create version) o unique file (append version)

Dynamic code list of operations:

  • Scan a network provided by the user (via ICMP, it’s automatic)
  • Store the reachable IP in a list
  • SSH connection to each IP. If it’s not a Cisco device skip it.
  • Push commands (stored in the Commands list)
  • Store outputs in dedicated files (create version) o unique file (append version)

Python Modules

Install the following modules:

pip3 install netmiko
pip3 install ping3
pip3 install networkscan

Username / Password ?

The code does not have any pre-defined username/password. When you run the code it will ask you the username and password for the devices.
Why? It’s simple, security: you don’t want to store your credentials in a file, keep them safe with appropriate password manager (btw, I use BitWarden ), so I decided that you must insert your credential manually. No more colleagues credentials… 😉 😏

Cisco OS

During my coding session I prepared a eve-ng machine where I installed and configured some Cisco devices. I successfully tested the code with the following Cisco OS:

  • IOS XE
  • NX-OS
  • IOS

If you want to add more, please drop me a message through social networks!😊
👈 You can find the relative icons here on the left of the page

Test the code

Please, don’t test the code in production, create a virtual and safe environment.
Here how to create a virtual environment and test the code:

python3 -m venv TEST
source TEST/bin/activate
cd TEST/
git clone https://github.com/thetechguy-it/show_commands.git
cd show_commands/
pip3 install netmiko
pip3 install ping3
pip3 install networkscan
sudo python3 < scriptname >.py

The code will create a folder called “BACKUP” and then another folder with the date. Inside this second folder, you will find all your files.

Example: example1

If you have a lab environment with some Cisco switches/routers, can you test it? Comments/feedback/improvements are appreciated!
Thank y’all 🙏
Yes, it may not be the best written code ever… but it works!

CODE 👉 https://github.com/thetechguy-it/show_commands

Thanks for your time I hope that you’re enjoying my blog!
If you have some questions, please drop me a message through social networks!😊
👈 You can find the relative icons here on the left of the page

Riccardo