V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
wsgzao
V2EX  ›  程序员

批量创建和导入 SecureCRT Sessions

  •  
  •   wsgzao ·
    wsgzao · 2018-07-19 17:58:44 +08:00 · 3074 次点击
    这是一个创建于 2100 天前的主题,其中的信息可能已经有所发展或是发生改变。

    前言

    批量创建和导入 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

    • Mac/Unix: ImportArbitraryDataFromFileToSecureCRTSessions.py.txt
    • Windows: ImportArbitraryDataFromFileToSecureCRTSessions.vbs.txt

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

    目前尚无回复
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   965 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 21:25 · PVG 05:25 · LAX 14:25 · JFK 17:25
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.