Introducing SOC Insights for BloxOne Threat Defense: Boost your SOC efficiency with AI-driven insights to eliminate manual work and accelerate investigation and response times. Read the blog announcement here.

Automation Scripts

Reply

Python Script abort while downloading file from FTP to Switch

New Member
Posts: 6
4594     0

Hello all,

 

I'm trying to download a new IOSXE on a C9200 from FTP server with a NetMRI Python Script.

Unfortunately I'm running into a timout while the IOS is copied.

 

My copy line looks like that:

easy.send_command("copy ftp://{}{} flash:".format(server, newIOS))

The execution of the command is successfull, but the copy process is too long.

 

I tried to set my script to sleep for 6 minutes after the copy line with time.sleep(360).

Also I tried the send_async_command:

easy.send_async_command("copy ftp://{}{} flash:".format(server, newIOS), 360)

This got me a new error. The send_async_command needs a further argument. 

 

The Script-Timeout is set to 3600.

 

Nothing of that fixed my problem.

Do you guys have an idea how to fix it?

 

Any help would be much appreciated!

Re: Python Script abort while downloading file from FTP to Switch

Superuser
Posts: 115
4594     0

Can you post the entire script?  Easier to troubleshoot

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

https://sifbaksh.com

Re: Python Script abort while downloading file from FTP to Switch

New Member
Posts: 6
4594     0

Thanks for your reply.

This is my script until now.

 

# BEGIN-SCRIPT-BLOCK
# Script-Timeout: 3600
# Script-Filter:
#	$Vendor eq "Cisco" and $sysDescr like /IOSXE/
#Script-Variables:
#	$serverPath string "Path"
# 	$newIOS string "IOS"
#	$checkSize boolean
# 	$fileSize string "file size"
#	$checkMD5 boolean
#	$md5 string "MD5"
# END-SCRIPT-BLOCK

from netmri_easy import NetMRIEasy
import time
import sys

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
}

with NetMRIEasy(**defaults) as easy:
	ver = easy.send_command("show version")
  
	if "C9200" in ver:
		flashContent = easy.send_command("dir | i {}".format(newIOS))
	
		if newIOS in flashContent:
			if checkSize:
				if fileSize in flashContent:
					print("size okay")
				else:
					print('size not okay', file=sys.stderr)
					sys.exit()

			if checkMD5:
				verifyMD5 = easy.send_command("verify /md5 flash:{} {}".format(newIOS, md5))
				if "Verified" in verifyMD5:
					print("md5 okay")
				else:
					print('md5 not okay', file=sys.stderr)
					sys.exit()

			easy.send_command("conf t")
			easy.send_command("no boot system")
			easy.send_command("boot system flash:{}".format(newIOS))
			easy.send_command("end")
			easy.send_command("wr")

		else:
			easy.send_command("conf t")
			easy.send_command("file prompt quiet")
			easy.send_command("end")
			easy.send_command("copy ftp://{}{} flash:".format(serverPath, newIOS))
			easy.send_command("conf t")
			easy.send_command("file prompt alert")
			easy.send_command("end")
			easy.send_command("wr")

			if checkSize:
				if fileSize in flashContent:
					print("size okay")
				else:
					print('size not okay', file=sys.stderr)
					sys.exit()

			if checkMD5:
				verifyMD5 = easy.send_command("verify /md5 flash:{} {}".format(newIOS, md5))
				if "Verified" in verifyMD5:
					print("md5 okay")
				else:
					print('md5 not okay', file=sys.stderr)
					sys.exit()

			easy.send_command("conf t")
			easy.send_command("no boot system")
			easy.send_command("boot system flash:{}".format(newIOS))
			easy.send_command("end")
			easy.send_command("wr")

	else:
		print('wrong switch model', file=sys.stderr)
		sys.exit()

Re: Python Script abort while downloading file from FTP to Switch

Superuser
Posts: 115
4594     0

Your code seems to be fine, what version of NetMRI are you running?

I know we did this issue, I would open a support case for this and Ping me on our Slack Channel 

Go here if you are not a member

https://github.com/infobloxopen/netmri-toolkit

 

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

https://sifbaksh.com

Re: Python Script abort while downloading file from FTP to Switch

New Member
Posts: 6
4594     0

We are running NetMRI version 7.4.1.95749

Today I will update to 7.4.2.97106

 

I joined the Channel.

Re: Python Script abort while downloading file from FTP to Switch

New Member
Posts: 6
4594     0

Support told me to use the send_async_command and add a regex pattern to match the next prompt.

 

The command syntax is:
send_async_command(command, timeout, regex)

 

In my case:

send_async_command("copy ftp://{}{} flash:".format(server, newIOS), 360, ".*#")

Showing results for 
Search instead for 
Did you mean: 

Recommended for You