VERSION 1.0 CLASS BEGIN MultiUse = -1 'True END Attribute VB_Name = "ThisWorkbook" Attribute VB_GlobalNameSpace = False Attribute VB_Creatable = False Attribute VB_PredeclaredId = True Attribute VB_Exposed = True Sub ConnectAndSendCommands() Dim objRow As Range Set objRow = Selection.Rows(1) strProtocol = Cells(objRow.Row, 2) strHostname = Cells(objRow.Row, 1) strUsername = Cells(objRow.Row, 3) strPassword = Cells(objRow.Row, 4) Set fso = CreateObject("Scripting.FileSystemObject") strTempPath = fso.GetSpecialFolder(2) strScriptFile = strTempPath & "\ScrtXLSscript.vbs" Set objFile = fso.OpenTextFile(strScriptFile, 2, True) objFile.Write _ "strProtocol = crt.arguments(0)" & vbCrLf & _ "strHostname = crt.arguments(1)" & vbCrLf & _ "strUsername = crt.arguments(2)" & vbCrLf & _ "strPassword = crt.arguments(3)" & vbCrLf & vbCrLf & _ "crt.session.setStatusText ""connecting to 192.168.0.1...("" & strProtocol & "")""" & vbCrLf & vbCrLf & _ "If strProtocol = ""telnet"" then" & vbCrLf & _ " crt.session.connect ""/"" & strProtocol & chr(32) & strHostname" & vbCrLf & _ " crt.screen.synchronous = true" & vbCrLf & _ " crt.screen.WaitForString ""ogin:""" & vbCrLf & _ " crt.screen.Send strUsername & vbcr" & vbCrLf & _ " crt.screen.WaitForString ""ssword:""" & vbCrLf & _ " crt.screen.Send strPassword & vbcr" & vbCrLf & _ "End If" & vbCrLf & vbCrLf & _ "If InStr(strProtocol, ""ssh"") > 0 then" & vbCrLf & _ " crt.session.connect ""/"" & strProtocol & chr(32) & _" & vbCrLf & _ " ""/L "" & strUsername & chr(32) & _" & vbCrLf & _ " ""/password "" & strPassword & chr(32) & _" & vbCrLf & _ " strHostname" & vbCrLf & _ " crt.screen.synchronous = true" & vbCrLf & _ "End If" & vbCrLf & vbCrLf objFile.Write _ "Do" & vbCrLf & _ " ' Attempt to detect the command prompt heuristically..." & vbCrLf & _ " Do" & vbCrLf & _ " bCursorMoved = crt.Screen.WaitForCursor(1)" & vbCrLf & _ " Loop Until bCursorMoved = False" & vbCrLf & _ " ' Once the cursor has stopped moving for about a second, we'll" & vbCrLf & _ " ' assume it's safe to start interacting with the remote system." & vbCrLf & _ " ' Get the shell prompt so that we can know what to look for when" & vbCrLf & _ " ' determining if the command is completed. Won't work if the prompt" & vbCrLf & _ " ' is dynamic (e.g., changes according to current working folder, etc.)" & vbCrLf & _ " nRow = crt.Screen.CurrentRow" & vbCrLf & _ " strPrompt = crt.screen.Get(_" & vbCrLf & _ " nRow, _" & vbCrLf & _ " 0, _" & vbCrLf & _ " nRow, _ " & vbCrLf & _ " crt.Screen.CurrentColumn - 1)" & vbCrLf & _ " ' Loop until we actually see a line of text appear (the" & vbCrLf & _ " ' timeout for WaitForCursor above might not be enough" & vbCrLf & _ " ' for slower-responding hosts)." & vbCrLf & _ " strPrompt = Trim(strPrompt)" & vbCrLf & _ " If strPrompt <> """" Then Exit Do" & vbCrLf & _ " " & vbCrLf & _ "Loop" & vbCrLf & vbCrLf objFile.Write _ "crt.Session.setStatusText ""running commands ...""" & vbCrLf & vbCrLf & _ "crt.Screen.Send ""pager 0"" & vbcr" & vbCrLf & _ "crt.screen.WaitForString strPrompt" & vbCrLf & vbCrLf & _ "crt.Screen.Send ""sh config"" & vbcr" & vbCrLf & _ "crt.screen.WaitForString strPrompt" & vbCrLf & vbCrLf & _ "crt.Session.setStatusText ""closing connection...""" & vbCrLf & _ "crt.Screen.Send ""exit"" & vbcr" & vbCrLf & vbCrLf & _ "Do" & vbCrLf & _ " If crt.Session.Connected <> True then Exit Do" & vbCrLf & _ " crt.Sleep 100" & vbCrLf & _ "Loop" & vbCrLf & vbCrLf & _ "crt.Session.setStatusText ""Done.""" & vbCrLf & _ "crt.Sleep 2000" & vbCrLf & _ "crt.quit" & vbCrLf objFile.Close Set objShell = CreateObject("wscript.shell") nResult = objShell.Run( _ "SecureCRT /script " & strScriptFile & _ " /arg " & strProtocol & _ " /arg " & strHostname & _ " /arg " & strUsername & _ " /arg " & strPassword, _ 5, True) fso.DeleteFile strScriptFile End Sub