View Single Post
  #2  
Old 10-11-2016, 11:07 AM
jdev's Avatar
jdev jdev is offline
VanDyke Technical Support
 
Join Date: Nov 2003
Location: Albuquerque, NM
Posts: 950
If you're trying to simply exit the script when you reach that part of your code, you're inside of a Sub Main(), so just use the Exit Sub statement all on its own. For example:
Code:
                If nResult = 0 Then
                    MsgBox "Timeout reached, equipment unreachable or an error has occured"
                    crt.Screen.Send Chr(3) & chr(13)
                    Exit Sub
                Else
                    crt.Screen.WaitForString "Password: "
                    ' ...
For making your script-authoring life easier in the long run, consider that currently you're using a Select Case that is triggering on the result of WaitForStrings, which is a number representing the index of the string that it found.

Rather than triggering on this index, you can trigger on the string itself (so that if you have to change/rearrange any of the strings in the call, you won't have to edit more than just that one).

Also, if you make use of appropriately-named variables, you can have an easier-to-read-and-maintain script, since magic numbers like "1", "2", "3", etc. won't leave you wondering, "Now, what does '5' mean again?..." Here's an example:
Code:
Sub Main()
    ' ...
    ' ... read in your value from the clipboard *before* you
    ' Send() the ssh command. That way you can simplify the
    ' remainder of the cleanup in your script, and you won't
    ' have to do this again when you try telnet later on.
    '   Hint: Check the Scripting Guide for a tip on how to
    '   validate what's in the clipbard as being an IP address,
    '   specifically, the diatribe dealing with 'Regular Expressions'
    '   beginning on page 73 and extending through page 76...
    '   Scripting guide is here:
    '      https://www.vandyke.com/support/tips/scripting/index.html
    ' ...
    
    ' Define variables for strings we'll be waiting for, then put
    ' them into an array. This way, we'll only have to define/declare
    ' them once and if we ever have to edit them later on, we'll only
    ' need to edit the string in one place instead of two (or more).
    strPasswordPrompt = "assword: "
    strNewHostKeyPrompt = "Are you sure you want to continue connecting (yes/no)?"
    strConnectionRefused = "onnection refused"
    strUnknownAddress = "no address associated with name"
    strSyntaxError = "syntax error"
    strControlC = "^C"
    
    ' Now, let's put all these strings into an array that we'll use
    ' when calling crt.Screen.WaitForStrings()...
    vWaitFors = Array(_
        strPasswordPrompt, _
        strNewHostKeyPrompt, _
        strConnectionRefused, _
        strUnknownAddress, _
        strSyntaxError, _
        strControlC)

    ' Wait for any of the above to be found. Note that we're wrapping
    ' the "crt.Screen.WaitForStrings() call in an __If__ statement -- this
    ' can be done if you're using a timeout because Screen.WaitForStrings()
    ' will always return 0/False if it times out before finding any of the
    ' elements of the array you pass in.
    If crt.screen.WaitForStrings(vWaitFors, 5) Then
        strWhatWasFound = vWaitFors(crt.Screen.MatchIndex - 1)
        ' Let's determine what we found, and do stuff about it...
        Select Case strWhatWasFound
            Case strPasswordPrompt
                ' Handle the [pP]assword: prompt
                ' ...
            
            Case strNewHostKeyPrompt
                ' Handle the new host key ssh prompt
                ' ...
                
            Case strConnectionRefused
                ' Handle the case where the connection is refused.
                ' ...
                
            Case strUnknownAddress
                ' Handle this case.
                ' ...
                
            Case strSyntaxError
                ' Handle this case.
                ' ...
                
            Case strControlC
                ' Handle this case.
                ' ...
            
            Case Else
                crt.Dialog.MessageBox(_
                    "Whoa!" & vbcrlf & vbcrlf & _
                    "You've something in your WaitFors array doesn't have " & _
                    "'Case handler' code to deal with: " & vbcrlf & vbcrlf & _
                    vbtab & strWhatWasFound & vbcrlf & vbcrlf & _
                    "Time to work on your script code some more and handle " & _
                    "this new thing you added to your vWaitFors array...")
        End Select
    Else
        ' This is where you handle your timeout case, because
        ' WaitForStrings returns False (zero) if it times out.
        crt.Screen.Send Chr(3) & chr(13)
        crt.Screen.WaitForString "$"
        crt.Screen.Send "telnet "
        ' ...
    End If

    ' ...
End Sub
__________________
Jake Devenport
VanDyke Software
Technical Support
YouTube Channel: https://www.youtube.com/vandykesoftware
Email: support@vandyke.com
Web: https://www.vandyke.com/support
Reply With Quote