SecureCRT Scripting FAQ

SecureCRT® supports several scripting languages so you can automate tasks and processes. On the Windows platform, ActiveX scripting languages include VBScript, JScript, and Perlscript. Python is supported on Windows, Mac, and Linux. A script recorder helps you build your keystrokes into a VBScipt or Python script.

More information on scripting in SecureCRT can be found on the Windows Scripting Examples page, the Python Scripting Examples page, and the VanDyke Software online Scripting Essentials guide.

To send or embed control characters in strings in JScript/Javascript convert them to octal values with a prepended "\" character for example:

// send ^C, (decimal 3)
crt.screen.Send("\003")

// send ^D, (decimal 4)
crt.screen.Send("\004")

// send ^M, (decimal 13)
crt.screen.Send("\015")

In VBScript use the Chr() function with the decimal value of the character you want to send:

' Send ^C
crt.screen.Send Chr(3)

' Send ^D
crt.screen.Send Chr(4)

' Send ^M
crt.screen.Send Chr(13)

You can use the VBScript '&' operator to concatenate strings with the Chr() function, for example:

crt.screen.Send "hello" & Chr(10) & "goodbye"

You cannot run programs directly in an ActiveX script. However, if you have Microsoft's Windows Script Host (WSH) installed, you can tell WSH to run it for you.

The following VBScript statements use WSH to start Internet Explorer with a command-line argument:

Dim shell
Set shell = CreateObject("WScript.Shell")
shell.Run """C:\Program Files\Internet Explorer\IExplore.exe""http://www.vandyke.com"

Here's the same thing in JScript:

var shell = new ActiveXObject("WScript.Shell");
shell.Run("\"C:\\Program Files\\Internet Explorer\\IExplore.exe\"http://www.vandyke.com");

Note that the Run() function uses whitespace to separate program names from possible command-line arguments. In order to deal with paths with spaces, it is necessary to add extra quotes in both languages to make it clear.

SecureCRT scripts require a header that identifies the script language and the interface version. The header should begin on the first line of the script file and each header line should contain the following two lines:

# $language = "language"
# $interface = "1.0"

Note that each header line must have a "#" character as the first character on the line. Header lines may also be empty lines that begin with "#" followed by whitespace and a carriage return.

You should substitute <language> with the name of the actual script language in which the script is written. For example, a VBScript starts with this:

# $language = "VBScript"
# $interface = "1.0"

VBScript code here...

A JScript script starts with this header:

# $language = "JScript"
# $interface = "1.0"

// JScript code here...

Yes. If you have installed the PerlScript engine, then you can run SecureCRT scripts written in Perl. You can download and find information regarding Perlscript from:

www.activestate.com

When using Perlscript, there are a couple of issues you should be aware of that will make debugging your scripts easier:

By default, runtime errors in your Perlscript won't be reported unless you enable warnings about these errors. Be sure you include these lines in your script if you want to be informed of errors:

# Perl doesn't warn about errors by default. Enable errors.
use Win32::OLE;
Win32::OLE->Option(Warn => 3);

Another problem you may find with Perlscript is that runtime errors that occur within the "main" routine invoked by SecureCRT aren't reported back to SecureCRT in an informative manner. This is believed to be a bug that was present in the latest implementation of Perlscript (build 518). The following simple script demonstrates the problem:

# $language = "PerlScript"
# $interface = "1.0"

# Perl doesn't warn about errors by default. Enable errors.
use Win32::OLE;
Win32::OLE->Option(Warn => 3);

# display the version
$crt->Dialog->MessageBox($crt->{'Version'});

# 1. generate an error: invalid property $crt->Dialog->MessageBox($crt->{'XYZZY'});

sub main {

# display the version
$crt->Dialog->MessageBox($crt->{'Version'});

# 2. generate an error: invalid property
$crt->Dialog->MessageBox($crt->{'XYZZY'});

}

This script gets a runtime error when it tries to fetch an invalid property both at the global level and within the "main" routine. The first runtime error should display some verbose information indicating the line and the nature of the problem, however the second runtime error will simply be reported as "main failed..." with no indication why. SecureCRT will invoke your main subroutine if it exists, but you are not required to have one. In order to work around this problem, you may want to develop your Perlscript code outside of a main subroutine.

Part of what is going on is VBScript distinguishing between different kinds of procedures (Sub or Function). Whether there is a return value being part of that distinction. The upshot is this: You will need parentheses if you are calling a function that returns a value and you are using the return value, or if you are using the "Call" keyword. For example:

' Parentheses can't be used here. Disregarded return value
' makes this a subroutine call.
crt.screen.WaitForString "string", 5

' Parentheses needed here. Return value makes this a
' function call.
Dim result
result = crt.screen.WaitForString("string", 5)

' Parentheses are always needed when using 'Call'
Call crt.screen.WaitForString("string")
Call crt.screen.WaitForString("string", 5)

' With one argument both of these forms are legal.
crt.screen.WaitForString "string"
crt.screen.WaitForString("string")

SecureCRT® does not offer direct support for reading and writing to files, but the ActiveX scripting language you are using probably offers a mechanism to access files.

Microsoft's VBScript and JScript languages both allow you to create instances of "filesystem objects" that offer a flexible interface for manipulating files.

The following VBScript sample code creates a filesystem object, opens a file, then reads the file line by line and sends it to a server:

# $language = "VBScript"
# $interface = "1.0"

Sub main

' Open a file, read it in & send it one line at a time
Dim fso, f, str
Const ForReading = 1

Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.OpenTextFile("c:\temp\file.txt", ForReading, 0)

Do While f.AtEndOfStream <> True
     str = f.Readline
     crt.Screen.Send str & Chr(13)
     ' Wait for my prompt before sending another line
     crt.Screen.WaitForString("linux$")
Loop

End Sub

The following JScript sample code performs a similar operation:

# $language = "JScript"
# $interface = "1.0"

function main()
{
        var fso, f;
        var ForReading = 1, ForWriting = 2;

         fso = new ActiveXObject("Scripting.FileSystemObject");
         f = fso.OpenTextFile("c:\\temp\\file.txt", ForReading);

         while ( f.AtEndOfStream != true )
         {
             var str = f.Readline();

             // Append a CR to the line (decimal: 13, octal: 15).
             str += "\015";

             crt.Screen.Send( str );
         }
};

You can pass arguments to ActiveX scripts (running in SecureCRT) by using the /ARG command-line option to pass arguments to SecureCRT and then retrieving those arguments in your script using the "Arguments" object.

The example below is a common script that connects to multiple hosts and performs the same operation on each host, having been passed variable arguments for hostname, username, and port.

' Detect lack of proper arguments.
If crt.Arguments.Count < > 3 Then
MsgBox "This script requires hostname, username, and port arguments"
Exit Sub
End If


hostname = crt.Arguments(0)
username = crt.Arguments(1)
port = crt.Arguments(2)

The arguments are then made available to scripts by starting different instances of SecureCRT and using the /ARG command-line option to pass the data. In the example below, two instances of SecureCRT are started and different hostnames, usernames, and ports are made available to scripts being run by those instances of SecureCRT:

SecureCRT.exe /ARG router1 /ARG admin1 /ARG 5001
SecureCRT.exe /ARG router2 /ARG admin2 /ARG 2400

Three Fast Ways to Learn More…

  1. Read or download one of our secure solutions white papers.
  2. Download a free evaluation copy of our products.
  3. Let us help define the right Secure Shell solution for your company.

VanDyke Software uses cookies to give you the best online experience. Before continuing to use this site, please confirm that you agree to our use of cookies. Please see our Cookie Usage for details.