Attribute VB_Name = "SCRT_Connect" Sub ConnectToSecondaryHostAndRunCommands() Attribute ConnectToSecondaryHostAndRunCommands.VB_ProcData.VB_Invoke_Func = " \n14" ' ' ConnectToSecondaryHostAndRunCommands Example ' Usage: ' 1) Modify the line below which sets the ' strJHShellPrompt variable to change it from "$ " to ' reflect the last 1-2 chars of your jump host's shell ' prompt. ' ' ' 2) Set up your Excel Spreadsheet to have this format: ' ' Host/IP | Protocol | Username | Password | JumpHost Session Name | Enable Password ' ----------+------------+-----------+-----------+-------------------------+----------------- ' 10.0.0.1 | ssh2 | admin | Cisco | Jump Host | p4$$w0rd ' ----------+------------+-----------+-----------+-------------------------+----------------- ' 10.0.0.2 | ssh2 | admin_dif | Cisco_dif | Jump Host | p4$$w0rd ' ----------+------------+-----------+-----------+-------------------------+----------------- ' 10.0.0.3 | ssh2 | admin | Cisco | Jump Host | p4$$w0rd ' ----------+------------+-----------+-----------+-------------------------+----------------- ' 10.0.0.4 | ssh2 | admin | Cisco | Jump Host | p4$$w0rd ' . ' . ' . ' ' ' 3) Set up your Excel Spreadsheet to include this module code. ' - In Excel, press Alt+F11 to open the VBA editor. ' ' - Expand your sheet's VBAProject (or your PERSONAL.XLSB) ' and locate the "Modules" folder. ' ' - Right-click the Modules folder and choose: ' Import File... ' ' - Navigate to where you've saved this .bas file on ' your system, and import it. ' ' - Set up a mechanism to run the macro named: ' ConnectToSecondaryHostAndRunCommands ' ' ' 4) Run as an Excel Macro. ' - If a single cell is selected at the time the macro ' runs, then the data on that cell's row is used. ' - If a single cell/row is selected at the time the ' macro runs, then SecureCRT is launched with the /T ' command line arg to open in a tab within SecureCRT ' if an existing instance is already open. ' - If multiple rows are part of the current selection ' when the macro runs, then a new instance of the ' SecureCRT app will be opened in which the host from ' the first row will be connected. Then, hosts from ' the other rows that are selected will be connected ' in tabs within that new SecureCRT window. ' ' What does it do? ' This example launches SecureCRT to connect with a saved ' session named as per the "JumpHost Session Name" column ' value in the current spreadsheet. ' - Once authenticated to the jump host session, a ' dynamically-created script runs an 'ssh' command to ' attempt a connection to a secondary host using th ' first 4 columns in the sheet. ' - Once authenticated to the secondary host, the 'enable' ' command is run, and when the Password prompt appears, ' the value from the 6th column (Enable Password) is ' sent to the secondary host. ' Dim strSecondaryHost, strSecondaryUser, strSecondaryPass As String Dim strJumpHostSession, strEnablePasswrd As String Dim objRow As Range Dim nTimes As Integer nTimes = 0 ' Begin iterating over all the rows in the current selection For Each objRow In Selection.Rows ' Gather the data from each column in the current row: strSecondaryHost = Cells(objRow.Row, 1) strSecondaryUser = Cells(objRow.Row, 3) strSecondaryPass = Cells(objRow.Row, 4) strEnablePasswrd = Cells(objRow.Row, 6) ' The Jump Host session must exist in SecureCRT ' saved sessions and must have saved credentials, ' since this script does not demonstrate ' authentication to the jump host. strJumpHostSession = Cells(objRow.Row, 5) ' Create code for a script that will be passed to ' SecureCRT for getting connected to the secondary ' host after the jump host connection has already ' been established. ' NOTE: You will need to modify this value to ' reflect the last 1-2 characters of your ' jump host's shell prompt -- so that teh ' script will know when it's time to run ' the 'ssh user@secondary_host' command ' in the attempt to get connected to the ' secondary host. Dim strJHShellPrompt As String strJHShellPrompt = "$ " Dim strScriptCode As String strScriptCode = "" & _ "strSecondaryHost = crt.Arguments(0)" & vbCrLf & _ "strSecondaryUser = crt.Arguments(1)" & vbCrLf & _ "strSecondaryPass = crt.Arguments(2)" & vbCrLf & _ "strEnablePasswrd = crt.Arguments(3)" & vbCrLf & _ vbCrLf & _ "crt.Screen.Synchronous = True" & vbCrLf & _ "crt.Screen.WaitForString(""" & strJHShellPrompt & """)" & vbCrLf & _ "crt.Screen.Send ""ssh "" & strSecondaryUser & ""@"" & strSecondaryHost & vbcr" & vbCrLf & _ "crt.Screen.WaitForString(""ssword:"")" & vbCrLf & _ "crt.Screen.Send strSecondaryPass & vbcr" & vbCrLf & _ "crt.Screen.WaitForString("">"")" & vbCrLf & _ "crt.Screen.Send ""ena"" & vbcr" & vbCrLf & _ "crt.Screen.WaitForString(""ssword:"")" & vbCrLf & _ "crt.Screen.Send strEnablePasswrd & vbcr" & vbCrLf & _ "crt.Screen.WaitForString(""#"")" & vbCrLf & _ "Set objFSO = CreateObject(""Scripting.FileSystemObject"")" & vbCrLf & _ "objFSO.DeleteFile(crt.ScriptFullName)" & vbCrLf & _ "" Set fso = CreateObject("Scripting.FileSystemObject") Dim strTempPath As String Dim strScriptFile As String ' We'll save the dynamic script file to the TMP ' folder: strTempPath = fso.GetSpecialFolder(2) ' We'll name the dynamic script file so that each ' unique tab will have its own copy of the script ' to run (since the script will delete itself once ' it has completed). strScriptFile = strTempPath & "\ScrtXLSConnectScript_" & nTimes & ".vbs" ' Actually write the script code out to the temp ' location: Set objFile = fso.OpenTextFile(strScriptFile, 2, True) objFile.Write strScriptCode objFile.Close ' Now, prep arguments to use when launching ' SecureCRT so that SecureCRT will know: ' - What to name the window/tab (2ndary host name) ' - What script to run ' - What args to pass to the script ' - What session to use for connecting to the jump host Dim strArgs As String strArgs = "" & _ " /N " & strSecondaryHost & _ " /Script """ & strScriptFile & """" & _ " /arg """ & strSecondaryHost & """" & _ " /arg """ & strSecondaryUser & """" & _ " /arg """ & strSecondaryPass & """" & _ " /arg """ & strEnablePasswrd & """" & _ " /S """ & strJumpHostSession & """" ' Launch SecureCRT! Set objShell = CreateObject("wscript.shell") If Selection.Rows.Count > 1 And nTimes = 0 Then ' If multiple rows/hosts are involved, and it's ' the first time running SecureCRT, run without ' the /T so that a new instance is created: nResult = objShell.Run("SecureCRT " & strArgs, 5, False) ' Give the above SecureCRT instance time to launch ' before launching subsequent instances. 3 seconds ' should be long enough. Application.Wait (Now + TimeValue("0:00:03")) Else ' If only one row is involved (or it's not our ' first time through with multiple rows), launch ' SecureCRT with the /T command line argument ' which will tell the new instance of SecureCRT ' to pass off the connection and script args to ' the most recently-used existing SecureCRT ' instance who will take over on all accounts (so ' that subsequent connections are opened in tabs ' in the same new SecureCRT window). nResult = objShell.Run("SecureCRT /T " & strArgs, 5, False) ' Because script arguments are *GLOBAL* to a ' singular SecureCRT instance, we must give a ' bit of time to make sure that the "hand off" ' completes and the script begins running in ' the new tab sufficiently long to be able to ' capture the args in that instance of the script ' before we loop around to start another instance. ' Otherwise, most if not all the new tabs will all ' inherit the last row's connection info as the last ' one will will (in replacing the global script ' args in the original SecureCRT instance). 1 second ' may be enough for many scenarios, but if you have ' any tabs that end up being duplicates of a host ' already connected, then you will want to increase ' the "1" below until the problem no longer exists. Application.Wait (Now + TimeValue("0:00:01")) End If ' Keep track of how many times we've connected, so that ' we can know whether or not we've already been through ' this once. nTimes = nTimes + 1 Next ' Loop up to the next row (if any) End Sub