Python - ReadString Issues using Array
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.
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!
TESTOUTPUT = SendAndReturnLONG("show int l0")
SESSIONID = crt.GetScriptTab()
SESSIONID.Screen.Synchronous = True
SESSIONID.Screen.IgnoreEscape = True
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
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")
Is that what you meant?
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.
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 09:39 AM. Reason: wrap code in [code] blocks to preserve indentation
|array , python , readstring , scripting|
|Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)|
|Display Modes||Rate This Thread|