Automation Scripts

Reply
Highlighted
Accepted Solution

Python scripting to download Cisco IOS

[ Edited ]
notanexpert
Techie
Posts: 1
4934     0

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('(?<=\")(.*?)(?=\Smiley Happy',(output))).group(0))

current_code = ((re.search('(?<=\Smiley Happy(.*?)(?=")',(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)

Re: Python scripting to download Cisco IOS

Adviser
Posts: 373
4935     0

You did it before Smiley Happy

 

 

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

 

Follow me on LinkedIn: https://www.linkedin.com/in/sifbaksh
Twitter: https://twitter.com/sifbaksh

Re: Python scripting to download Cisco IOS

notanexpert
Techie
Posts: 1
4935     0

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

Adviser
Posts: 373
4935     0

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

 

Follow me on LinkedIn: https://www.linkedin.com/in/sifbaksh
Twitter: https://twitter.com/sifbaksh

Re: Python scripting to download Cisco IOS

notanexpert
Techie
Posts: 1
4935     0

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

WilliamJohn
Techie
Posts: 1
4935     0

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

Showing results for 
Search instead for 
Do you mean 

Recommended for You