# $language = "VBScript" # $interface = "1.0" ' ImportSuperPuttyDBIntoSecureCRTSessions.vbs ' DESCRIPTION: ' Imports Superputty session configurations into SecureCRT saved sessions. ' ' This script will attempt to import Superputty sessions from an exported ' XML file. ' ' To ensure that any pre-existing sessions are not overwritten, imported ' sessions are stored in SecureCRT's Session Manager in a new folder named: ' ##_SuperputtyImport(YYYYMMDD_HHMMSS.mmm) ' ' Only the following fields will be imported from the XML file: ' Session Path & Session Name (SessionId) ' Hostname (Host) ' Protocol (Proto) ' Port ' ' Last Modified: ' 31 Oct, 2017 ' - Add code to "sanitize" folder names of any illegal ' characters (:|"<>), as well as probelematic ' characters such as trailing spaces. ' ' 20 Feb, 2015 ' - Initial revision ' --------------------------------------------------------------------- Set g_shell = CreateObject("WScript.Shell") Set g_fso = CreateObject("Scripting.FileSystemObject") Const ForReading = 1 Const ForWriting = 2 Const ForAppending = 8 g_strDesktop = g_shell.SpecialFolders("Desktop") Dim g_strWarnings Dim g_colDuplicatesOfExistingSessions, g_colSessionsCreated Set g_colDuplicatesOfExistingSessions = CreateObject("Scripting.Dictionary") Dim g_nDuplicateCount Set g_colSessionsCreated = CreateObject("Scripting.Dictionary") Dim g_nSessionsCreated g_nStartTime = Timer Dim g_vCommands(5) ' GetTimeDateWithMilliseconds is a function defined in this script which ' will return something like: ' 20150114093717.418 [ That is, YYYYMMDD_HHMMSS.mmm ] g_strImportDestinationFolder = "##_SuperputtyImport(" & GetTimeDateWithMilliseconds & ")" Sub Main() strPathToDataFile = _ crt.Dialog.FileOpenDialog( _ "Specify Path to existing super-putty XML export file", _ "Import", _ g_strDesktop & "\*.xml", _ "XML files (*.xml)|*.xml||") If strPathToDataFile = "" Then Exit Sub nImportStartTime = Timer ' Create an XML document object using Microsoft's building XMLDOM object: Set xmlDoc = CreateObject("Microsoft.XMLDOM") ' Instruct the XMLDOM object to be synchronous (wait for the XML file ' to be fully parsed prior to moving on with the script): xmlDoc.Async = False ' Now, actually load the contents of the XML file into the xmlDoc object xmlDoc.Load strPathToDataFile If xmlDoc.parseError.errorCode <> 0 Then crt.Dialog.MessageBox "A parsing error was detected while attempting to " & _ "read in the specified XML file: " & strPathToDataFile & _ vbcrlf & vbcrlf & _ "Error description: " & vbcrlf & xmlDoc.parseError.reason & vbcrlf & _ "This could mean that your XML file is saved with an encoding " & _ "that does not match the XML content-specified encoding. For " & _ "example, if your XML file were saved in ASCII format, but the " & _ "first line of the XML file said the following, then you would " & _ "need to either change the line in your XML file to reflect the " & _ "actual format of the file, or re-save the file in the format or " & _ "encoding specified on the line:" & vbcrlf & _ vbtab & "" Exit Sub End If ' XPath instruction to return all nodes containing SessionData strXPathQuery = "//SessionData" ' Get a list of all connections in the XML doc Set objNodeList = xmlDoc.SelectNodes(strXPathQuery) If objNodeList.Length > 0 Then ' Iterate over each connection node and pull out the associated data For Each objNode in objNodeList ' Get the necessary attributes: strSessionPath = objNode.getAttribute("SessionId") ' Nested folders in SecureCRT are recognized by "\" character so ' change character used by Superputty ("/") strSessionPath = FixSessionPathForIllegalCharsAndSpaces(strSessionPath) strProtocol = objNode.getAttribute("Proto") strPort = objNode.getAttribute("Port") strHost = objNode.getAttribute("Host") ' Copy the default session settings into new session name and set the ' protocol. Setting protocol is essential since some variables ' within a config are only available with certain protocols. Set objConfig = crt.OpenSessionConfiguration("Default") objConfig.SetOption "Protocol Name", strProtocol ' Save the config, then reload it, so the protocol, if changed, is accurate. ' Setting protocol is essential since some variables within a config ' are only available with certain protocols. objConfig.Save g_strImportDestinationFolder & "\" & strSessionPath ' Get a handle to new session config Set objConfig = crt.OpenSessionConfiguration(g_strImportDestinationFolder & "\" & strSessionPath) objConfig.SetOption "Hostname", strHost ' Putty's default emulation configuration objConfig.SetOption "Emulation", "Xterm" objConfig.SetOption "ANSI Color", True Select Case LCase(strProtocol) Case "ssh" objConfig.SetOption "[SSH2] Port", cInt(strPort) Case "telnet" objConfig.SetOption "Port", cInt(strPort) End Select objConfig.Save g_nSessionsCreated = g_nSessionsCreated + 1 If g_nSessionsCreated Mod 5 = 0 Then crt.Session.SetStatusText "Processed " & g_nSessionsCreated & " of " & objNodeList.Length & " sessions for import" ElseIf g_nSessionsCreated = objNodeList.Length Then crt.Session.SetStatusText "Processed all " & objNodeList.Length & " sessions in source file." End If Next Else MsgBox "No connections found in XML file using path: " & vbcrlf & vbcrlf & _ strXPathQuery & _ vbcrlf & vbcrlf & _ "Are you sure the data file is an XML file from super-putty?" End If If g_nSessionsCreated > 0 Then strSummaryMsg = _ "Successfully created " & g_nSessionsCreated & _ " sessions in " & Round(Timer - nImportStartTime, 2) & " seconds" & vbcrlf & _ "You may need to close and re-open the Session Manager in order to see the imported sessions." Else strSummaryMsg = _ "No super-putty session configurations were detected in the .xml file" & _ " you specified: " & vbcrlf & vbcrlf & strPathToDataFile End If crt.Dialog.MessageBox strSummaryMsg & vbcrlf & g_strWarnings ' Open up the Session Manager automatically. crt.Screen.SendSpecial "MENU_TOGGLE_SESSION_MANAGER" crt.Sleep 250 crt.Screen.SendSpecial "MENU_TOGGLE_SESSION_MANAGER" End Sub '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Function CreateXMLDocObject(byRef objXMLDoc, byRef strInterface) g_strLastError = "" Dim nIndex ' Get the 2nd (0-based) element of the interface number ' MsgBox strInterface nIndex = split(strInterface, ".")(2) On Error Resume Next Err.Clear Set objXMLDoc = CreateObject("Msxml2.DOMDocument." & nIndex & ".0") if Err.Number <> 0 then if nIndex < 2 then g_strLastError = "Error creating xml DOM object(" & strInterface & "): " & Err.Description On Error Goto 0 exit Function else strInterface = "Msxml2.DOMDocument." & nIndex - 1 & ".0" CreateXMLDocObject = CreateXMLDocObject(objXMLDoc, strInterface) On Error Goto 0 exit function end if end if On Error Goto 0 CreateXMLDocObject = True End Function '----------------------------------------------------------------------------- Function GetTimeDateWithMilliseconds() Set objWMIService = GetObject("winmgmts:\\.\root\cimv2") Set colItems = objWMIService.ExecQuery("Select * from Win32_OperatingSystem") For Each objItem In colItems strLocalDateTime = objItem.LocalDateTime Exit For Next ' strLocalDateTime has the following pattern: ' 20111013093717.418000-360 [ That is, YYYYMMDDHHMMSS.MILLIS(zone) ] ' Take the left-most 18 digits... strTimeDate = Left(strLocalDateTime, 18) strTimeDate = Left(strTimeDate, 8) & "_" & Mid(strTimeDate, 9) ' ... return the time/date string as value of the function in the following ' format: 20111013_093717.418 GetTimeDateWithMilliseconds = strTimeDate End Function '------------------------------------------------------------------------------ Function FixSessionPathForIllegalCharsAndSpaces(strSessionPath) strSessionPath = Replace(strSessionPath, "/", "\") vElems = Split(strSessionPath, "\") strFixedSessionPath = "" For Each strElem in vElems strElem = Trim(strElem) strElem = Replace(strElem, "?", "[QUESTION]") strElem = Replace(strElem, ":", "[COLON]") strElem = Replace(strElem, """", "[QUOTE]") strElem = Replace(strElem, "!", "[BANG]") strElem = Replace(strElem, "<", "[LT]") strElem = Replace(strElem, ">", "[GT]") strElem = Replace(strElem, "|", "[PIPE]") If strElem <> "" Then If strFixedSessionPath = "" Then strFixedSessionPath = strElem Else strFixedSessionPath = strFixedSessionPath & "\" & strElem End If End If Next FixSessionPathForIllegalCharsAndSpaces = strFixedSessionPath End Function