Reply

netmri_easy not running inside of python if statement

New Member
Posts: 5
10240     0

Hello,

While I know support for python in netmri is limited at best, I'm curious to see if anyone else has run into this.

 

I'm running a script that goes through a list of software images on a cisco device and deletes every image except the one that's currently running. 

Here's the relevant code:

 

removed = 0
for line in Image_Files:
     result = re.search('(?<=00:00 )(.*?)(?=$)',line)
     if result:
            if result.group(0) != Running_Code:
                 removed = removed + 1
                 easy.send_command("delete /force flash:" + result.group(0))

 

If I change "easy.send_command" to "print" it prints the commands I need to run. When "easy.send_command" is entered, the script will run every line except actually sending the command to the device. 

 

At this point in the script I've already run several commands with the same syntax, so I know that send_command is working, just not sure why it's not working here.

Is there something I'm missing?

Re: netmri_easy not running inside of python if statement

Expert
Posts: 18
10241     0
If I'd had to guess it is the '/' that is messing it up.  Try:
 
easy.send_command("delete \/force flash:" + result.group(0))
 
or
 
easy.send_command("delete \\/force flash:" + result.group(0))
 
 
-Lon.

Re: netmri_easy not running inside of python if statement

Expert
Posts: 18
10241     0

My other thought is that are you importing NetMRIEasy and creating the NetMRI context?  Since you didn't post all of your code, I assumed that you were doing this in the code previous to what you had shown.  Before you call NetMRIEasy, you will need to do the following:

 

from netmri_easy import NetMRIEasy

# This values will be provided by NetMRI before execution
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
}

# Create NetMRI context manager. It will close session after execution
with NetMRIEasy(**defaults) as easy:

        # put rest of code here that calls NetMRIEasy
        # .
        # .
        # .

Re: netmri_easy not running inside of python if statement

[ Edited ]
New Member
Posts: 5
10241     0

Hello LThompson,

Thanks for replying.


In regards to your first reply, I've tried it with and without the slash, and it didn't seem to make a difference. also I'm not really sure that would have done it, since with the print() command instead, it prints the proper lines every time.

 

In regards to the second one, by this point in the script I've already sent several commands (show ver, dir, etc.) so I know that netmri_easy is working, but I don't know why it won't work in that loop.

 

Also, I tried the last line without the tabs, like this:


print(Running_Code)
     removed = 0
     for line in Image_Files:
          result = re.search('(?<=00:00 )(.*?)(?=$)',line)
          if result:
               if result.group(0) != Running_Code:
               removed = removed + 1
easy.send_command("delete /force flash:" + result.group(0))

 

and it complains that it doesn't know what result.group is, so I know it will run without the tabs.

Perhaps it's something with the interpreter?

Re: netmri_easy not running inside of python if statement

Expert
Posts: 18
10241     0

Could you please post the entire script?  It would be good to see how you are arriving that the list of Image_Files.  Indentation is important in Python, be sure that you are not mixing space indents and tabs.  Add a print(line) to see what you are processing.

 

print(Running_Code)
     removed = 0
     for line in Image_Files:
          print(line)
          result = re.search('(?<=00:00 )(.*?)(?=$)',line)
          if result:
               if result.group(0) != Running_Code:
                   removed = removed + 1
                   easy.send_command("delete /force flash:" + result.group(0))

Re: netmri_easy not running inside of python if statement

New Member
Posts: 5
10241     0

Sure.

 

###########################################################################
## 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"
#Script-timeout:900
# 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
#}

 

#Author:Todd Vogt
#Automated Code Cleanup
#Version 4.1

import re, difflib, sys
import urllib
#from netmri_easy import NetMRIEasy

#easy = NetMRIEasy(**defaults)

 

#First, gather all the information we will need.
#-------------------------------------------------------------------------------
#Get a show version output to determine what code is currently running and where it is located

 

#ImageLoc = (easy.send_command('show version | include image'))
ImageLoc = ('System image file is "flash:c3560cx-universalk9-mz.152-6.E2.bin"')

 

#Get a list of the image files currently residing on the device
#Image_Files = ((easy.send_command("dir | include .bin")).readlines())
Image_Files = open("diri.txt", "r").readlines()

 

#-------------------------------------------------------------------------------
#Next, transform that data for our purposes.
#-------------------------------------------------------------------------------
#Find the location of the boot images
Code_Location = ((re.search('(?<=\")(.*?)(?=\Smiley Happy',(ImageLoc))).group(0))

