#$language = "VBScript" #$interface = "1.0" ' LogOutputOfSpecificCommand-UseReadString.vbs ' Example that demonstrates how to log the output of ' specific commands to a file without having to manually ' turn logging on before and off after running each ' command. ' ' This specific example doesn't use the logging script API to write data to a ' file. Instead, we use the ReadString() method to capture the result of each ' command and write it manually to a file of our choosing (g_szLogFile serves as ' a template for the file name which changes with each command that is issued to ' the remote. For example, the results of the first command will be written to ' a file named Command#01_Results.txt, the results of the 2nd command to ' Command#02_Results.txt, etc...). ' ' Specifically, this example automates the logging process by: ' - Using an array of commands that are to be sent to the ' remote system (g_vCommands) ' - Using objTab.Screen.Send() to issue each command. ' - Using the objTab.Screen.ReadString() method to not only ' wait for an indication that the command sent has been ' completed, but also capture all of the text received ' while the command was running. ' ' Note: This script assumes that SecureCRT is currently connected to ' the remote device prior to running this script. Otherwise, the ' script will exit with an error message. ' ' If you want this script to perform the connection ' sequence as well, simply replace the "If Not ' objTab.Session.Connected" block within the Main() sub ' below with a line similar to the following: ' ' objTab.Session.Connect "/S MySessionName" ' Option Explicit Dim g_fso Set g_fso = CreateObject("Scripting.FileSystemObject") Const ForReading = 1 Const ForWriting = 2 Const ForAppending = 8 Dim g_szLogFile, objTab g_szLogFile = GetMyDocumentsFolder & "\Command#__NUM___Results.txt" Set objTab = crt.GetScriptTab Dim g_vCommands(100) g_vCommands(0) = "pwd" g_vCommands(1) = "ls -lR --color /tmp" g_vCommands(2) = "ps -eaf | grep vshelld" g_vCommands(3) = "tail -100 /var/log/messages" 'g_vCommands(4) = "sh run" 'g_vCommands(5) = "" 'g_vCommands(6) = "" 'g_vCommands(7) = "" 'g_vCommands(8) = "" 'g_vCommands(9) = "" ' . ' . ' . '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Sub Main() objTab.Screen.Synchronous = True ' Instruct WaitForString and ReadString to ignore escape sequences when ' detecting and capturing data received from the remote (this doesn't ' affect the way the data is displayed to the screen, only how it is handled ' by the WaitForString, WaitForStrings, and ReadString methods associated ' with the Screen object. objTab.Screen.IgnoreEscape = True If Not objTab.session.connected then MsgBox "Not Connected. Please connect before running this script." exit sub end if Dim szCommand, szPrompt, nRow, szLogFileName, nIndex ' Get the shell prompt so that we can know what to look for when ' determining if the command is completed. Won't work if the prompt ' is dynamic (e.g. changes according to current working folder, etc) nRow = objTab.Screen.CurrentRow szPrompt = objTab.screen.Get(nRow, 0, nRow, objTab.Screen.CurrentColumn - 1) szPrompt = Trim(szPrompt) Dim szLogFile nIndex = 0 Do szCommand = Trim(g_vCommands(nIndex)) ' If the command is empty, then we should be done issuing commands ' (there's nothing else in our command array g_vCommands) if szCommand = "" then Exit Do ' Set up the log file for this specific command szLogFile = Replace(g_szLogFile, "__NUM__", NN(nIndex + 1, 2)) ' Send the command text to the remote objTab.Screen.Send szCommand & vbcr ' Wait for the command to be echo'd back to us. objTab.Screen.WaitForString vbcr, 1 objTab.Screen.WaitForString vblf, 1 Dim szResult ' Use the ReadString() method to get the text displayed while the ' command was runnning. Note that the ReadString usage shown below ' is not documented properly in SecureCRT help files included in ' SecureCRT versions prior to 6.0 Official. Note also that the ' ReadString() method captures escape sequences sent from the remote ' machine as well as displayed text. As mentioned earlier in comments ' above, if you want to suppress escape sequences from being captured, ' set the Screen's IgnoreEscape property to True. szResult = objTab.Screen.ReadString(szPrompt) Dim objFile Set objFile = g_fso.OpenTextFile(szLogFile, ForWriting, True) ' If you don't want the command logged along with the results, comment ' out the very next line objFile.WriteLine "Results of command: " & szCommand ' Write out the results of the command to our log file objFile.WriteLine szResult ' Close the log file objFile.Close ' Move on to the next command in our command array g_vCommands nIndex = nIndex + 1 Loop End Sub '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Function GetMyDocumentsFolder() Dim myShell Set myShell = CreateObject("WScript.Shell") GetMyDocumentsFolder = myShell.SpecialFolders("MyDocuments") End Function '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Function NN(nNumber, nDesiredDigits) ' Normalizes a single digit number to have nDesiredDigits 0s in front of it Dim nIndex, nOffbyDigits, szResult nOffbyDigits = nDesiredDigits - len(nNumber) szResult = nNumber For nIndex = 1 to nOffByDigits szResult = "0" & szResult Next NN = szResult End Function