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 07-12-2013, 05:35 PM
drkwood drkwood is offline
Registered User
 
Join Date: May 2013
Posts: 6
Need to detect and handle remote end disconnects

I am working on a script to login to a number of devices and do specific work after connecting. I am randomly running into a case where the device I am logged into decides to disconnect the SSH session on me, or possibly a firewall in between decides to reset the connection. This currently means that my script hard stops and shows a message box on the screen indicating connection disconnect.

I need to "catch" this error, and jump to a specific location in my script. A simplified version of my script is below. What I am looking for is a way to:
(a) detect the remote-end disconnect,
(b) set a variable to record where in the script I was,
(c) update a variable to track the number of times it has happened within this particular FOR loop entry, and
(d) resume from the top of the FOR loop, maintaining my current position

Any ideas? Thanks in advance for any help.

Code:
Sub Main()
For Each strHostIP In g_vHostIP1
    If AttemptConnection(g_vHostIP1(g_nCounterHost)) = True Then
        g_szCurrentHostIP = g_vHostIP1(g_nCounterHost)
        nCounterHostIPCurrent = 1
    ElseIf AttemptConnection(g_vHostIP2(g_nCounterHost)) = True Then
        g_szCurrentHostIP = g_vHostIP2(g_nCounterHost)
        nCounterHostIPCurrent = 2
    ElseIf AttemptConnection(g_vHostIP3(g_nCounterHost)) = True Then
        g_szCurrentHostIP = g_vHostIP3(g_nCounterHost)
        nCounterHostIPCurrent = 3
    Else
        'Write to Results File Indicating Timeout
        nCounterNoConnection = nCounterNoConnection + 1
        WriteToCSV("Connection Timeout")
        strHostSkip = "Yes"
        nCounterHostIPCurrent = 0
    End If

    If strHostSkip <> "Yes" Then
        g_objNewTab.Screen.Synchronous = True
        nCounterLoginAttempt = 0
        Do
            nCounterLoop = nCounterLoop + 1
            Select Case AttemptMainAuth(vUsernames(nCounterLoginAttempt),vPasswords(nCounterLoginAttempt))
                Case "Username Sent"
                Case "Password Sent"
                Case "Auth Timeout"
                    'Reconnect and Retry with current set of credentials (up to 3 times)
                Case "Auth Failed"
                    'Increment nCounterLoginAttempt to move to next set of credentials
                    'If at last set of credentials, switch to HostIP 2 or 3 (if applicable) and retry from 0
                Case "Possible Success"
                    If VerifyLogin() = True Then
                        'Do work
                    End If
                Case Else
                    'Unhandled Exception
            End Select
        Loop Until nCounterLoop > X
        If nCounterLoop > X Then
            'Unhandled Exception
        End If
    End If
Next

'Disconnect
If crt.Session.Connected Then crt.Session.Disconnect

'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Function AttemptConnection(strHostIP)

Dim nCounterWait, nError, strErr

    'See if the function was passed anything
    If strHostIP = "" Then Exit Function

    ' Make sure we are disconnected before attempting a connection
    If crt.Session.Connected Then crt.Session.Disconnect

    ' Instruct the script host we want to handle errors ourselves
    On Error Resume Next

    'Connect to Host
    Set g_objNewTab = crt.Session.ConnectInTab("/SSH2 /ACCEPTHOSTKEYS " & strHostIP, False)

    ' Capture error code and description (if any)
    nError = Err.Number
    strErr = Err.Description

    ' Now, tell the script host that it should handle errors as usual now
    On Error Goto 0

    'Attempt to wait for connection to complete
    nCounterWait = 0
    Do
        nCounterWait = nCounterWait + 1
        If crt.Session.Connected = True Then
            AttemptConnection = True
            Exit Function
        End If
        If nCounterWait > 10 Then
            Exit Function
        End If 
        crt.sleep 100
    Loop

