#1
|
|||
|
|||
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) = "" |
#2
|
|||
|
|||
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: http://forums.vandyke.com/showthread...7087#post37087Does this help you modify your script to meet your needs? |
#3
|
|||
|
|||
When I run the script that was linked, I receive a runtime error at:
Set objNewTab = crt.Session.ConnectInTab( _ "/SSH2 " & strHost, _ bWaitForAuthToCompleteBeforeReturning, _ bLetCallerDetectAndHandleConnectionErrors) Wrong number of arguments or invalid propertly assignment. I also had to add the following before it would even run. #$Language="VBScript" #$Interface="1.0" I am using SecureCRT v6.1.4. Is this not supported until a later version? |
#4
|
|||
|
|||
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? |
#5
|
|||
|
|||
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. |
#6
|
|||
|
|||
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. Last edited by rtb; 07-15-2013 at 08:39 AM. |
![]() |
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
Thread Tools | |
Display Modes | Rate This Thread |
|
|