# $language = "VBScript" # $interface = "1.0" ' IterateOverAllSessionRecursively_CSVOutputFormat.vbs ' ' Last Modified: ' 11 Dec, 2019 ' - Initial revision. ' ' Description: ' ' Shows how to iterate over all sessions in SecureCRT's session manager, and ' perform some custom action on that data. The example reads specific data ' from each session configuration and writes it to a comma separated value (CSV) ' file. This script also illustrates how to use the VBScript built-in Shell ' and File System objects, as well as SecureCRT's SessionConfiguration object ' and how to create functions. ' ' This script must be run from within SecureCRT. Dim g_shell Set g_shell = CreateObject("WScript.Shell") Dim g_fso Set g_fso = CreateObject("Scripting.FileSystemObject") ' If you want this script to iterate over all the SSH2 sessions testing ' connectivity, set this boolean to True. See the DoWorkWithSession() ' function below. Dim g_bAttemptSSHConnections g_bAttemptSSHConnections = False Const ForReading = 1 Const ForWriting = 2 Const ForAppending = 8 Dim g_strOutputFile, g_objLogFile g_strOutputFile = _ g_shell.SpecialFolders("MyDocuments") & "\IterationOutputFile.csv" If g_fso.FileExists(g_strOutputFile) Then If g_fso.FileExists(g_strOutputFile & ".bak") Then g_fso.DeleteFile g_strOutputFile & ".bak" End If g_fso.CopyFile g_strOutputFile, g_strOutputFile & ".bak" g_fso.DeleteFile g_strOutputFile End If ' ' The following line writes a header at the top of the file. ' The DoWorkWithSession function below is where you specify which data to ' extract from sessions' .ini files. If you want the descriptive header ' at the top of the file, then you should make sure the information in ' WriteToFile "Session,Hostname,etc." matches the variables and order ' you specify in the DoWorkWithSession function below ' WriteToFile "Folder,SessionName,Hostname,Protocol,Emulation,Description,ServerIdent", g_strOutputFile Dim g_strConfigFolder, g_strSessionFolder, g_nSessionCount '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Sub Main() ' Get SecureCRT's configuration folder path g_strConfigFolder = g_shell.ExpandEnvironmentStrings(GetConfigPath()) ' Get the Sessions folder path g_strSessionFolder = _ g_fso.GetAbsolutePathName(g_strConfigFolder & "\Sessions\") IterateOverFilesInFolder g_strSessionFolder IterateOverFolder g_strSessionFolder MsgBox "Finished iterating over " & g_nSessionCount & " sessions." g_shell.Run Chr(34) & g_strOutputFile & Chr(34) End Sub '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Sub IterateOverFolder(strPath) Dim objFolder, objSubFolder, colFolders, strCurPath Set objFolder = g_fso.GetFolder(strPath) 'MsgBox "Processing " & objFolder Set colFolders = objFolder.SubFolders For Each objSubFolder In colFolders strCurPath = strPath & "\" & objSubFolder.name 'MsgBox "Processing folder: " & strCurPath IterateOverFilesInFolder strCurPath IterateOverFolder strCurPath Next End Sub '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Sub IterateOverFilesInFolder(strPath) Dim colFiles, objFolder, objFile, strFilename, strSessionName Set objFolder = g_fso.GetFolder(strPath) Set colFiles = objFolder.Files For Each objFile In colFiles If objFile.Name <> "__FolderData__.ini" And _ objFile.Name <> "Default.ini" And _ LCase(g_fso.GetExtensionName(objFile.Path)) = "ini" Then g_nSessionCount = g_nSessionCount + 1 strFilename = strPath & "\" & objFile.Name ' Now iterate over each session. We need the path relative to ' Sessions so GetBaseName isn't enough; need to trim everything out ' of path through the ...\Sessions\, so compare to ' g_strSessionFolder. Dim nSessionLength nSessionLength = Len(strFilename) - Len(g_strSessionFolder) strSessionName = Right(strFilename, nSessionLength) ' Convert backslashes to forward slashes strSessionName = Replace(strSessionName, "\", "/") DoWorkWithSession strSessionName End If Next End Sub '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Sub DoWorkWithSession(strSession) Dim objSessionConfig, strHostname, strProtocol, strEmulation, strDescription strPathToSecureCRT = g_strInstallFolder strSession = Replace(strSession, ".ini", "") ' Ignore errors to bypass failure to connect popup windows On Error Resume Next ' Open the session's config file Set objSessionConfig = crt.OpenSessionConfiguration(strSession) ' Get the specific data from session ini file ' Add or remove variables according to the information you want to extract ' from .ini files. If you visually inspect a .ini file you should be able ' to determine the parameter name to use with GetOption. For example, ' you would see the following in a .ini file for an SSH2 session: ' S:"Protocol Name"=SSH2 ' The parameter you provide to GetOption below would be "Protocol Name" ' if you wanted to know the protocol the session uses ' ' We quote all of the values in case they have commas or linefeeds (as ' in the case of the description, particularly) strFolder = g_fso.GetParentFolderName(strSession) strSessionName = g_fso.GetFileName(strSession) strSessionPath = """" & strSession & """" strHostname = """" & objSessionConfig.GetOption("Hostname") & """" strProtocol = """" & objSessionConfig.GetOption("Protocol Name") & """" strEmulation = """" & objSessionConfig.GetOption("Emulation") & """" strDescription = _ """" & Join(objSessionConfig.GetOption("Description"), vblf) & """" ' If g_bAttemptSSHConnections is True, and it's an SSH2 connection ' attempt to connect to the remote host and get the server's ident ' string. If the connection fails, write NO_RESPONSE in that field ' instead. If the protocol isn't SSH2, then write NOT_APPLICABLE ' in that field instead. crt.Session.SetStatusText "Protocol: <" & strProtocol & ">" On Error Goto 0 strIdent = "UNDEFINED" If g_bAttemptSSHConnections And Instr(strProtocol, "SSH2") > 0 Then strHost = Replace(strHostname, """", "") strPort = CSTR(objSessionConfig.GetOption("[SSH2] Port")) crt.Session.SetStatusText "Trying " & strHost & ":" & strPort If crt.Session.Connected Then crt.Session.Disconnect On Error Resume Next crt.Session.Connect "/RAW " & strHost & " " & strPort, False, True On Error Goto 0 crt.Screen.Synchronous = True crt.Session.SetStatusText "Connected? " & crt.Session.Connected If crt.Session.Connected Then On Error Resume Next strIdent = crt.Screen.ReadString(vbcr, 5) On Error Goto 0 If crt.Screen.MatchIndex = 0 Then strIdent = "TIMED_OUT" End If If strIdent = "" Then strIdent = "ERROR_READING_IDENT_STRING" End If crt.Session.Disconnect Else strIdent = "NO_RESPONSE" End If Else strIdent = "NOT_APPLICABLE (g_bAttemptSSHConnections is False)" End If crt.Session.SetStatusText "" ' Disable on error resume next On Error Goto 0 ' For each session's information, write the data to a file. ' WARNING: if you change the order of the items below, be sure to modify ' line 49 above which writes the header information, currently: ' WriteToFile "Session,Hostname,Protocol,Emulation,Description", g_strOutputFile ' to reflect the new order of the items written to the CSV file. Dim vData vData = Array(_ strFolder, strSessionName, strHostname, strProtocol, strEmulation, strDescription, strIdent) WriteToFile Join(vData, ","), g_strOutputFile End Sub '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Sub WriteToFile(strText, strFilePath) Set objFile = g_fso.OpenTextFile(strFilePath, ForAppending, True) objFile.WriteLine strText objFile.Close End Sub ' ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Function GetConfigPath() ' Try and get at where your configuration folder is located strOptionName = "Upload Directory V2" strOrigValue = crt.Session.Config.GetOption(strOptionName) crt.Session.Config.SetOption strOptionName, "${VDS_CONFIG_PATH}" ' Make the change, so that the above templated name will get written ' to the config... crt.Session.Config.Save ' Now, load a fresh copy of the config, and pull the option... so ' that SecureCRT will convert from the template path value to the ' actual path value: Set objConfig = crt.OpenSessionConfiguration(crt.Session.Path) strConfigPath = objConfig.GetOption(strOptionName) ' Now, let's restore the setting to its original value crt.Session.Config.SetOption strOptionName, strOrigValue crt.Session.Config.Save ' Now return the config path GetConfigPath = strConfigPath End Function