VanDyke Software Forums

VanDyke Software Forums (https://forums.vandyke.com/index.php)
-   Scripting (https://forums.vandyke.com/forumdisplay.php?f=14)
-   -   Python - ReadString Issues using Array (https://forums.vandyke.com/showthread.php?t=13608)

MattP 06-28-2019 08:43 PM

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()

bgagnon 07-01-2019 08:59 AM

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?

MattP 07-01-2019 01:52 PM

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()



All times are GMT -6. The time now is 07:47 AM.