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 05-10-2013, 11:15 AM
rdrovdahl rdrovdahl is offline
Registered User
 
Join Date: May 2013
Posts: 3
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!

Last edited by miked; 05-10-2013 at 11:29 AM. Reason: Put code brackets around the Python code to keep the correct formatting
Reply With Quote
  #2  
Old 05-10-2013, 12:18 PM
miked's Avatar
miked miked is offline
Registered User
 
Join Date: Feb 2004
Posts: 2,040
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 + "#"
__________________
Mike
VanDyke Software
Technical Support
[http://www.vandyke.com/support]
Reply With Quote
  #3  
Old 05-10-2013, 01:10 PM
rdrovdahl rdrovdahl is offline
Registered User
 
Join Date: May 2013
Posts: 3
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!
Reply With Quote
  #4  
Old 05-10-2013, 01:28 PM
rdrovdahl rdrovdahl is offline
Registered User
 
Join Date: May 2013
Posts: 3
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!
Reply With Quote
  #5  
Old 05-10-2013, 01:58 PM
miked's Avatar
miked miked is offline
Registered User
 
Join Date: Feb 2004
Posts: 2,040
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 + "\"")
__________________
Mike
VanDyke Software
Technical Support
[http://www.vandyke.com/support]
Reply With Quote
Reply

Tags
cisco , readstring , variables , waitforstring


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 12:32 PM.