Welcome to the VanDyke Software Forums

Join the discussion today!


Go Back   VanDyke Software Forums > Scripting

Reply
 
Thread Tools Rate Thread Display Modes
  #1  
Old 06-28-2019, 07:43 PM
MattP MattP is offline
Registered User
 
Join Date: Jun 2019
Posts: 2
Python - ReadString Issues using Array

Hello Everyone,

I have been scripting via SecureCRT for a while, but I've run into an issue that I believe may be a bug.
I was trying to rework my 'SendAndReturn', which just sends a command and returns a two dimensional array. Currently the problem is that on commands with extremely long outputs (i.e. ARP cache on a router that feeds ~15000 cable modems) the ReadString function locks up pretty aggressively for 10/15 minutes.

My work around for this was to process the data one line at a time, then append it into a text variable. This was able to accomplish what I need, it runs through the command properly without stalling, however it doesn't appear that the list function on ReadString is working appropriately. When I debugged, it seemed that it was processing the \r\n properly but not the #.
I attempted to replace it with the full name of the chassis to see what out happen, and it still didn't catch. When I hit 'enter' it detected the CR and saw the #, so it gave me the dialog box, but it removed the name of the chassis from the output. I.E.
<commandoutput>
ROUTERNAME#
became simply "#" on that line in the dialog output.

When I run it exactly as below, it will not even end when I hit Enter and generate a new \r\n and feed it a line containing the "#", it does not find it - i believe because it's removing the # from the string, so it is unable to find it.
I'm hoping that someone can give me some input on this. I'm currently on 8.1.0 x64 build 1294. If there is a newer version I won't be able to update due to my organizations IT policies - until they do they entire organization, so I'm hoping to find a work-around regardless. Any help would be greatly appreciated!


def Main():
TESTOUTPUT = SendAndReturnLONG("show int l0")
crt.Dialog.MessageBox(TESTOUTPUT)


def SendAndReturnLONG(COMMANDTEXT,SESSIONID="DEFAULT"):
if SESSIONID=="DEFAULT":
SESSIONID = crt.GetScriptTab()
SESSIONID.Screen.Synchronous = True
SESSIONID.Screen.IgnoreEscape = True
SESSIONID.Activate
SESSIONID.Screen.Send(COMMANDTEXT + "\n")
OUTPUTSTRING = ""
Response = ""
LoopBreak = 0
while LoopBreak == 0:
Response = SESSIONID.Screen.ReadString(["\r\n","#"])
#crt.Dialog.MessageBox("hit a CR")
OUTPUTSTRING = OUTPUTSTRING+"\r\n"+Response
if OUTPUTSTRING[-1:] == "#":
LoopBreak = 1
Response = None
SESSIONID = None
return OUTPUTSTRING
Main()
Reply With Quote
  #2  
Old 07-01-2019, 07:59 AM
bgagnon bgagnon is offline
VanDyke Technical Support
 
Join Date: Oct 2008
Posts: 3,946
Hi MattP,

I am not sure the bug you are reporting. First, one thing I noticed, this should be a \r to simulate pressing ENTER:

SESSIONID.Screen.Send(COMMANDTEXT + "\r")


Quote:
When I run it exactly as below, it will not even end when I hit Enter and generate a new \r\n and feed it a line containing the "#", it does not find it - i believe because it's removing the # from the string, so it is unable to find it.
The ReadString() function reads until <string(s) specified>, so it's not going to include the # character in the variable, if that's what you were expecting. So you would appear to be in an endless loop inside the while statement.

Is that what you meant?
__________________
Thanks,
--Brenda

VanDyke Software
Technical Support
support@vandyke.com
(505) 332-5730
Reply With Quote
  #3  
Old 07-01-2019, 12:52 PM
MattP MattP is offline
Registered User
 
Join Date: Jun 2019
Posts: 2
Resolved

Hey bgagnon,

Yep, you were correct.. I had a logic failure around including the # in the captured text. Thanks for your help with that! Last week was a very long week, haha.

Here is the code working to pull back the string, for anyone who may be interested, corrected for this logic.
This runs much faster than trying to use just ReadString("#") to capture very long output to a string.

Code:
def Main():
	TESTOUTPUT = SendAndReturnLONG("show int l0")
	crt.Dialog.MessageBox(TESTOUTPUT)


def SendAndReturnLONG(COMMANDTEXT,SESSIONID="DEFAULT"):
	if SESSIONID=="DEFAULT":
		SESSIONID = crt.GetScriptTab()
	SESSIONID.Screen.Synchronous = True
	SESSIONID.Screen.IgnoreEscape = True
	SESSIONID.Activate
	SESSIONID.Screen.Send(COMMANDTEXT + "\n")
	OUTPUTSTRING = ""
	Response = ""
	while 1:
		Response = SESSIONID.Screen.ReadString([COMMANDTEXT + "\r\n","\r\n","CHNDCAPC02"])
		if SESSIONID.Screen.MatchIndex == 1:
			pass #I don't want the first line of output with the command I issued, so pass
		elif SESSIONID.Screen.MatchIndex == 2:
			OUTPUTSTRING = OUTPUTSTRING+"\r\n"+Response #Append the current line to the total output, re-adding the CR
		elif SESSIONID.Screen.MatchIndex == 3:
			break #end the loop.  I also don't want the last line with the TID of the chassis, so don't add it.
	Response = None
	SESSIONID = None
	return OUTPUTSTRING
	OUTPUTSTRING = None
Main()

Last edited by jdev; 07-08-2019 at 08:39 AM. Reason: wrap code in [code] blocks to preserve indentation
Reply With Quote
Reply

Tags
array , python , readstring , scripting


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -6. The time now is 07:13 PM.