SecureCRT scripting

本文摘自This help manual documents the VanDyke Software® Secure®, version 6.5 terminal emulation

 

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"

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

Sub Main

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.

 


 

Example VBScripts can be found in the SecureCRT installation folder. If you would like to modify an example script, please modify a copy of the script file because if the original scripts are updated, the installer will overwrite the example scripts.

The following are descriptions of the example scripts:

·    AddCommentToLogFile.vbs – This example script allows you to add a custom line of text to your SecureCRT log file.

·    AutoResponder.vbs – Demonstrates how to set up a list of commands/text that get sent to the remote as soon as a corresponding "trigger" string/text is detected from the remote system.

·    BasicReadStringExample.vbs – This example script captures the output of a command (ls -l) sent to a remote machine by using the Screen.ReadString() method. The captured text is then displayed in a message box window.

·    ConnectToMultipleSessionsAndSendCommands.vbs – Reads in sessions from a file (one session per line) and connects to each one (one at a time) and sends a series of commands to the remote, then disconnects from the session and moves on to the next session.

·    CopyOutputToClipboard.vbs – This script example is designed to run while connected to a Cisco Pix firewall or other router device.

·    GoogleSelectedText.vbs – When this script is launched, the text selected within the terminal window is used as the search term for a web search using google.com.

·    LogOutputOfSpecificCommand-UseReadString.vbs – Sends commands one by one as listed in the g_vCommands() array to the remote machine. The results of each command are captured into a variable, and then written to an individual log file (one log file for each command). Once all the commands have been run, Windows Explorer is launched, with the first command output file selected within the explorer window.

·    SaveSelectedTextToFile.vbs – If non-whitespace text is selected within the terminal screen, the user will be prompted for a location and filename in which to store the selected text. The selected text will then be saved to the file specified by the user.

·    SendCommandToAllTabs.vbs – This example script shows how to send the same command to all tabs that currently have an active connection.

·    UseIEAsCustomDialog.vbs – This sample script shows how the Internet Explorer automation object can be used to create custom dialog that can be used within a SecureCRT script.

·    UseIEAsListOutput.vbs – This example script shows how to create a custom dialog using the InternetExplorer.Application ActiveX object. This example also shows how data can be passed between the InternetExplorer object and SecureCRT.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值