Welcome to the VanDyke Software Forums

Join the discussion today!


Go Back   VanDyke Software Forums > Scripting

 
 
Thread Tools Rate Thread Display Modes
Prev Previous Post   Next Post Next
  #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
 


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 11:47 AM.