4848 tramway ridge dr. ne
suite 101
albuquerque, nm 87111
505 - 332 - 5700
www.vandyke.com
Scripting Essentials:
A Guide to Using
VBScript in SecureCRT
User's Guide
VanDyke Software and SecureCRT are a registered trademarks of VanDyke Software, Inc.
All other trademarks or registered trademarks are the property of their respective owners.
Copyright 2009-2010 VanDyke Software, Inc. All rights reserved.
A
A
G
G
u
u
i
i
d
d
e
e
t
t
o
o
U
U
s
s
i
i
n
n
g
g
V
V
B
B
S
S
c
c
r
r
i
i
p
p
t
t
i
i
n
n
S
S
e
e
c
c
u
u
r
r
e
e
C
C
R
R
T
T
Introduction 1
How This Document is Organized 1
Conventions Used in This Document 1
SecureCRT’s Scripting Objects/API Reference 2
Chapter 1: Creating Scripts 4
1.1 Starting from Scratch with a New Script 4
1.2 Recording a Script 5
1.3 Modifying an Existing Example Script 9
Chapter 2: Launching Scripts 11
2.1 Manual Script Execution 11
2.2 Automated Script Execution 20
Chapter 3: Connecting to Remote Machines 24
3.1 Connecting with a Pre-configured Session 24
3.2 Connecting in an “Ad Hoc” Fashion 25
3.3 Connecting in Tabs 26
Solution: Open a Group of Sessions in Tabs 28
3.4 Disconnecting Active Connections 29
Solution: Automatically Close an Inactive Connection (Auto Disconnect) 30
3.5 Connecting to a List of Remote Machines Within a Loop 31
3.6 Handling Connection Failures Within a Script 31
Chapter 4: Reading Data from Remote Machines 35
4.1 Accessing Selected Text on the Screen 35
Solution: Performing a Web Search with Selected Text 35
4.2 Waiting for Specific Data to Arrive 36
Solution: Receive Notification when “Error-Indicating” Text Appears 38
4.3 Capturing Data from a Remote Machine 46
Chapter 5: Sending Data to Remote Machines 54
5.1 Sending Plain Text 54
Solution: Repeat a Command with Variable Input from User 54
5.2 Sending Control Codes 56
5.3 Simulating Keyboard Events 57
Solution: Add "no" to Each Selected Line and Send to Remote 59
Chapter 6: Getting Information from the End User 62
6.1 Prompting for Simple Responses: Yes, No, OK, Cancel, etc. 62
6.2 Prompting for Arbitrary Text Input 69
Solution: Clone Current Tab Multiple Times 72
6.3 Building Custom Dialogs or Forms 77
Solution: Create a Custom Username and Password Input Dialog 81
Chapter 7: Logging, Reading, and Writing Files 83
7.1 Logging with SecureCRT's Session Object 83
7.2 Reading Data from Files Using the FileSystemObject 87
Solution: Read Data from Separate Files: Hosts, Commands 90
7.3 Writing Data to Files Using the FileSystemObject 95
Solution: Save Selected Text to a CSV File 97
Solution: Import Data from File to SecureCRT Sessions 100
Chapter 8: Working with the Windows Clipboard 113
8.1 Retrieving Data Stored in the Clipboard 113
8.2 Storing Data to the Clipboard 113
Solution: Auto-Save Command Results to the Clipboard 113
8.3 Changing Paste "Speed" 115
Solution: “Slow Paste” (Line Delay) 115
Solution: “Slow Paste” (Echo Delay) 116
Solution: Vary Paste Speed Based on Clipboard Length 117
8.4 Setting the Clipboard Text Format (Encoding) 118
A Guide to Using VBScript in SecureCRT
1 © 2009-2010 VanDyke Software, Inc.
I
I
n
n
t
t
r
r
o
o
d
d
u
u
c
c
t
t
i
i
o
o
n
n
A SecureCRT user once wrote to the VanDyke Software technical support department in reply to some
answers to scripting questions saying:
“This will alleviate a lot my daily boring and annoying job. My dream is to be able to
use scripts even at a higher level, …activate macros pertaining to the various
windows to automate almost completely my job.”
While your job may not be boring or annoying, freeing up time from the repetitive widget-cranking tasks
you need to do on a regular basis may very well be a target worth shooting for.
The main objective of this document is to provide concepts and examples of scripting within
SecureCRT – information that might even help you accomplish more work in less time.
H
H
o
o
w
w
T
T
h
h
i
i
s
s
D
D
o
o
c
c
u
u
m
m
e
e
n
n
t
t
i
i
s
s
O
O
r
r
g
g
a
a
n
n
i
i
z
z
e
e
d
d
The first two chapters of this document provide introductory information about how to create or edit
scripts and explore different ways of launching scripts.
The remaining chapters focus on goals/tasks and provide actual solutions in the form of example
scripts. If you’re looking for example code right away, skim through the chapters and pay special
attention to the solution sections indicated by the
Solution indicators within the table of contents
and throughout this document.
If you’re looking to learn a few tips and tricks which may help stock your toolbox with time-saving
techniques that you might find useful in various automation scenarios, consider reading through this
document chapter by chapter.
C
C
o
o
n
n
v
v
e
e
n
n
t
t
i
i
o
o
n
n
s
s
U
U
s
s
e
e
d
d
i
i
n
n
T
T
h
h
i
i
s
s
D
D
o
o
c
c
u
u
m
m
e
e
n
n
t
t
L
L
a
a
n
n
g
g
u
u
a
a
g
g
e
e
U
U
s
s
e
e
d
d
SecureCRT’s use of ActiveX technology allows a script author to use any ActiveX scripting
language for which a scripting engine is available to interface with SecureCRT as the script
host. Examples include VBScript and JScript, which are both native to Windows. Another
example of an ActiveX scripting language that can be used is PerlScript, which requires a third-
party Perl ActiveX scripting engine be installed.
VBScript, also known as Visual Basic Scripting Edition, is the language of choice for this
document. All example script code herein is written using the VBScript language. If you are not
familiar with VBScript, reference documentation is available both online and for download from
Microsoft.
Online:
http://msdn.microsoft.com/en-us/library/t0aew7h6.aspx
If the above link doesn’t work, perform a web search on the terms “Windows Script
Technologies Documentation” and you should be provided with an up-to-date link. Of
particular note would be the VBScript User’s Guide which gives information specific to
using VBScript in general, and also the VBScript Language Reference which provides
explanations of core components of the VBScript language.
Download:
A Guide to Using VBScript in SecureCRT
2 © 2009-2010 VanDyke Software, Inc.
http://www.microsoft.com/downloads/details.aspx?FamilyId=
01592C48-207D-4BE1-
8A76-1C4099D7BBB9
If the above link doesn’t work, performing a web search on the terms “Windows Script
Technologies Download” should provide you with an up-to-date link for downloading the
documentation in Compiled HTML Help (.chm) format directly from Microsoft.
C
C
o
o
d
d
i
i
n
n
g
g
S
S
t
t
y
y
l
l
e
e
a
a
n
n
d
d
C
C
o
o
n
n
v
v
e
e
n
n
t
t
i
i
o
o
n
n
s
s
Throughout this document you’ll see the use of specific font styles that indicate either actual
VBScript code or reflect user interface labels. The following table outlines these font styles and
their associated meanings.
Style Reflects Example
Fixed-width font
VBScript Code, file names
and extensions.
MsgBox "Hello World!"
Bolded Text
User interface labels or
window titles.
Open up the main Script menu and
choose Start Recording Script.
Examples of VBScript code throughout this document attempt to adhere to a common variable
naming convention where variables are named loosely according to their purpose. Since
VBScript is not a “typed” language, this naming convention helps remind the reader (and the
author) of the intended use of each variable. This naming convention is presented in the table
below.
Prefix Intended Use and Meaning Example
b
Boolean variable whose value will
typically be either
True or False.
bConnected = crt.Session.Connected
n
Numeric variable whose value will
typically be numeric.
nCounter = nCounter + 1
str
String variable for holding generic
text.
strLogFile = crt.Session.LogFileName
obj
Object variable referencing an
object.
Set objShell = CreateObject("WScript.Shell")
v
Array (vector) variable that
references an array of elements.
vElements = Split(strLine, ",")
g_
Indicates that a variable is intended
for use in a global scope. Helps
keep track of variables that might
be used both inside subroutines
and functions as well as outside
the scope of methods and
functions.
Set g_strMyDocs = _
objShell.SpecialFolders("My Documents")
S
S
e
e
c
c
u
u
r
r
e
e
C
C
R
R
T
T
’
’
s
s
S
S
c
c
r
r
i
i
p
p
t
t
i
i
n
n
g
g
O
O
b
b
j
j
e
e
c
c
t
t
s
s
/
/
A
A
P
P
I
I
R
R
e
e
f
f
e
e
r
r
e
e
n
n
c
c
e
e
This scripting guide focuses on scripting techniques specifically related to solving a variety of
problems by providing actual solution code that you can take and modify to meet your needs.
If you are looking for documentation detailing all of the SecureCRT scripting objects, methods,
properties, and related parameters (the API reference), you’ll find it in the SecureCRT Help file.
The SecureCRT scripting objects/API reference is found by opening the SecureCRT Help, and
browsing to the ActiveX Scripting / Script Objects Reference chapter, as indicated in the graphic
below:
A Guide to Using VBScript in SecureCRT
3 © 2009-2010 VanDyke Software, Inc.