# $language = "VBScript" # $interface = "1.0" ' JumpHost-HandleSecondaryHostConnectionAttempts.vbs ' ' - NOTE: SecureCRT must currently be connected to a gateway machine or ' jump host on which commands can be issued to connect to secondary hosts ' (eg: 'ssh hostb', 'telnet hostc', etc.) crt.Screen.Synchronous = True vHosts = Array(_ "192.168.0.1",_ "192.168.1.1",_ "192.168.0.2") ' The order of elements in this array should be in the order of: ' - Expected login prompts (eg: "-->", "#", etc.) ' - Special cases (accept host key, enter pwd, enter username, etc.) ' - Fail/Bail cases (wrong pwd/login, hostname not found, time out, etc.) vPossibleResponses = Array(_ "-->",_ "$",_ "ogin:", _ "ame:", _ "(yes/no)?",_ "sword:",_ "Permission denied",_ "incorrect", _ "not known", _ "timed out") nIndex = 0 strJumpHostPrompt = "-->" 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 Telnet... or 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. Note that the strStringFound variable currently contains ' the "Secondary Host Prompt" if needed. ' . ' . ' . ' 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 strJumpHostPrompt ' Exit inner Do..Loop since we are done with the success case. Exit Do Case 3,4 ' Found "ogin" or "ame" which means waiting for user account. crt.Screen.Send strUsername & vbcr ' Fall through to the top of the inner Do..Loop to continue ' waiting for strings until all possibilities are exhausted. Case 5 ' 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 6 ' We are being prompted for a password. Send it. crt.Screen.Send strPassword & vbcr ' Fall through to the top of the inner Do..Loop to continue ' waiting for strings until all possibilities are exhausted. Case 7 ' SSH Password was denied or login was incorrect. 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 strJumpHostPrompt ' Exit inner Do..Loop and move to next host. Exit Do Case 8 ' Telnet login or password was incorrect. 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 "^]" crt.Screen.WaitForString "telnet>" crt.Screen.Send "quit" & vbcr ' Wait for an indication that the Ctrl+C was successful before ' attempting to connect to next host. crt.Screen.WaitForString strJumpHostPrompt ' Exit inner Do..Loop and move to next host. Exit Do Case 9,10 ' Not able to reach secondary host. Connection timed out. ' Wait for primary host prompt before exiting inner Do..Loop. crt.Screen.WaitForString strJumpHostPrompt ' 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.