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 07-27-2011, 04:54 AM
rleyba rleyba is offline
Registered User
 
Join Date: Feb 2010
Posts: 24
Need vbscript to recover from wrong password

Hi Team,

I just need some securecrt scripting assistance to allow a script to recover from an incorrect password scenario and resume processing of the next part of the script.

Based on an earlier code snippet from this post --> http://forums.vandyke.com/showthread.php?t=8546

Could you advise how that script can be modified so if logging in to one host results in a "denied" message, it could skip this host and proceed to the next?


The actual message I am getting are these lines below and I was looking for a way to do a ctrl+c on the first "denied" message and move on.

Thanks very much for all the help.

Quote:
-bash-3.00$ ssh admin@10.1.8.62
This system is for authorized use only.admin@10.1.8.62's password:
Permission denied, please try again.
admin@10.1.8.62's password:
Permission denied, please try again.
admin@10.1.8.62's password:
Permission denied (publickey,password,keyboard-interactive).
-bash-3.00$
Reply With Quote
  #2  
Old 07-27-2011, 04:40 PM
rtb rtb is offline
VanDyke Technical Support
 
Join Date: Aug 2008
Posts: 4,306
Hi rleyba,

Thanks for the question. Here is an example that you can use in your script to handle the possible responses from remote hosts. This example will likely need extensive modification:
Code:
crt.Screen.Synchronous = True
vHosts = Array(_
    "192.168.0.1",_
    "192.168.1.1",_
    "192.168.0.2")
vPossibleResponses = Array(_
    "-->",_
    "$",_
    "(yes/no)?",_
    "sword:",_
    "Permission denied",_
    "Connection timed out")
    
nIndex = 0
strPrimaryPrompt = "-->"
Do
    strCommand = "ssh user@" & vHosts(nIndex)
    crt.Screen.Send strCommand & vbcr
    
    ' Wait for our command to be echoed back to us so we know that it has been
    ' received before we attempt to wait for all possible responses.
    crt.Screen.WaitForString strCommand & vbcr
    
    ' Start an inner Do..Loop that looks for all defined possible responses to
    ' the 'ssh...' command we just sent.
    Do
        crt.Screen.WaitForStrings vPossibleResponses
        If crt.Screen.MatchIndex > 0 Then
            strStringFound = vPossibleResponses(crt.Screen.MatchIndex - 1)
        End If
        Select Case crt.Screen.MatchIndex
            Case 1,2
                ' Found "-->" or "$" which in our example indicate successful
                ' authentications.
                
                ' Do work we need to complete on host including sending the exit
                ' command which will disconnect us from the secondary host
                ' allowing us to loop up to the top and connect to the next
                ' host.
                ' .
                ' .
                ' .
                
                ' Now that we have done the necessary work (including waiting
                ' for an indication that the last command we sent above has
                ' completed, send "exit" to disconnect from secondary host.
                crt.Screen.Send "exit" & vbcr
                
                ' Wait for an indication that the exit command was successful
                ' before attempting to connect to next host.
                crt.Screen.WaitForString strPrimaryPrompt
                
                ' Exit inner Do..Loop since we are done with the success case.
                Exit Do
                
            Case 3
                ' Found new hostkey prompt.  This indicates that this is the
                ' first time we have connected to the remote machine, and we
                ' need to accept the hostkey.    
                crt.Screen.Send "yes" & vbcr
                
                ' Fall through to the top of the inner Do..Loop to continue
                ' waiting for strings until all possibilities are exhausted.
            
            Case 4
                ' We are being prompted for a password.  Send it.
                crt.Screen.Send "password" & vbcr
                
                ' Fall through to the top of the inner Do..Loop to continue
                ' waiting for strings until all possibilities are exhausted.
                
            Case 5
                ' Password was denied.  Exit this inner Do..Loop and move on to
                ' the next host.
                
                ' First cancel the current authentication attempt to the
                ' secondary host.
                crt.Screen.SendKeys "^c"
                
                ' Wait for an indication that the Ctrl+C was successful before
                ' attempting to connect to next host.
                crt.Screen.WaitForString strPrimaryPrompt
                
                ' Exit inner Do..Loop and move to next host.
                Exit Do
                
            Case 6
                ' Not able to reach secondary host.  Connection timed out.  Wait
                ' for primary host prompt before exiting inner Do..Loop.
                crt.Screen.WaitForString strPrimaryPrompt
                
                ' Exit inner Do..Loop and move to next host.
                Exit Do
                
            Case Else
                ' Let user know that there is an unhandled case
                crt.Session.SetStatusText _
                    "Unhandled """ & strStringFound & """"
                ' Yikes... Never expect to be here, but if we got here, it's
                ' probably a programming error you've introduced with the
                ' 'vPossibleResponses' variable that you'll need to fix
                crt.Dialog.MessageBox "Yikes!" & vbcrlf & vbcrlf & _
                    "We never expect to get here. if you see this, you" & _
                    vbcrlf & _
                    "have probably introduced a programming error into" & _
                    vbcrlf & _
                    "your script code which will you will need to fix." & _
                    vbcrlf & vbcrlf & _
                    "Chances are you added a string to vPossibleResponses " & _
                    vbcrlf & _
                    "but you haven't added the code to handle " & _
                    vbcrlf & _
                    "what to do when that special string was found:" & _
                    vbcrlf & vbcrlf & vbtab & """" & strStringFound & """"
                crt.Screen.SendSpecial "MENU_SCRIPT_CANCEL"
                
            End Select
    Loop ' Inner loop waiting for all possible responses.

    nIndex = nIndex + 1
    
    ' Exit outer Do..Loop if we have attempted to connect to all hosts.
    If nIndex > Ubound(vHosts) Then Exit Do
    
Loop ' Outer loop connecting to all hosts in array.
Does this help you accomplish your goal?
__________________
--Todd

VanDyke Software
Technical Support
support@vandyke.com
505-332-5730
Reply With Quote
  #3  
Old 07-28-2011, 05:07 AM
rleyba rleyba is offline
Registered User
 
Join Date: Feb 2010
Posts: 24
Hi Todd,

Thanks for this. I shall certainly try this out.
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 03:55 AM.