VanDyke Software Forums

VanDyke Software Forums (
-   Scripting (
-   -   Auto-Cancel SSH connection on bad credentials (

drkwood 05-01-2013 08:23 PM

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

' 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
Set objErrorsFile = g_fso.OpenTextFile(g_strErrorsFile, ForAppending, True)
objErrorsFile.WriteLine "Customer: " & vHostname(nCounterHost) & ", IP: " & vHostIP(nCounterHost) & ", Error Description = " & strErr
End If
'Do Work
End If

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

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

Loop Until vHostIP(nCounterHost) = ""

rtb 05-02-2013 07:56 AM

Hi drkwood,

Thanks for the question. The Connect() method is not an authentication method. Any failure to connect would be a connection failure.

To deal with and detect authentication failures, it would be necessary to handle authentication programmatically. Here is an example script in the Scripting forum sticky that illustrates how one can handle authentication:
Does this help you modify your script to meet your needs?

drkwood 05-02-2013 04:07 PM

When I run the script that was linked, I receive a runtime error at:

Set objNewTab = crt.Session.ConnectInTab( _
"/SSH2 " & strHost, _
bWaitForAuthToCompleteBeforeReturning, _

Wrong number of arguments or invalid propertly assignment.

I also had to add the following before it would even run.

I am using SecureCRT v6.1.4. Is this not supported until a later version?

rtb 05-03-2013 08:08 AM

Hi drkwood,

SecureCRT 6.1.4 does not support the third Boolean argument for the ConnectInTab() method. You should be able to get past the issue by removing the third argument.

Does this help resolve the issue?

drkwood 05-23-2013 11:16 AM

Finally getting back to this project...

Yes! The example code did get me what I needed - thank you for that. As soon as I clean up my script I will post the code in the event anyone else is having a similar issue in the future.

Also - this may be a bug in v6.1.4, but the code snippet to update the global setting for handling the logon in the terminal window was not taking effect on the next "ConnectInTab" process; I verified that it was taking effect on the tab I was currently working in. I ended up having to change the default session settings under the Global options to make the script function properly.

rtb 05-23-2013 12:14 PM

Hi drkwood,

I am glad to hear that you have things working.

The problem you encountered is another situation where the example script is not compatible with older versions of SecureCRT. We don't test script examples with old versions of SecureCRT. The script will run as expected in SecureCRT 7.1 which is the current official release.

The script failure is not a bug in SecureCRT 6.1.4, rather it is a difference of functionality in the Scripting API.

All times are GMT -6. The time now is 10:04 PM.