# $language = "VBScript" # $interface = "1.0" ' ConnectInTabToSpecifiedHostname-HandlePasswordAuthInTerminalWindow.vbs ' Last Modified: 21 Mar, 2012 ' ' DESCRIPTION: ' Prompts for host/IP, username, and password (if not hard-coded in the script ' variables strHost, strUser, and strPwd), and then uses the information to ' connect to the host using the SSH2 protocol. The session configuration ' object is used to detect if the option is enabled for handling ' authentication in the terminal window, and prompts the user to have this ' script automatically enable it if the option is not currently enabled. ' SSH2 authentication is handled by waiting for the username and password ' prompts and sending the username and password, respectively. ' If you don't want to be prompted for host information, set the three variables ' below to non-empty, legitimate values; understand that storing sensitive data ' such as passwords in a plaintext file is not a best practice, and should be ' avoided for reasons of security: strHost = "192.168.232.128" strUser = "" ' If you choose to store a plaintext password in this file, make sure that the ' file system permissions for this script file are such that your user account ' is the only one that has access to the file. strPwd = "" '------------------------------------------------------------------------------- Sub Main() If strHost = "" Then strHost = Trim( _ GetPlainTextInput( _ "Specify host name or IP address", _ "Connect To Host", _ strHost)) ' Bail if the input was cancelled If strHost = "" Then Exit Sub End If ' Get Username and Password as needed If strUser = "" Then strUser = Trim( _ GetPlainTextInput(_ "Specify username for " & strHost & ":", _ "Username?", _ strUser)) ' Bail if user cancelled If strUser = "" Then Exit Sub End If If strPwd = "" Then strPwd = Trim(_ GetPasswordInput(_ "Specify password for " & strUser & ":", _ "Password?", _ strPwd)) ' Bail if user cancelled If strPwd = "" Then Exit Sub End If ' Connect in a new tab to the host specified bWaitForAuthToCompleteBeforeReturning = False bLetCallerDetectAndHandleConnectionErrors = True Set objNewTab = crt.Session.ConnectInTab( _ "/SSH2 " & strHost, _ bWaitForAuthToCompleteBeforeReturning, _ bLetCallerDetectAndHandleConnectionErrors) If objNewTab.Session.Connected <> True Then DisplayMessage "Failed to connect to " & strHost ' You're not allowed to close the script tab (the tab in which the ' script was launched oringinally), so only try if the new tab really ' was a new tab -- not just reusing a disconnected tab.) If objNewTab.Index <> crt.GetScriptTab().Index Then objNewTab.Close End If Exit Sub End If ' Before continuing on with the script, ensure that the session option ' for handling authentication within the terminal window is enabled Set objConfig = objNewTab.Session.Config bAuthInTerminal = objConfig.GetOption("Auth Prompts In Window") If Not bAuthInTerminal Then If PromptYesNo(_ "The 'Default' session (used for all ad hoc " & _ "connections) does not have the ""Display logon prompts in " & _ "terminal window"" option enabled, which is required for this " & _ "script to operate successfully." & vbcrlf & vbcrlf & _ "Would you like to have this script automatically enable this " & _ "option in the 'Default' session so that next time you run " & _ "this script, the option will already be enabled?") <> vbYes Then Exit Sub End If ' User answered prompt with Yes, so let's set the option, save, and exit objConfig.SetOption "Auth Prompts In Window", True objConfig.Save Exit Sub End If ' Make sure the new tab is "Synchronous" so we can properly wait/send/etc. objNewTab.Screen.Synchronous = True ' Handle authentication in the new tab using the new tab's object reference ' instead of 'crt' nAuthTimeout = 10 ' seconds ' Modify the "$" or the "->" in the array below to reflect the actual shell ' prompt you expect to find when authentication is successful to your remote ' machine. vPossibleShellPrompts = Array(_ "ogin:", _ "name:", _ "sword:", _ "Login incorrect", _ "authentication failed.", _ "$", _ "]#", _ "->") Do objNewTab.Screen.WaitForStrings vPossibleShellPrompts, nAuthTimeout Select Case objNewTab.Screen.MatchIndex Case 0 DisplayMessage "Authentication timed out!" Exit Sub Case 1,2 ' "ogin:", "name:" ' Send the username objNewTab.Screen.Send strUser & vbcr Case 3 ' "sword:" ' Send the password objNewTab.Screen.Send strPwd & vbcr Case 4,5 ' "Login incorrect", "authentication failed." ' Let user know, and attempt to get correct password strPwd = GetPasswordInput(_ "Password authentication to '" & strHost & "' as user '" & _ strUser & "' failed." & vbcrlf & vbcrlf & _ "Please specify the correct password for user " & _ "'" & strUser & "':",_ "Auth failure - Bad Password?",_ strPwd) If strPwd = "" Then DisplayMessage "User cancelled auth. Script exiting." Exit Sub End If ' If we have another password to try, automatically loop through ' to the top and try again... Case 6,7,8 ' "$", "]#", or "->" <-- Shell prompt means auth success objNewTab.Session.SetStatusText _ "Connected to " & strHost & " as " & strUser Exit Do Case Else DisplayMessage "Ooops! Looks like you forgot to add code " & _ "to handle this index: " & objNewTab.Screen.MatchIndex & _ vbcrlf & _ vbcrlf & _ "Modify your script code's ""Select Case"" block " & _ "to have 'Case' statements for all of the strings you " & _ "are passing to the ""WaitForStrings"" method." objNewTab.Session.Disconnect ' Only attempt to close the tab if it's not *the* script tab If crt.GetScriptTab().Index <> objNewTab.Index Then objNewTab.Close End If Exit Sub End Select Loop objNewTab.Session.SetStatusText "Doing work in script..." ' Do work in new tab here, using objNewTab.Screen, objNewTab.Session, etc. ' . ' . ' . ' For demonstration, simulate work being done by sending "work work work " ' and "sleeping" for a while. For nTempIndex = 1 to 3 objNewTab.Screen.Send "work " objNewTab.Screen.WaitForString "work " crt.Sleep 1000 Next objNewTab.Session.SetStatusText "Work completed." crt.Sleep 2000 ' Clear the status bar objNewTab.Session.SetStatusText "" ' When done with work, disconnect the new tab and close it objNewTab.Session.Disconnect ' Only attempt to close the tab if it's not *the* script tab If crt.GetScriptTab().Index <> objNewTab.Index Then objNewTab.Session.SetStatusText "Closing tab..." crt.Sleep 1000 objNewTab.Close crt.Session.SetStatusText "Script Completed." crt.Sleep 2000 crt.Session.SetStatusText "" Else crt.Session.SetStatusText "Script Completed." crt.Sleep 2000 crt.Session.SetStatusText "" ' Close the entire application? You decide. ' crt.Quit End If End Sub '------------------------------------------------------------------------------- Function PromptYesNo(strText) PromptYesNo = crt.Dialog.MessageBox(strText, "SecureCRT", vbYesNo) End Function '------------------------------------------------------------------------------- Function DisplayMessage(strText) crt.Dialog.MessageBox strText End Function '------------------------------------------------------------------------------- Function GetPlainTextInput(strPrompt, strTitle, strDefaultValue) GetPlainTextInput = _ crt.Dialog.Prompt(strPrompt, strTitle, strDefaultValue) End Function '------------------------------------------------------------------------------- Function GetPasswordInput(strPrompt, strTitle, strDefaultValue) GetPasswordInput = _ crt.Dialog.Prompt(strPrompt, strTitle, strDefaultValue, True) End Function