- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page

Python scripting to download Cisco IOS
[ Edited ]- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
04-10-2018 12:01 PM - edited 04-10-2018 12:08 PM
Hello,
I'm working on a script to use python within NetMRI to get our network devices to download their code from a local SCP server without intervention. I'm running under the assumption that I can use NetMRI in the place of something like ansible to actually reach out and touch the devices (Please let me know if I'm wrong!)
The problem I keep running into is that NetMRI will run the script fine, but will only peform the first "show version" and then stop.
How do I take the output from a command and put it into a python variable without NetMRI just running the command and telling me it's finished?
The python portion actually works quite well, I can run it on other boxes with no issue. My main problem is getting this to work with NetMRI. Also please excuse the mess, I'm sure there are better ways to write this, I'm just a noob.
###########################################################################
## Export of Script: Python netmri_easy test ## Script-Level: 1 ## Script-Category: netmti_easy.py tests ## Script-Language: python ###########################################################################
# BEGIN-SCRIPT-BLOCK
# Script-Filter:
# $vendor = "Cisco"
# END-SCRIPT-BLOCK
defaults = {
"api_url": api_url,
"http_username": http_username,
"http_password": http_password,
"job_id": job_id,
"device_id": device_id,
"batch_id": batch_id
}
import re, difflib
import urllib.request
from netmri_easy import NetMRIEasy
easy = NetMRIEasy(**defaults)
# NOTE: Look at the show ver output and take both the location of the file and it's name.
output = (easy.send_command("show version | i image"))
#output = ('System image file is "flash:c3560-ipservicesk9-mz.150-2.SE11.bin"')
Location_Clean = ((re.search('(?<=\")(.*?)(?=\',(output))).group(0))
current_code = ((re.search('(?<=\(.*?)(?=")',(output))).group(0))
# NOTE: Do all kinds of crap to take the text from the code webpage and use the closest match to what we are running.
list_object=urllib.request.urlopen("SCP Server list").readlines()
SearchCode = [item.decode("utf-8") for item in (list_object)]
prettylist = [item.replace("\n","") for item in (SearchCode)]
newver = (str(difflib.get_close_matches(current_code,prettylist ,(1))))
# NOTE: Take the output newver, and split it in half to get the new file's hash and it's name
MD5 = ((re.search('(?<=\,)(.*?)(?=\')',(newver))).group(0))
command_ver = ((re.search('(?<=\')(.*?)(?=\,)',(newver))).group(0))
# NOTE: Create a command to download the new code based on all the crap we have running.
command = ("copy scp://") + ("Username") + ("@") + ("IP ADDRESS") + (command_ver) + (" ") + (Location_Clean) + (":") + '\n' + '\n' + ("Password") + '\n'
# NOTE: Send the command to the device and pray it works.
easy.send_command(command)
Solved! Go to Solution.

Re: Python scripting to download Cisco IOS
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
04-11-2018 03:54 PM
You did it before
output = (easy.send_command("show version | i image"))
Now just change
easy.send_command(command)
to
copy = (easy.send_command(command))
Also in NetMRI top block, you might want to add Script-Timeout:300
This will wait 5 minutes before timing out the transfer for the file
Sif
Twitter: https://twitter.com/sifbaksh
Re: Python scripting to download Cisco IOS
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
04-11-2018 04:40 PM
Thanks very much for your reply!
So initially, when I do the below:
output = (easy.send_command("show version | i image"))
It's actually copying the output of that command and saving it as a variable? That's perfect. Wasn't sure if that was actually happening :-)
Now, towards the bottom end, when I've done all my regex, do I issue a separate command? In other words, once I do
copy = (easy.send_command(command))
What do I then do with the copy variable? Apologies if that's a silly question.
Re: Python scripting to download Cisco IOS
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
04-17-2018 06:01 AM
Depends, you can parse it to make sure that the file transfer correctly and if it didn't generate an issue so you know that it didn't.
You don't need to send the command to a variable unless you want to parse the output and do something extra with it
Twitter: https://twitter.com/sifbaksh
Re: Python scripting to download Cisco IOS
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
04-17-2018 06:06 AM
Thanks again for replying.
I was able to get the syntax right and the command works. I'm having an issue now where the command is saying complete before the file transfer is finished. (I opened a new thread, figured that would be best)
Re: Python scripting to download Cisco IOS
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
11-22-2018 03:43 AM
Cisco has composed a python apparatus, known as Cisco Automatic Upgrade Tool, that updates an IOS-XR gadget with pre and post checks. Did you check Cisco CPOC video? It clarifies almost everything about this instrument which you should watch before begin perusing this post.
community.infoblox member
thetermpapers.net