#1
|
|||
|
|||
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. |
#2
|
|||
|
|||
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 Last edited by rtb; 07-15-2013 at 09:58 AM. |
#3
|
|||
|
|||
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?
|
#4
|
|||
|
|||
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).aspxYou 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? |
#5
|
|||
|
|||
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.
|
#6
|
|||
|
|||
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. |
![]() |
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
Thread Tools | |
Display Modes | Rate This Thread |
|
|