Welcome to the VanDyke Software Forums

Join the discussion today!


Go Back   VanDyke Software Forums > Scripting

Notices

Reply
 
Thread Tools Rate Thread Display Modes
  #1  
Old 10-22-2013, 10:18 AM
he204035 he204035 is offline
Registered User
 
Join Date: Mar 2011
Posts: 8
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.
Reply With Quote
  #2  
Old 10-22-2013, 11:17 AM
bgagnon bgagnon is offline
VanDyke Technical Support
 
Join Date: Oct 2008
Posts: 4,636
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
Reply With Quote
  #3  
Old 10-22-2013, 11:39 AM
he204035 he204035 is offline
Registered User
 
Join Date: Mar 2011
Posts: 8
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?
Reply With Quote
  #4  
Old 10-22-2013, 12:19 PM
bgagnon bgagnon is offline
VanDyke Technical Support
 
Join Date: Oct 2008
Posts: 4,636
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
Reply With Quote
  #5  
Old 10-22-2013, 12:26 PM
he204035 he204035 is offline
Registered User
 
Join Date: Mar 2011
Posts: 8
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.
Reply With Quote
  #6  
Old 10-22-2013, 12:31 PM
bgagnon bgagnon is offline
VanDyke Technical Support
 
Join Date: Oct 2008
Posts: 4,636
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
Reply With Quote
  #7  
Old 10-22-2013, 12:39 PM
he204035 he204035 is offline
Registered User
 
Join Date: Mar 2011
Posts: 8
Talking

thanks so very much brenda!!

Last edited by he204035; 10-22-2013 at 12:46 PM. Reason: Smileys didnt show up
Reply With Quote
  #8  
Old 10-22-2013, 01:21 PM
he204035 he204035 is offline
Registered User
 
Join Date: Mar 2011
Posts: 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
is best for a TRUE or FALSE assessment. So would ReadString be better to be able to move forward based on more than just a specific output scenario?

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?
Reply With Quote
  #9  
Old 10-23-2013, 08:01 AM
bgagnon bgagnon is offline
VanDyke Technical Support
 
Join Date: Oct 2008
Posts: 4,636
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
Reply With Quote
  #10  
Old 10-19-2016, 07:04 AM
Rhudi's Avatar
Rhudi Rhudi is offline
Registered User
 
Join Date: Jul 2013
Location: Greenville, SC
Posts: 97
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
sSSHv2 is a constant with the ssh command I'm using:
Code:
Const sSSHv2    = "ssh v2 routing-instance TDNVPN admin@")
sNIDIP will be set before this code happens

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
This bit of code deals with the chance that my device may have one of two passwords. So, it will try both. These are constants, stored under sNIDPW1 & sNIDPW2.

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.
Reply With Quote
Reply

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 05:09 PM.