If you have more than three Cisco Nexus switches in nx-os mode, and you are not using Cisco DCNM or any other similar tool, you probably already have encountered this question: How to automate file uploads to your Cisco Nexus switches?
Here is a turnkey Python script using Netmiko’s SCP function to do this.
This script is very simple, it relies only on Netmiko functions and SCP. But it does its job very well and I share it here because it can certainly help you to save time.
What is Netmiko?
If you want to know more about Netmiko, here is the git page of its creator, Kirk Byers: https://github.com/ktbyers/netmiko
On my side, I’ve already written a post on this blog about another script I created based on Netmiko.
I can’t thank Kirk Byers enough for his huge contribution on automation for network engineers. And I recommend you check out his web site and, as I wrote in a previous article about network automation, take his free Python course for network engineers.
Get the script
So, how to automate file uploads to your Cisco Nexus switches?
The script is available on my git page, here: https://github.com/jerome-t/nxos-scp-upload
It is based on Kirk’s example, available here: https://pynet.twb-tech.com/blog/automation/netmiko-scp.html
How to use it?
To use this script, you must have SCP on your machine. Sorry Windows users but I did not tested this with WinSCP or anything similar. Use this script on a Linux machine or container.
Then, make a git clone of my git repository above and edit the script. There are two lines to adapt to your environment:
- Line 6: you can put here the list of your Nexus switches. If needed, you can replace this by an external file or inventory tool. There is no limit here.
sw_list = ['host1.test.net', 'host2.test.net', 'host3.test.net']
- Line 9: you must specify here the nx-os binary file and it’s location. Here I put as example a local file in the same directory of the script.
source_file = "./put_your_nxos_file_here.bin"
Note: to make a test, you can use a small txt file of two or three lines, before uploading a very large nx-os binary file.
See it in action – upload files!
Once you’ve done that, run the script:
$ ./nxos-scp-upload.py
For each host, you have to confirm you want do the upload. You can stop the script with Ctrl-C.
Then, for each switch, the output will give you three information:
- Does the file already exist on the remote switch? (true or false)
- Has the file been transferred? (true or false) – if it already exists, it will not be transferred.
- Is the MD5 checksum of the file correct? (true or false)
Here is an example of the output:
$ ./nxos-scp-upload.py Please insert your Nexus username: test And your password Password: Upload on: host1.test.net Hit enter to continue or Ctrl C to stop: ---------------------------------------- File exists already: False File transferred: True MD5 verified : True ======================================== Upload on host2.test.net Hit enter to continue or Ctrl C to stop: ---------------------------------------- File exists already: False File transferred: True MD5 verified : True ======================================== All copy are done ========================================
If the file already exists, you will have the following output:
---------------------------------------- File exists already: True File transferred: False MD5 verified : True ========================================
As you can see, this script can also be used to just make a MD5 checksum comparison.
Happy uploading!
Update May 2021:
I updated this script with more features, including multi-threading.
You can clone it from here: https://github.com/jerome-t/netmiko-scp-multi-thread-upload
Hello,
I suggest to use threading. It speeds up the task. Upto 30 concurrent connection the performance per thread is as good as if it were just one. I do connect to 500 nodes at the same time. Here is a little performance drawback per thread, but it is still much much faster.
Hi Istvan,
Thank you for your comment, you were right.
Now, I updated this script with multi-threading, check here: https://github.com/jerome-t/netmiko-scp-multi-thread-upload
Cheers,
Jerome