|
#$language = "VBScript"
#$interface = "1.0"
' ImportCRTSessionsToSecureCRT.vbs
'
' Note: This script is intended to be run from with SecureCRT
' (Script / Run).
'
' Description: Obtains the CRT configuration folder location
from the
' registry and imports each session into the configuration
folder for
' SecureCRT as indidcated in the registry.
' - Creates folders if they do not already
exist in the SecureCRT
' config folder
' - Modifies Protocol to SSH2 (or the value
of g_szProtocol)
' - Modifies the port to 22 (or the value
of g_szPort _in Hex_).
' - Ignores the default.ini file
'
Option Explicit
Dim g_fso, g_shell
Set g_fso = CreateObject("Scripting.FileSystemObject")
Set g_shell = CreateObject("WScript.Shell")
Dim g_szProtocol, g_szPort
g_szProtocol = "ssh2"
' Port string must be HEX
g_szPort = "00000016"
' The lines that we will be modifying in
each .ini file
Dim g_szProtocolTag, g_szPortTag
g_szProtocolTag = "S:""Protocol Name""="
g_szPortTag = "D:""Port""="
Dim g_szCRTConfigFolderPath, g_szSecureCRTConfigFolderPath,
szCrtReg
Dim g_szCRTSessions, g_szSecureCRTSessions, szSecureCRTReg
szCrtReg =
"HKCU\Software\VanDyke\CRT\Global\Config Path"
szSecureCRTReg = "HKCU\Software\VanDyke\SecureCRT\Global\Config
Path"
g_szCRTConfigFolderPath = g_shell.RegRead(szCrtReg)
g_szSecureCRTConfigFolderPath = g_shell.RegRead(szSecureCRTReg)
Dim g_szSessionsAlreadyThere, g_nMovedCount,
g_nFoldersCreated
g_szSessionsAlreadyThere = ""
g_nMovedCount = 0
g_nFoldersCreated = 0
Const ForReading = 1
' If running this from outside SecureCRT:
' - comment out the first two lines of this file
' - remove the comment from the line below.
' - change all instances of "crt.Sleep" to "WScript.Sleep"
'Main
Dim g_szTitle
g_szTitle = "Import CRT Sessions To SecureCRT Script"
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Sub Main()
' Get a valid CRT
configuration folder
While Not g_fso.FolderExists(g_szCRTSessions)
g_szCRTSessions
= InputBox( _
"Please
enter the location of the CRT config folder:", _
g_szTitle,
_
g_szCRTConfigFolderPath)
If
g_szCRTSessions = "" Then Exit Sub
If
Not g_fso.FolderExists(g_szCRTSessions & "\Sessions")
Then
MsgBox
"The folder specified does not contain the" &
vbcrlf & _
"required
""Sessions"" subfolder:" & vbcrlf
& _
chr(9)
& g_szCRTSessions
g_szCRTConfigFolderPath
= g_szCRTSessions
g_szCRTSessions
= ""
End If
Wend
' Get a valid SecureCRT
Configuration folder
While Not g_fso.FolderExists(g_szSecureCRTSessions)
g_szSecureCRTSessions
= InputBox( _
"Please
enter the location of the SecureCRT config folder:",
_
g_szTitle,
_
g_szSecureCRTConfigFolderPath)
If
g_szSecureCRTSessions = "" Then Exit Sub
If
not g_fso.FolderExists(g_szSecureCRTSessions & "\Sessions")
Then
MsgBox
"The folder specified does not contain the " &
vbcrlf & _
"required
""Sessions"" subfolder:" & vbcrlf
& _
chr(9)
& g_szSecureCRTSessions
g_szSecureCRTConfigFolderPath
= g_szSecureCRTSessions
g_szSecureCRTSessions
= ""
End If
Wend
g_szCRTSessions
= g_szCRTSessions & "\Sessions"
g_szSecureCRTSessions = g_szSecureCRTSessions
& "\Sessions"
' This is where
all of the importing takes place.
ProcessFolder g_szCRTSessions, g_szSecureCRTSessions
Dim szStatusMsg
szStatusMsg = "Moved " &
g_nMovedCount & " sessions" & vbcrlf &
vbcrlf & _
"
From: " & chr(9) & g_szCRTSessions
& vbcrlf & _
" To:
" & chr(9) & g_szSecureCRTSessions & vbcrlf
& _
vbcrlf
& _
"In
the process, we created " & g_nFoldersCreated &
_
"
new folders in the SecureCRT Sessions hierarchy."
If g_szSessionsAlreadyThere
<> "" Then
szStatusMsg
= szStatusMsg & vbcrlf & _
vbcrlf
& "======" & vbcrlf & _
"The
following sessions were not copied from the CRT" &
_
"
config because they" & vbcrlf & _
"already
existed in SecureCRT:" & vbcrlf & _
g_szSessionsAlreadyThere
End If
MsgBox szStatusMsg
Set g_fso = Nothing
Set g_shell = Nothing
End Sub
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
' Helper Methods and Functions
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Sub ProcessFolder(szCRTFolderPath, szSecureCRTFolderPath)
' Recursive routine that copies all contents of the szCRTFolderPath
' to the szSecureCRTFolderPath
Dim objCRTFolder,
objSecureCRTFolder
Set objCRTFolder = g_fso.GetFolder(szCRTFolderPath)
' Create all of
the folders in the current location
Dim objSubFolder
For Each objSubFolder in objCRTFolder.SubFolders
Dim szCurTargetFolder
szCurTargetFolder
= szSecureCRTFolderPath & "\" & objSubFolder.Name
If Not g_fso.FolderExists(szCurTargetFolder)
Then
g_fso.CreateFolder
szCurTargetFolder
g_nFoldersCreated
= g_nFoldersCreated + 1
End If
ProcessFolder
objSubFolder.Path, szCurTargetFolder
Next
' Copy and modify
all of the sessions in the current location
Dim objFile
For Each objFile in objCRTFolder.Files
' Only copy
the file if it's not __FolderData__.ini or default.ini
' and if it
doesn't already exist.
If
objFile.Name <> "__FolderData__.ini" And _
objFile.Name
<> "Default.ini" Then
Dim
szTargetSession
szTargetSession
= szSecureCRTFolderPath & "\" & objFile.Name
If
Not g_fso.FileExists(szTargetSession) Then
'
Now make the config changes
Dim
objSrc, objDst, szCurrentLine, szText
Set
objSrc = objFile.OpenAsTextStream(ForReading)
Set
objDst = g_fso.CreateTextFile(szTargetSession)
While
Not objSrc.AtEndOfStream
szCurrentLine
= objSrc.ReadLine
If
InStr(szCurrentLine, g_szProtocolTag) > 0 Then
szCurrentLine
= g_szProtocolTag & g_szProtocol
ElseIf
InStr(szCurrentLine, g_szPortTag) > 0 Then
szCurrentLine
= g_szPortTag & g_szPort
End
If
szText
= szText & szCurrentLine & vbcrlf
Wend
objDst.Write
szText
objDst.Close
g_nMovedCount
= g_nMovedCount + 1
Else
If
Not InStr(g_szSessionsAlreadyThere, objFile.Name) > 0 Then
g_szSessionsAlreadyThere
= g_szSessionsAlreadyThere & _
vbcrlf
& chr(9) & objFile.Name
End
If
End
If ' Not g_fso.FileExists(szCurTargetSession)...
End If ' objFile.Name
<> "__FolderData__.ini" , etc...
Next
End Sub
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Function Continue(szMsg)
Continue = True
crt.Sleep 200
If MsgBox(szMsg, vbYesno, g_szTitle)
<> vbYes Then Continue = False
crt.Sleep 400
End Function
|