#1
|
|||
|
|||
Using "If...Then" with crt.Screen.Send and WaitForString
I need a script that will
1. Open a text file 2. Read each line of the text file 3. For each line in the text file, execute a command using the data in that line 4. Wait for the output of the executed command 5. If the out put looks like "XYZ", do another command...but if it looks like anything other than XYZ, continue on to the next line in the text file. I have items 1-4 knocked out no problem. But, using "If..Then" in conjunction with "crt.Screen.WaitForString" is proving elusive. |
#2
|
|||
|
|||
Hello he204035,
Are you using VBScript? How you are doing #4 (and possibly 1, 2 and 3) is going to be important to how #5 is done. ![]() Are you "waiting for strings" (multiple) and XYZ might be one of those possible strings? Or are you using ReadString()? With the limited information available I would expect you might use ReadString(), then check if the data you are looking for is found in that variable (with InStr) and if so, perform the If statement. If you only want to act on that particular scenario, all you need is the If portion of the If/Else construct, assuming you are inside some loop that will then return control to reading the next line of the text file (ie: the For/Next from #3). Code:
strResult = crt.Screen.ReadString("til_end_of_command_output", 5) If InStr(strResult, "XYZ") Then ... "Do Another Command"
__________________
Thanks, --Brenda VanDyke Software Technical Support support@vandyke.com (505) 332-5730 |
#3
|
|||
|
|||
Here's the code i am trying:
Code:
Const ForReading = 1 Const ForWriting = 2 Sub Main Dim fso, file, str Set fso = CreateObject("Scripting.FileSystemObject") ' Note: A runtime exception will be generated if 'input.txt' doesn't exist. ' Set file = fso.OpenTextFile("C:\Users\Documents\Users.txt", ForReading, False) crt.Screen.Synchronous = True Do While file.AtEndOfStream <> True str = file.Readline crt.Screen.Send "rtrv-user-secu::" & str & ";" If crt.WaitForString "OOS-AU" Then crt.Screen.Send "dlt-user-secu::" & str & ";" Loop crt.Screen.Synchronous = False End Sub If I use the ReadString, is the text in quotes the syntax that i want to be looking for? Is there a timer on that? |
#4
|
|||
|
|||
Hello he204035,
I believe ReadString() or WaitForString() would work in this instance, since it is apparently the remote's prompt that determines whether the If statement is executed. However, you need some qualifier in the current If statement: Code:
crt.Screen.Send "rtrv-user-secu::" & str & ";" If crt.Screen.WaitForString("OOS-AU", 5) = True Then crt.Screen.Send "dlt-user-secu::" & str & ";" Yes, ReadString() also can take a timeout parameter. See the SecureCRT Help topic Scripting / Script Objects Reference / Screen Object for details on ReadString().
__________________
Thanks, --Brenda VanDyke Software Technical Support support@vandyke.com (505) 332-5730 |
#5
|
|||
|
|||
Brenda,
Thanks so much for the info. One more question, if that is ok. Is there anyway to spawn more than 1 execution after THEN if "OOS-AU" is matched? This script is being used to retrieve user accounts on a node, and if the user account shows OOS-AU, then it will be DELETED. However, turns out the node requires one additional command be issued before the DELETE command. |
#6
|
|||
|
|||
Hi he204035
Oh sure, then you would use the block style from your original post: Code:
crt.Screen.Send "rtrv-user-secu::" & str & ";" If crt.Screen.WaitForString("OOS-AU", 5) = True Then crt.Screen.Send "dlt-user-secu::" & str & ";" & vbcr crt.Screen.WaitForString "prompt" crt.Screen.Send "second command" & vbcr ' ... etc. ... End If
__________________
Thanks, --Brenda VanDyke Software Technical Support support@vandyke.com (505) 332-5730 |
#7
|
|||
|
|||
![]()
thanks so very much brenda!!
Last edited by he204035; 10-22-2013 at 12:46 PM. Reason: Smileys didnt show up |
#8
|
|||
|
|||
It seems like the
Code:
If crt.Screen.WaitForString("OOS-AU", 2) = True Then crt.Screen.Send "ed-user-secu::" & str & ":::::oos;" crt.Screen.WaitForString "COMPLD", 5 crt.Screen.WaitForString ";", 5 crt.Screen.Send "dlt-user-secu::" & str & ";" ' ... etc. ... End If So instead of just looking for "OOS-AU" = yes or no, ReadString could be used to find "OOS-AU", "IS", "IS-EXP", or "User Does Not Exist" etc? |
#9
|
|||
|
|||
Hello he204035,
OK, so apparently that is not the prompt displayed after output. ![]() Yes, you can use ReadString() with the MatchIndex property to take certain actions depending on the text found. There are excellent examples of this in SecureCRT's Help file.
__________________
Thanks, --Brenda VanDyke Software Technical Support support@vandyke.com (505) 332-5730 |
#10
|
||||
|
||||
Sending text based on different conditions
I know this is an old thread, but I'm hoping someone may find my method useful.
I have a particular environment, where I'm logging on to a device through SSH. That device may only prompt for a password, but may prompt for a security key acceptance. Here's a snip from code I use to do that. Code:
vShellPrompts = Array(_ sRouterPrompt & " ", _ "admin@" & sNIDIP & "'s password: ", _ "(yes/no)? ") ' objTab.Screen.Send vbCr Do Select Case objTab.Screen.WaitForStrings(vShellPrompts, szTimeout) Case 0 ' NID non-responsive crt.Dialog.MessageBox "NO RESPONSE (" & szTimeout & " seconds)" sChangeOK = sChangeOK & vbCrLf & "N: " & sNIDIP Case 1 ' We got the router prompt, try to logon to NID objTab.Screen.Send sSSHv2 & sNIDIP & vbCr Case 2 ' We got a NID password prompt bNeedPW = True Exit Do ' Ready to Send PW Case 3 ' Host Key prompt objTab.Screen.Send "yes" & vbCr End Select Loop Code:
Const sSSHv2 = "ssh v2 routing-instance TDNVPN admin@") The next section is this: Code:
If bNeedPW Then vShellPrompts = Array( _ sRouterPrompt & " ", _ ": ", _ "Permission denied (publickey,password).") Do objTab.Screen.Send sNIDPW & vbCr Select Case objTab.Screen.WaitForStrings(vShellPrompts, szTimeout) Case 0 crt.Dialog.MessageBox "NO RESPONSE (" & szTimeout & " seconds)" sChangeOK = sChangeOK & vbCrLf & "N: " & sNIDIP Case 1 ' Got back Router Prompt - BAD sChangeOK = sChangeOK & vbCrLf & "N: " & sNIDIP Case 2 ' This could be a PW Prompt or a Router prompt sNIDPrompt = Trim(objTab.Screen.Get(objTab.Screen.CurrentRow, 1, objTab.Screen.CurrentRow, 40)) Select Case Right(sNIDPrompt, 12) Case "'s password:" ' This is a PW Prompt sNIDPW = sNIDPW2 nTryCount = nTryCount + 1 Case Else' We are most likely in. bLogonOK = True Exit Do End Select Case 3 ' Kicked from NID? sChangeOK = sChangeOK & vbCrLf & "N: " & sNIDIP End Select If nTryCount > 1 Then crt.Dialog.MessageBox "UNABLE TO AUTO LOGON." sChangeOK = sChangeOK & vbCrLf & "N: " & sNIDIP End If Loop End If There may be a more elegant way to do this, but this works well. Any time you may have different strings in your response, just include the appropriate text in the array and add the relevant Case statement. |
![]() |
Thread Tools | |
Display Modes | Rate This Thread |
|
|