VanDyke Software Forums

VanDyke Software Forums (https://forums.vandyke.com/index.php)
-   Scripting (https://forums.vandyke.com/forumdisplay.php?f=14)
-   -   Anyway to use variables in ReadString or WaitForString? (https://forums.vandyke.com/showthread.php?t=11018)

rdrovdahl 05-10-2013 11:15 AM

Anyway to use variables in ReadString or WaitForString?
 
I've set up a function that logs into Cisco devices supplied via text file and runs commands also supplied from a txt file.

I need to capture all the outputs from the commands being run to a log file and need to be very specific about the ReadString and WaitForString entries every time the function is being run.

I'm trying to use a variable in as follows.

Code:

def commands(currentdevice):
    crt.Screen.Synchronous = True
    filep = open(LOGFILE, "ab+")
    for line in open(COMMANDFILE, "r"):
                crt.Screen.Send(line + '\r')
                data = crt.Screen.ReadString(currentdevice + "#")
                filep.write(data + "\n")
                crt.Screen.WaitForString(currentdevice + "#")
    return()

So far, I've not been able to find a way to use a variable in the ReadString or WaitForString commands. Is there a way to do this or maybe some other method that would accomplish the same thing?

Thanks!

miked 05-10-2013 12:18 PM

Greetings rdrovdahl,

Yes, you can use variables with ReadString and WaitForString.

Here's a python example script that does something similar to what I think you're wanting to do. Lines 378-404 (with comments) demonstrate using Send, WaitForString, and ReadString.
Code:

            for strCommand in vCommandsList:
                # Get date and time for use in log file naming
                strLogFilename = GetSafeFilename(strCommand)
                if not os.path.isdir(g_strPath + "ScriptOutput/"):
                    os.makedirs(g_strPath + "ScriptOutput/")
                strLogFilePath = (g_strPath + "ScriptOutput/" +
                    g_strHost + "--" + strLogFilename + ".txt")
                try:
                    objLogFile = open(strLogFilePath,"w")
                # SecureCRT on Mac uses Python 2.5, so use old syntax.
                # Python 2.7 syntax:
                # except Exception as strError:
                except Exception, strError:
                    MsgBox(
                        "Could not open log file with error: " + str(strError))
                    break
                   
                # We only continue if we are able to open the results file.
                # Send the command to the remote
                g_objNewTab.Screen.Send(strCommand + "\r")
                # Wait for the command to be echo'd back to us.
                g_objNewTab.Screen.WaitForString(strCommand + "\r\n")
                # As mentioned earlier in comments above, if you want to
                # suppress escape sequences from being captured, set the
                # Screen.IgnoreEscape property = True.
                g_objNewTab.Screen.IgnoreEscape = True
                strResult = g_objNewTab.Screen.ReadString(strPrompt, 5)


Have you tried assigning currentdevice + "#" to a new variable (and using that variable instead of trying to combine the variable and string inside the argument list, like the following line shows?
Code:

    strPrompt = currentdevice + "#"

rdrovdahl 05-10-2013 01:10 PM

Thanks for the reply!

Great to know that variables can be used with these commands. I took your suggestion and made a new variable named strPrompt as follows:

strPrompt = currentdevice + "#"


I then used the strPrompt variable with the WaitForString command:

crt.Screen.WaitForString(strPrompt)


The script was still not recognizing the strPrompt so I put in the following as a test to see what strPrompt was being defined as.

crt.Screen.Send("! strPrompt = " + strPrompt)


Here's the output in SecureCRT:

router1#! strPrompt = router1
router1##



What's interesting is that there is a carriage return between 'currentdevice' variable and '#'. I'm guessing this is what has been giving me trouble all along. Do you know how to reformat a variable to strip out the carriage return?

Thanks again!

rdrovdahl 05-10-2013 01:28 PM

Solved
 
Found the solution using the strip command and all is working perfectly now.

strPrompt = currentdevice.strip() + "#"

This stripped off the carriage return from the 'currentdevice' variable and now everything is working perfectly.

Thanks again for the assistance!

miked 05-10-2013 01:58 PM

Excellent - thanks for posting the solution!

I was going to suggest using a MessageBox to make sure we knew exactly what the variable was set to, because a MessageBox seemed clearer than using Send. In case it helps for future debugging, here's a way to message box your variables.
crt.Dialog.MessageBox("\"" + strPrompt + "\"")


All times are GMT -6. The time now is 11:08 PM.