VanDyke Software

Scripting FAQ

Scripting FAQ

SecureCRT® supports several scripting languages including VBScript using ActiveX on Windows. Starting with SecureCRT 6.6, you can also use Python on Windows and Mac OS X. 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.

How do I send control characters in a script?

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"

Four Fast Ways to Learn More…

  1. Read or download one of our secure solutions white papers.
  2. Download a free 30-day evaluation copy of our products.
  3. Let us help define the right Secure Shell solution for your company.
  4. Subscribe to our What's New page for tips, solution ideas, and product news.

Where can I get VBScript and JScript documentation?

Microsoft has plenty of documentation including an online reference, example scripts and a downloadable reference that you can install on your PC for both VBScript and JScript.

You can find all of this at Microsoft's Scripting Technologies web page:

http://msdn.microsoft.com/en-us/library/d1et7k7c(VS.85).aspx

Where can I get the VBScript and JScript engines?

If you have IE 4.0 or later installed then you already have versions of both of these script engines on your machine. You can download the most recent versions of these engines from Microsoft's scripting web page:

http://msdn.microsoft.com/en-us/library/d1et7k7c(VS.85).aspx

How can I run a program in a script?

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, 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.

For information and downloads related to WSH look for the "Windows Script Host" section at:

http://msdn.microsoft.com/en-us/library/ms950396.aspx

What does "Invalid script header:..." mean?

CRT/SecureCRT 3.0 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, 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...

Can I write CRT scripts in Perl?

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

http://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 CRT aren't reported back to CRT 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. CRT 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.

Sometimes in VBScript I get errors if I don't use parentheses around function arguments and other times I get the error "can't use parentheses when calling a subroutine" when I *do* use them. What's the problem?

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")

How can I read files in my script?

CRT™ 3.0/SecureCRT® 3.0 does not offer direct support for reading and writing to files, but the ActiveX scripting language you are

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 );
         }
};

How can I pass arguments to my script?

Note: The following capability requires at least CRT™ /SecureCRT® version 4.0.

You can now pass arguments to ActiveX scripts (running in CRT) by using the /ARG command-line option to pass arguments to CRT 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 CRT and using the /ARG command-line option to pass the data. In the example below, two instances of CRT are started and different hostnames, usernames, and ports are made available to scripts being run by those instances of CRT:

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