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:
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.