End Function

'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Function AttemptMainAuth(strUsername,strPassword)

Dim nAuthTimeout
nAuthTimeout = 10

vPossibleShellPrompts = Array(_
    "name:", _
    "sword:", _
    "ogin incorrect", _
    "uthentication failed", _
    ">")

    g_objNewTab.Screen.WaitForStrings vPossibleShellPrompts, nAuthTimeout
    Select Case g_objNewTab.Screen.MatchIndex
        Case 0
            AttemptMainAuth = "Auth Timeout"

        Case 1 ' "name:"
            crt.Screen.Send strUsername & vbcr
            AttemptMainAuth = "Username Sent"

        Case 2 ' "sword:"
            crt.Screen.Send strPassword & vbcr
            AttemptMainAuth = "Password Sent"

        Case 3,4 ' "ogin incorrect", "uthentication failed"
            AttemptMainAuth = "Auth Failed"
            
        Case 5 ' ">" <-- Shell prompt means auth success
            AttemptMainAuth = "Possible Success"

        Case Else
            AttemptMainAuth = "Unhandled Exception"
        End Select

End Function

Last edited by rtb; 07-15-2013 at 08:43 AM.
Reply With Quote
  #2  
Old 07-15-2013, 09:12 AM
rtb rtb is offline
VanDyke Technical Support
 
Join Date: Aug 2008
Posts: 4,306
Hi drkwood,

My suggestion would be to use VBScript error handling. Here is an MSDN page as of the date of this post that has information about the subject (Microsoft could change the location in the future):
http://msdn.microsoft.com/en-us/libr...(v=vs.85).aspx
__________________
--Todd

VanDyke Software
Technical Support
support@vandyke.com
505-332-5730

Last edited by rtb; 07-15-2013 at 09:58 AM.
Reply With Quote
  #3  
Old 07-16-2013, 12:13 PM
drkwood drkwood is offline
Registered User
 
Join Date: May 2013
Posts: 6
What I really need was the "OnError Goto X" command, but that does not appear to be available. If I chose to re-write my script in C or another language that SecureCRT will accept, would I have more error handling options available to me?
Reply With Quote
  #4  
Old 07-16-2013, 12:52 PM
rtb rtb is offline
VanDyke Technical Support
 
Join Date: Aug 2008
Posts: 4,306
Hi drkwood,

SecureCRT 7.1.x supports Python natively, and ActiveX script engines like WScript (VBScript, JScript). It does not support C. The error handling available to you would be what is provided by the language you use to write your script.

Here is a better VBScript example and explanation on MSDN (location is subject to change):
http://msdn.microsoft.com/en-us/libr...(v=vs.84).aspx
You can enable error handling, capture the error in variables, disable error handling and finally take action based on the error that is captured if any error did occur.

Does this help answer your question?
__________________
--Todd

VanDyke Software
Technical Support
support@vandyke.com
505-332-5730
Reply With Quote
  #5  
Old 07-16-2013, 02:38 PM
drkwood drkwood is offline
Registered User
 
Join Date: May 2013
Posts: 6
Possibly...

The way that I incorporated that currently was to execute "Err.Clear()" at the beginning of the Do loop and then check if Err.Number <> 0 at the end, which I think will cover me in most cases. I'm trying to determine how to modify the structure of the do loop so that I can check for an error after every configuration routine and resume at the same place if I detect than an error has occurred and am forced to reconnect.
Reply With Quote
  #6  
Old 07-16-2013, 04:50 PM
rtb rtb is offline
VanDyke Technical Support
 
Join Date: Aug 2008
Posts: 4,306
Hi drkwood,

Thanks for the update. I am glad that the information may have helped. Please post the current iteration of your script if you have a question about the Do...Loop.
__________________
--Todd

VanDyke Software
Technical Support
support@vandyke.com
505-332-5730
Reply With Quote
Reply


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 07:46 PM.