View Single Post
  #1  
Old 05-01-2013, 08:23 PM
drkwood drkwood is offline
Registered User
 
Join Date: May 2013
Posts: 6
Auto-Cancel SSH connection on bad credentials

I am required to login to a large number of units via SSH, make template changes, update the login credentials, and record several parameters to various files.

I have all of that working, however I am at a loss when looking for a way to automatically move to the next host when the SSH credentials are rejected. Preferably, I would also like to log the error code with something different than when a host simply fails to respond. Currently, I have to babysit my script, and hit "OK" followed by "Cancel" when the credentials fail.

Currently, both a failure to connect and me cancelling the connection result in "Err.Description" resolving to "Connection Failed".

Thanks in advance for any advice.

Here are the relevant parts of my script:

nCounterHost = 0
Do

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

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

'Connect to Host
crt.Session.Connect "/SSH2 /L Adtran001 /PASSWORD Adtran001 /ACCEPTHOSTKEYS " & vHostIP(nCounterHost)

' 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
If nError <> 0 Then
' Handle the error (log to a file, etc.)
nCounterErrors = nCounterErrors + 1
If nCounterErrors = 1 Then
Set objErrorsFile = g_fso.OpenTextFile(g_strErrorsFile, ForAppending, True)
objErrorsFile.WriteLine "'~~~~~~~~~~~~~NEW RUN " & g_strHostsFile & " ~~~~~~~~~~~~~"
objErrorsFile.WriteLine "Customer: " & vHostname(nCounterHost) & ", IP: " & vHostIP(nCounterHost) & ", Error Description = " & strErr
objErrorsFile.Close
Else
Set objErrorsFile = g_fso.OpenTextFile(g_strErrorsFile, ForAppending, True)
objErrorsFile.WriteLine "Customer: " & vHostname(nCounterHost) & ", IP: " & vHostIP(nCounterHost) & ", Error Description = " & strErr
objErrorsFile.Close
End If
Else
'Do Work
End If

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

'Iterate Counter and Move To Next Entry
nCounterHost = nCounterHost + 1

Loop Until vHostIP(nCounterHost) = ""
Reply With Quote