前言

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# 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即可看到导入的配置信息
1
2
3
4
5
6
7
8
9
# 找到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

文章目录
  1. 1. 前言
  2. 2. 更新历史
  3. 3. 使用前提
  4. 4. Video
  5. 5. 实践过程
  6. 6. Files