批量创建和导入 SecureCRT Sessions

2018-07-19 17:58:44 +08:00
 wsgzao

前言

批量创建和导入 SecureCRT Sessions 过程很简单,只是我花了一点时间寻找合适的方法,没想到还是官方的最简单,如果大家已经有现成的配置文件,直接把别人的拷贝过来就好了,不用自己造轮子。说实话我在 Windows 上用 Xmanager/Xshell,MacBook 上使用 iTerm2,Mac 上似乎也没有特别舒服的 GUI,批量操作还是推荐 Ansible。

Import Arbitrary Data From File To SecureCRT Sessions

更新历史

2018 年 07 月 17 日 - 初稿

阅读原文 - https://wsgzao.github.io/post/securecrt/

扩展阅读

VanDyke Software Forums - https://forums.vandyke.com/forumdisplay.php?f=14


使用前提

This example shows how to create sessions from information in a text file (.csv format by default, but this can be edited to fit the format you have).

Note: This script will only work with SecureCRT version 7.2 and later.

# ImportArbitraryDataFromFileToSecureCRTSessions.py
#   (Designed for use with SecureCRT 7.2 and later)
#
#   Last Modified: 16 May, 2018
#     - Add a section to the results log that shows lines that weren't
#       imported. Lines are shown in a way that makes it easier to re-
#       run the script to import those lines after they've been modified
#       to correct any issues idendified (missing fields, too many fields,
#       disallowed characters in folder or session name, etc.) Also
#       modified the results log to have a formatting that more closely
#       matches that of the vbscript version to provide more consistency
#       when running the script regardless of the platform.
#     - Set session options for all imported sessions to reflect commonly
#       desired configurations (color scheme, anti-idle, log file naming,
#       larger scrollback buffer, etc.)
#     - Validate both folder paths and session names to prevent errors
#       that would normally stop the script. Report such failures at the
#       end of the scipt rather than halting the script in media res.
#       This validation includes preventing any attempts to name a session
#       with any disallowed character or illegal name (CON, PRN, AUX, NUL,
#       etc.)
#
#   Last Modified: 23 Feb, 2018
#     - Info-blurb about sessions that were created during the import was
#       missing from the results log
#     - If running on Windows, and unable to write to results log, make
#       sure clipboard data containing the results log info is formatted
#       with \r\n instead of just \n so that it's legible in Notepad, for
#       example, when pasted.
#
#   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
#      - 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.
#      - Allow header fields to be case-insentive so that "Description"
#        and "HostName", etc. work just as well as "description" and "hostname"
#
#   Last Modified: 18 Dec, 2017
#      - Remove unused (commented out) code block left in from the
#        20 Apr, 2017 changes.
#      - Fix required header line message to no longer reference
#        'protocol' field as required.
#      - Add fallback locations where the script will attempt to
#        write summary log of script's activities/errors/warnings.
#        This attempts to facilitate running this script in environments
#        where SecureCRT may not have access to a "Documents" folder
#        (such as when SecureCRT is being launched through VDI publishing).
#         --> First try Documents,
#         --> Then try Desktop,
#         --> Then try SecureCRT's config folder.
#         --> If none of the above are accessible for writing, the
#             script will copy the summary report to the clipboard,
#             providing the user with a way to see the summary report
#             if pasted into a text editor.
#      - Added support for defaulting the "folder" header so that all
#        new entries could be imported into a folder w/o having to
#        specify the folder on each line. Example header line for
#        CSV file with only hostname data would be:
#            hostname,folder=default_import_folder_name
#
#   Last Modified: 17 Nov, 2017
#      - No longer attempt to use platform to determine OS ver info,
#        as it's no longer needed.
#
#   Last Modified: 20 Apr, 2017
#      - No longer require protocol in header. Use the Default session's
#        protocol if the protocol field is not present in the header line.
#      - Conform to python join() method requiring only one argument.
#      - Prompt for delimiter character if it isn't found in the header line.
#      - Allow delimiter character to be NONE, so that a single field (hostname)
#        and corresponding data can be used to import sessions (say for example
#        if you have a file that just contains hostnames, one per line).
#      - [Bug Fix]: can't use + to concatenate str and int, so use format()
#        instead.
#      - [Bug Fix]: "Procotol" typo fixed to "Protocol" in error case where
#        protocol header field not found/set.
#
#   Last Modified: 04 Jan, 2017
#      - Added support for specifying logon script file to be set for
#        imported sessions.
#
#   Last Modified: 02 Jul, 2015
#      - Display status bar info for each line we're processing so that if
#        there's an error, the individual running the script might have
#        better information about why the error might have occurred.
#      - Handle cases where a line in the data file might have more fields
#        in it than the number of header fields designated for import. This
#        fixes an error reported by forum user wixxyl here:
#           https://forums.vandyke.com/showthread.php?t=12021
#        If a line has too many fields, create a warning to be displayed
#        later on, and move on to the next line -- skipping the current line
#        because it's unknown whether the data is even valid for import.
#
#   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'
# logon_script: The full path to the Logon Script filename for the session.
# =============================================================================
#
#
# 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
# =============================================================================

Video

Watch this YouTube video if you're having troubles understanding what this script does, or how to go about using it.

https://youtu.be/f7nMFYhGoiI

实践过程

批量导入列表适合于使用 SSH 密钥登录方式管理

  1. 下载 ImportArbitraryDataFromFileToSecureCRTSessions
  2. 配置自定义 SecureCRTSessions.csv 格式的文件
  3. 运行 SecureCRT,选择菜单栏Script-Run,先选择脚本再选择自定义配置文件
  4. 关闭 Session Manager 或者重新打开 SecureCRT 即可看到导入的配置信息
# 找到 Seesions 目录
/Users/wangao/Library/Application Support/VanDyke/SecureCRT/Config/Sessions

# 编辑 SecureCRTSessions.csv
folder,username,hostname,protocol
GOP/NGINX,wangao,192.168.0.13,SSH2
GOP/NGINX,wangao,192.168.0.14,SSH2
GOP/NGINX,wangao,192.168.0.15,SSH2
GOP/NGINX,wangao,192.168.0.16,SSH2

Files

https://forums.vandyke.com/showpost.php?p=37089&postcount=1

3163 次点击
所在节点    程序员
0 条回复

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/472435

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX