#1
|
|||
|
|||
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() 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 |
#2
|
||||
|
||||
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] |
#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! |
#4
|
|||
|
|||
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! |
#5
|
||||
|
||||
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] |
![]() |
Tags |
cisco , readstring , variables , waitforstring |
Thread Tools | |
Display Modes | Rate This Thread |
|
|