SecureCRT也能和Xshell一样批量导入主机
Posted Jim
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SecureCRT也能和Xshell一样批量导入主机相关的知识,希望对你有一定的参考价值。
在Xshell可以像这样一个文件批量导入主机:
https://blog.netsarang.com/91/importing-csv-formatted-host-information-to-xshell/
在CRT上也可以,格式和Xshell的一样,但前提是需要运行一个自定义脚本:
脚本:https://forums.vandyke.com/showthread.php?p=37089#post37089
操作:https://www.vandyke.com/support/tips/importsessions.html
视频操作:https://youtu.be/f7nMFYhGoiI
简单操作步骤:
脚本文件内容:
ImportArbitraryDataFromFileToSecureCRTSessions.vbs
# $language = "VBScript" # $interface = "1.0" \' ImportArbitraryDataFromFileToSecureCRTSessions.txt \' (Designed for use with SecureCRT 7.2 and later) \' \' Last Modified: 23 Feb, 2018 \' - Warn user if the configuration folder appears to be read-only. \' - Fall back to secondary locations in which to attempt to write \' the results log file in case the user\'s Documents, Desktop, or \' configuration folder are all read-only or otherwise un-write-able \' for the user. \' \' Last Modified: 21 Dec, 2017 \' - Allow multiple \'description\' fields on the same line. All will be \' compounded together with each one ending up on a separate line in \' the Session\'s Description session option. \' - Allow \'username\' field to be defaulted in the header line \' - Allow \'folder\' field to be defaulted in the header line \' - Duplicate sessions are now imported with unique time-stamped \' names (for each additional duplicate). Earlier versions of this \' script would overwrite the first duplicate with any subsequent \' duplicates that were found in the data file. \' - When displaying the browse dialog, filter now includes both \' CSV and TXT file types, to make it easier to find the data file \' (less clicking). \' - Allow for protocol field to be defaulted, if not present in the \' header line. \' - Fix error messages relating to invalid header lines so they no \' longer indicate Protocol is a required field. If it\'s not present \' the Default Session\'s protocol value will be used. \' - Allow header fields to be case-insensitive so that "Description" \' and "UserName" work just the same as "description" and "username" \' \' Last Modified: 09 Aug, 2017 \' - Changed from using CInt to CLng in order to support port \' specifications larger than 32768 (max integer supported in VBScript) \' \' Last Modified: 20 Feb, 2017 \' - Added progress info to status bar \' - When a line from the source file has bogus/incomplete data on it, \' the script no longer halts operation, but instead, continues the \' import process for all remaining legitimate lines, skipping any \' lines that don\'t have sufficient/accurate format. \' - Changed format of summary message shown at end to include header \' line so entries that were skipped can be easily copied into a new \' document to be imported. \' - Toggle the Session Manager automatically so that imported sessions \' are more immediately visible in the Session Manager. \' \' Last Modified: 20 Jan, 2015 \' - Combined TAPI protocol handling (which is no longer \' supported for mass import) with Serial protocol \' import errors. \' - Enhanced example .csv file data to show subfolder specification. \' \' Last Modified: 21 Mar, 2012 \' - Initial version for public forums \' \' DESCRIPTION \' This sample script is designed to create sessions from a text file (.csv \' format by default, but this can be edited to fit the format you have). \' \' To launch this script, map a button on the button bar to run this script: \' http://www.vandyke.com/support/tips/buttonbar.html \' \' The first line of your data file should contain a comma-separated (or whatever \' you define as the g_strDelimiter below) list of supported "fields" designated \' by the following keywords: \' ----------------------------------------------------------------------------- \' session_name: The name that should be used for the session. If this field \' does not exist, the hostname field is used as the session_name. \' folder: Relative path for session as displayed in the Connect dialog. \' hostname: The hostname or IP for the remote server. \' protocol: The protocol (SSH2, SSH1, telnet, rlogin) \' port: The port on which remote server is listening \' username: The username for the account on the remote server \' emulation: The emulation (vt100, xterm, etc.) \' description: The comment/description. Multiple lines are separated with \'\\r\' \' ============================================================================= \' \' \' As mentioned above, the first line of the data file instructs this script as \' to the format of the fields in your data file and their meaning. It is not a \' requirement that all the options be used. For example, notice the first line \' of the following file only uses the "hostname", "username", and "protocol" \' fields. Note also that the "protocol" field can be defaulted so that if a \' protocol field is empty it will use the default value. \' ----------------------------------------------------------------------------- \' hostname,username,folder,protocol=SSH2 \' 192.168.0.1,root,_imported,SSH1 \' 192.168.0.2,admin,_imported,SSH2 \' 192.168.0.3,root,_imported\\folderA, \' 192.168.0.4,root,, \' 192.168.0.5,admin,_imported\\folderB,telnet \' ... and so on \' ============================================================================= \' \' \' The g_strDefaultProtocol variable will only be defined within the \' ValidateFieldDesignations function if the protocol field has a default value \' (e.g., protocol=SSH2), as read in from the first line of the data file. Dim g_strDefaultProtocol \' The g_strDefaultFolder variable will only be defined within the \' ValidateFieldDesignations function if the folder field has a default value \' (e.g., folder=Site34), as read in from the first line of the data file. Dim g_strDefaultFolder \' The g_strDefaultUsername variable will only be defined within the \' ValidateFieldDesignations function if the username field has a default value \' (e.g., username=bensolo), as read in from the first line of the data file. Dim g_strDefaultUsername \' If your data file uses spaces or a character other than comma as the \' delimiter, you would also need to edit the g_strDelimiter value a few lines \' below to indicate that fields are separated by spaces, rather than by commas. \' For example: \' g_strDelimiter = " " \' Using a ";" might be a good alternative for a file that includes the comma \' character as part of any legitimate session name or folder name, etc. Dim g_strDelimiter g_strDelimiter = "," \' comma \' g_strDelimiter = " " \' space \' g_strDelimiter = ";" \' semi-colon \' g_strDelimiter = chr(9) \' tab \' g_strDelimiter = "|||" \' a more unique example of a delimiter. \' The g_strSupportedFields indicates which of all the possible fields, are \' supported in this example script. If a field designation is found in a data \' file that is not listed in this variable, it will not be imported into the \' session configuration. Dim g_strSupportedFields g_strSupportedFields = _ "description,emulation,folder,hostname,port,protocol,session_name,username" \' If you wish to overwrite existing sessions, set the \' g_bOverwriteExistingSessions to True; for this example script, we\'re playing \' it safe and leaving any existing sessions in place :). Dim g_bOverwriteExistingSessions g_bOverwriteExistingSessions = False Dim g_fso, g_shell Set g_fso = CreateObject("Scripting.FileSystemObject") Set g_shell = CreateObject("WScript.Shell") Const ForReading = 1 Const ForWriting = 2 Const ForAppending = 8 Dim g_strHostsFile, g_strExampleHostsFile, g_strMyDocs, g_strMyDesktop g_strMyDocs = g_shell.SpecialFolders("MyDocuments") g_strMyDesktop = g_shell.SpecialFolders("Desktop") g_strHostsFile = g_strMyDocs & "\\MyDataFile.csv" g_strExampleHostsFile = _ vbtab & "hostname,protocol,username,folder,emulation" & vbcrlf & _ vbtab & "192.168.0.1,SSH2,root,Linux Machines,XTerm" & vbcrlf & _ vbtab & "192.168.0.2,SSH2,root,Linux Machines,XTerm" & vbcrlf & _ vbtab & "..." & vbcrlf & _ vbtab & "10.0.100.1,SSH1,admin,CISCO Routers,VT100" & vbcrlf & _ vbtab & "10.0.101.1,SSH1,admin,CISCO Routers,VT100" & vbcrlf & _ vbtab & "..." & vbcrlf & _ vbtab & "myhost.domain.com,SSH2,administrator,Windows Servers,VShell" & _ vbtab & "..." & vbcrlf g_strExampleHostsFile = Replace(g_strExampleHostsFile, ",", g_strDelimiter) Dim g_strConfigFolder, strFieldDesignations, vFieldsArray, vSessionInfo g_strConfigFolder = GetConfigPath() Dim strSessionName, strHostName, strPort Dim strUserName, strProtocol, strEmulation Dim strPathForSessions, strLine, nFieldIndex Dim strSessionFileName, strFolder, nDescriptionLineCount, strDescription Dim g_strLastError, g_strErrors, g_strSessionsCreated Dim g_nSessionsCreated, g_nDataLines g_strDateTimeTag = GetDateTimeTag() g_strBogusLinesNotImported = "" Import \'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Sub Import() g_strHostsFile = crt.Dialog.FileOpenDialog( _ "Please select the host data file to be imported.", _ "Open", _ g_strHostsFile, _ "CSV/Text Files (*.txt;*.csv)|*.txt;*.csv|All files (*.*)|*.*") If g_strHostsFile = "" Then Exit Sub End If \' Open our data file for reading Dim objDataFile Set objDataFile = g_fso.OpenTextFile(g_strHostsFile, ForReading, False) \' Now read the first line of the data file to determine the field \' designations On Error Resume Next strFieldDesignations = objDataFile.ReadLine() nError = Err.Number strErr = Err.Description On Error Goto 0 If nError <> 0 Then If nError = 62 Then crt.Dialog.MessageBox("Your data file is empty." & vbcrlf & _ "Fill it with import data and try again." & vbcrlf & vbcrlf & _ "ReadLine() Error code: " & nError & vbcrlf & _ "ReadLine() Error text: " & strErr) Else crt.Dialog.MessageBox("Unable to read the first line from your data file!" & _ vbcrlf & vbcrlf & _ "ReadLine() Error code: " & nError & vbcrlf & vbcrlf & _ "ReadLine() Error text: " & strErr) End If Exit Sub End If \' Validate the data file If Not ValidateFieldDesignations(strFieldDesignations) Then objDataFile.Close Exit Sub End If \' Get a timer reading so that we can calculate how long it takes to import. nStartTime = Timer \' Here we create an array of the items that will be used to create the new \' session, based on the fields separated by the delimiter specified in \' g_strDelimiter vFieldsArray = Split(strFieldDesignations, g_strDelimiter) \' Loop through reading each line in the data file and creating a session \' based on the information contained on each line. Do While Not objDataFile.AtEndOfStream strLine = "" strLine = objDataFile.ReadLine crt.Session.SetStatusText "Processing line #: " & _ NN(objDataFile.Line - 1, 4) \' This sets v_File Data array elements to each section of strLine, \' separated by the delimiter vSessionInfo = Split(strLine, g_strDelimiter) If UBound(vSessionInfo) < UBound(vFieldsArray) Then If Trim(strLine) <> "" Then g_strErrors = g_strErrors & vbcrlf & _ "Insufficient data on line #" & _ NN(objDataFile.Line - 1, 4) & ": " & strLine Else g_strErrors = g_strErrors & vbcrlf & _ "Insufficient data on line #" & _ NN(objDataFile.Line - 1, 4) & ": [Empty Line]" End If ElseIf UBound(vSessionInfo) > UBound(vFieldsArray) Then g_strErrors = g_strErrors & vbcrlf & _ "==> Number of data fields on line #" & _ NN(objDataFile.Line - 1, 4) & _ "(" & UBound(vSessionInfo) & ") " & _ "does not match the number of fields in the header " & _ "(" & UBound(vFieldsArray) & ")." & vbcrlf & _ " This line will not be imported (Does the session name have a character that " & _ "matches the delimiter you\'re using? Also check for characters that Windows does not " & _ "allow to be used in filenames: /\\:*?""<>|): " & vbcrlf & vbtab & strLine g_strBogusLinesNotImported = g_strBogusLinesNotImported & _ vbcrlf & strLine Else \' Variable used to determine if a session file should actually be \' created, or if there was an unrecoverable error (and the session \' should be skipped). Dim bSaveSession bSaveSession = True \' Now we will match the items from the new file array to the correct \' variable for the session\'s ini file For nFieldIndex = 0 To UBound(vSessionInfo) Select Case LCase(vFieldsArray(nFieldIndex)) Case "session_name" strSessionName = vSessionInfo(nFieldIndex) \' Check folder name for any invalid characters Dim re Set re = New RegExp re.Pattern = "[\\\\\\|\\/\\:\\*\\?\\""\\<\\>]" If re.Test(strSessionName) Then bSaveSession = False If g_strErrors <> "" Then g_strErrors = _ vbcrlf & g_strErrors g_strErrors = _ "Error: " & _ "Invalid characters found in SessionName """ & _ strSessionName & """ specified on line #" & _ NN(objDataFile.Line - 1, 4) & _ ": " & strLine & g_strErrors g_strBogusLinesNotImported = g_strBogusLinesNotImported & _ vbcrlf & strLine End If Case "port" strPort = Trim(vSessionInfo(nFieldIndex)) If Not IsNumeric(strPort) Then bSaveSession = False If g_strErrors <> "" Then g_strErrors = _ vbcrlf & g_strErrors g_strErrors = _ "Error: Invalid port """ & strPort & _ """ specified on line #" & _ NN(objDataFile.Line - 1, 4) & _ ": " & strLine & g_strErrors g_strBogusLinesNotImported = g_strBogusLinesNotImported & _ vbcrlf & strLine End If Case "protocol" strProtocol = Trim(lcase(vSessionInfo(nFieldIndex))) Select Case strProtocol Case "ssh2" strProtocol = "SSH2" Case "ssh1" strProtocol = "SSH1" Case "telnet" strProtocol = "Telnet" Case "serial", "tapi" bSaveSession = False g_strErrors = g_strErrors & vbcrlf & _ "Error: Unsupported protocol """ & _ vSessionInfo(nFieldIndex) & _ """ specified on line #" & _ NN(objDataFile.Line - 1, 4) & _ ": " & strLine Case "rlogin" strProtocol = "RLogin" Case Else If g_strDefaultProtocol <> "" Then strProtocol = g_strDefaultProtocol Else bSaveSession = False If g_strErrors <> "" Then g_strErrors = _ vbcrlf & g_strErrors g_strErrors = _ "Error: Invalid protocol """ & _ vSessionInfo(nFieldIndex) & _ """ specified on line #" & _ NN(objDataFile.Line - 1, 4) & _ ": " & strLine & g_strErrors g_strBogusLinesNotImported = g_strBogusLinesNotImported & _ vbcrlf & strLine End If End Select \' for protocols Case "hostname" strHostName = Trim(vSessionInfo(nFieldIndex)) If strHostName = "" Then bSaveSession = False g_strErrors = g_strErrors & vbcrlf & _ "Warning: \'hostname\' field on line #" & _ NN(objDataFile.Line - 1, 4) & _ " is empty: " & strLine g_strBogusLinesNotImported = g_strBogusLinesNotImported & _ vbcrlf & strLine End If Case "username" strUserName = Trim(vSessionInfo(nFieldIndex)) Case "emulation" strEmulation = LCase(Trim(vSessionInfo(nFieldIndex))) Select Case strEmulation Case "xterm" strEmulation = "Xterm" Case "vt100" strEmulation = "VT100" Case "vt102" strEmulation = "VT102" Case "vt220" strEmulation = "VT220" Case "ansi" strEmulation = "ANSI" Case "linux" strEmulation = "Linux" Case "scoansi" strEmulation = "SCOANSI" Case "vshell" strEmulation = "VShell" Case "wyse50" strEmulation = "WYSE50" Case "wyse60" strEmulation = "WYSE60" Case Else bSaveSession = False g_strErrors = g_strErrors & vbcrlf & _ "Warning: Invalid emulation """ & _ strEmulation & """ specified on line #" & _ NN(objDataFile.Line - 1, 4) & _ ": " & strLine g_strBogusLinesNotImported = g_strBogusLinesNotImported & _ vbcrlf & strLine End Select Case "folder" strFolder = Trim(vSessionInfo(nFieldIndex)) \' Check folder name for any invalid characters \' Note that a folder can have subfolder designations, \' so \'/\' is a valid character for the folder (path). Set re = New RegExp re.Pattern = "[\\|\\:\\*\\?\\""\\<\\>]" If re.Test(strFolder) Then bSaveSession = False If g_strErrors <> "" Then g_strErrors = _ vbcrlf & g_strErrors g_strErrors = _ "Error: Invalid characters in folder """ & _ strFolder & """ specified on line #" & _ NN(objDataFile.Line - 1, 4) & _ ": " & strLine & g_strErrors g_strBogusLinesNotImported = g_strBogusLinesNotImported & _ vbcrlf & strLine End If Case "description" strCurDescription = Trim(vSessionInfo(nFieldIndex)) If strDescription = "" Then strDescription = strCurDescription Else strDescription = strDescription & "\\r" & strCurDescription End If Case Else \' If there is an entry that the script is not set to use \' in strFieldDesignations, stop the script and display a \' message Dim strMsg1 strMsg1 = "Error: Unknown field designation: " & _ vFieldsArray(nFieldIndex) & vbcrlf & vbcrlf & _ " Supported fields are as follows: " & _ vbcrlf & vbcrlf & vbtab & g_strSupportedFields & _ vbcrlf & _ vbcrlf & " For a description of " & _以上是关于SecureCRT也能和Xshell一样批量导入主机的主要内容,如果未能解决你的问题,请参考以下文章