Welcome to the VanDyke Software Forums

Join the discussion today!


Go Back   VanDyke Software Forums > Scripting

Reply
 
Thread Tools Rate Thread Display Modes
  #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
  #2  
Old 05-02-2013, 07:56 AM
rtb rtb is offline
VanDyke Technical Support
 
Join Date: Aug 2008
Posts: 4,306
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#post37087
Does this help you modify your script to meet your needs?
__________________
--Todd

VanDyke Software
Technical Support
support@vandyke.com
505-332-5730
Reply With Quote
  #3  
Old 05-02-2013, 04:07 PM
drkwood drkwood is offline
Registered User
 
Join Date: May 2013
Posts: 6
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?
Reply With Quote
  #4  
Old 05-03-2013, 08:08 AM
rtb rtb is offline
VanDyke Technical Support
 
Join Date: Aug 2008
Posts: 4,306
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?
__________________
--Todd

VanDyke Software
Technical Support
support@vandyke.com
505-332-5730
Reply With Quote
  #5  
Old 05-23-2013, 11:16 AM
drkwood drkwood is offline
Registered User
 
Join Date: May 2013
Posts: 6
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.
Reply With Quote
  #6  
Old 05-23-2013, 12:14 PM
rtb rtb is offline
VanDyke Technical Support
 
Join Date: Aug 2008
Posts: 4,306
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.
__________________
--Todd

VanDyke Software
Technical Support
support@vandyke.com
505-332-5730

Last edited by rtb; 07-15-2013 at 07:39 AM.
Reply With Quote
Reply


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 05:30 AM.