#Find the version of the code currently running on the device
Running_Code = ((re.search('(?<=\Smiley Happy(.*?)(?=")',(ImageLoc))).group(0))

#Find how much space is left in the location above
Free_Space_Bytes = float(((re.search('(?<=\()(.*?)(?= b)',(Free_Space))).group(0)))

Image_Count = sum(1 for line in Image_Files)


#-------------------------------------------------------------------------------
#Next, find the old images (anything not currently running) and remove them
#-------------------------------------------------------------------------------
#How many old images are there?
if Image_Count == 1:
sys.exit("There's only one image! I'm not deleting anything!")

removed = 0
for line in Image_Files:
     result = re.search('(?<=00:00 )(.*?)(?=$)',line)
     if result:
          if result.group(0) != Running_Code:
               removed = removed + 1
               print("delete /force flash:" + result.group(0))

print("I deleted " + str(removed) + " old images! Enjoy the free space")

 

There's the script. You'll notice that some of the commands are commented out - that's because I have to use my laptop as a sandbox, because I can't get any good text editors on the customer's network. So instead of sending commands to the devices, I have the output from those commands in text files, which allows me to play with different outputs.

For example, here are the inputs I'm giving it:

 

Image_Files:

 

2 -rwx 21440512 Jul 01 2015 00:08:48 +00:00 c3560cx-universalk9-mz.152-4.E1.bin
5 -rwx 22802432 Aug 22 2017 17:50:25 +00:00 c3560cx-universalk9-mz.152-6.E2.bin
6 -rwx 22802432 Mar 30 2016 17:50:25 +00:00 c3560cx-universalk9-mz.151-2.E1.bin
7 -rwx 22802432 Sep 22 2018 17:50:25 +00:00 c3560cx-universalk9-mz.123-6.E2.bin

 

That's the same output that you would get from sending the command "dir | i .bin" - which is what I use when I'm actually running it.

 

Also - the handy text input box on this website won't seem to let me use tabs - so I've used spaces - please be assured that in all other cases the tabs are correct :-)

 

With this script running on my laptop, I get the following output:

 

delete /force flash:c3560cx-universalk9-mz.152-4.E1.bin
delete /force flash:c3560cx-universalk9-mz.151-2.E1.bin
delete /force flash:c3560cx-universalk9-mz.123-6.E2.bin
I deleted 3 old images! Enjoy the free space

 

When I remove the necessary comments and put it in NetMRI, it runs all the commands in the first section, but skips over the command to actually remove the files. What I'd like it to do is send the above delete commands to the device.

 

 

Re: netmri_easy not running inside of python if statement

New Member
Posts: 5
10241     0

Image result for john travolta gif

Re: netmri_easy not running inside of python if statement

Superuser
Posts: 115
10241     0

Is this correct or is it tabbed in?

 

#How many old images are there?
if Image_Count == 1:
sys.exit("There's only one image! I'm not deleting anything!")

or
#How many old images are there? if Image_Count == 1: sys.exit("There's only one image! I'm not deleting anything!")
Follow me on LinkedIn: https://www.linkedin.com/in/sifbaksh
Twitter: https://twitter.com/sifbaksh

https://sifbaksh.com

Re: netmri_easy not running inside of python if statement

Superuser
Posts: 115
10241     0

Also, check that NetMRI is reading that .txt file

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

https://sifbaksh.com

Re: netmri_easy not running inside of python if statement

New Member
Posts: 5
10241     0

Hey Sif,

It's tabbed correctly - I can verify by running the same code in python on my laptop.

However, netmri_easy doesn't exist on my laptop, so I have to use text files containing what the output would have been from each command. This way I can play with the outputs without waiting for the script to run inside of netmri. So to answer the question, netmri isn't reading any of those text files - those are there for me. Since I have to transfer this thing between computers every time I post on here, sometimes I forget to remove some of the comments. Apologies :-)

 

When it comes to the end, everything inside of that if loop works correctly - my ticker counts the amount of images that it finds, it's searching the strings properly, etc. The only thing that doesn't work is sending a command from inside of it.

 

I'm going to try something much simpler soon - just a basic loop containing a show command or something. That way I can be sure I'm not the one causing this.

Showing results for 
Search instead for 
Did you mean: 

Recommended for You