Creating ActiveX Scripts

SecureCRT


SecureCRT is capable of hosting "ActiveX Script" engines. The most common ActiveX script engines are VBScript and JScript (Microsoft’s version of JavaScript), both of which are freely available from Microsoft. Chances are you already have them installed if you’ve installed Internet Explorer 4.0.

ActiveX script engines communicate with SecureCRT via standard interfaces. Therefore, SecureCRT can host any compliant script engine to run your scripts. The advantage of this approach is that you can script SecureCRT using the language of your choice. If an ActiveX script engine is available for your preferred scripting language, you can write scripts that will work with SecureCRT.

Note: To run a logon script in SecureCRT, you must first turn off the Automate logon option in the Connection/Logon Actions category of the Session Options dialog and then, on the same dialog, check the Logon script check box and choose the script that you want to run.

Note2: A script cannot be run from another script. This includes scripts started manually and logon scripts.

Scripts for SecureCRT are text files that you create with your text editor.

A script header is not required as long as the file extension is registered in the registry with a script engine (as common file extensions are). If you want to use a header (for example, if you are using a file without an extension or with an extension that is not common), the header must begin on the first line of the script.

Script headers will be used by SecureCRT to identify which script language the script is written in and the version of SecureCRT scripting interface. Each line of the script header must begin with a (#) character. A SecureCRT script header includes a $language line that identifies the script engine and an $interface line to identify SecureCRT's interface version.

The syntax of the script header is always the same regardless of the script language you are using.

A simple but complete SecureCRT script with a header that identifies it as VBScript is shown below:

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

Sub Main

  ' Display
SecureCRT's version
  MsgBox "SecureCRT version is: " & crt.Version
End Sub

Note: A SecureCRT script header may also contain blank lines that begin with (#).

The quoted string following $language identifies the script engine. If you are writing scripts that use Microsoft’s JScript language, the appropriate identifier is JScript. If you are using another script engine you’ll need to consult the documentation for the identifier for that language. Currently the script header should specify version 1.0 for $interface. Future versions of SecureCRT may support other versions. The example script above has a subroutine named main where all of the script’s code is located. When SecureCRT executes scripts it always attempts to run a main routine if you have defined one.

It is not a requirement that you place your code within a main however there may be reasons why you would want to do this. The VBScript and JScript engines will parse and execute global script code (script code you have defined outside of any subroutine) before your main is executed. If you have "initialization" code that you want to ensure has been completely executed before your actual script code begins, it may be useful to place your initialization code at the global level. This will ensure that your initialization code will all execute before your main code runs.

Another reason you may want a main routine is to allow your scripts a way of aborting themselves in case of problems. In VBScript there is no built-in way of exiting a script at the global level. However, if you want to exit a subroutine it is possible to use the Exit Sub syntax to do so. For example, in VBScript:

Sub Main

  condition = DoSomething()
  If condition = 0 Then
    ' Error, bailout
    Exit Sub
  End If
  
 
End Sub

When the main routine ends the script has finished running. By placing your code within a main you have the option of invoking Exit Sub whenever it might be necessary.

The previous script samples are written in VBScript. The remainder of code samples in this document are all written in VBScript unless it is stated otherwise. The properties and methods of SecureCRT's interface can be used as documented by any compatible scripting language.

For files stored on NTFS file systems, the script header can optionally be stored in an NTFS alternate data stream called header.txt. One way to do this is to choose Run from the Windows Start menu and enter the following:

notepad myscript.txt:header.txt

Script files can be stored in UTF-8 or Unicode format. Storing a script file in UTF-8 or Unicode format enables characters from languages such as Chinese or Japanese to be included in the script.

For examples of SecureCRT scripts, see the sample scripts in the SecureCRT Installation\Scripts folder and on the VanDyke Software website in the Scripting FAQ section.