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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
SecureCRT 是一款常用的终端仿真软件,它支持使用脚本进行自动化操作,这个功能被称为“SecureCRT Scripting”。 SecureCRT Scripting 可以通过编写脚本实现自动化登录、执行命令、收集输出等操作,大大提高了工作效率。以下是 SecureCRT Scripting 技术文档的基本内容: ## 脚本语言 SecureCRT Scripting 使用的脚本语言是 VBScript,因此需要对 VBScript 有一定的了解。VBScript 是一种基于 COM(Component Object Model)的脚本语言,它可以调用 Windows 系统 API 和 COM 对象,实现各种操作。 ## 脚本编辑器 SecureCRT 内置了一个脚本编辑器,可以在编辑器中编写和编辑脚本。编辑器提供了自动完成、语法突出显示、语法检查等功能,方便编写和调试脚本。 ## 脚本基本操作 SecureCRT Scripting 支持以下基本操作: - 连接远程设备:使用 `crt.Session.Connect` 方法连接远程设备; - 执行命令:使用 `crt.Screen.Send` 方法发送命令; - 收集输出:使用 `crt.Screen.WaitForString` 方法等待输出,并使用 `crt.Screen.ReadString` 方法读取输出; - 断开连接:使用 `crt.Session.Disconnect` 方法断开连接。 ## 对象和方法 SecureCRT Scripting 中的对象和方法主要有以下几个: - `crt.Session`:表示当前会话,包括连接信息、登录信息等; - `crt.Screen`:表示当前屏幕输出,可以读取屏幕输出、发送命令等; - `crt.Dialog`:表示 SecureCRT 对话框,可以显示和处理各种对话框; - `crt.ScreenReader`:表示屏幕阅读器,可以读取屏幕上的文本。 ## 示例脚本 以下是一个示例脚本,可以连接远程设备并执行命令: ``` # $language = "VBScript" # $interface = "1.0" Sub Main Dim hostname, username, password hostname = "192.168.1.1" username = "admin" password = "password" ' 连接远程设备 crt.Session.Connect "/SSH2 /L " & username & " /PASSWORD """ & password & """ " & hostname ' 等待登录完成 crt.Screen.WaitForString "Password:" ' 输入密码 crt.Screen.Send password & vbCr ' 等待命令提示符 crt.Screen.WaitForString ">" ' 执行命令 crt.Screen.Send "show interfaces" & vbCr ' 等待命令输出 crt.Screen.WaitForString "show interfaces" ' 读取命令输出 output = crt.Screen.ReadString(">") MsgBox output ' 断开连接 crt.Session.Disconnect End Sub ``` 以上是 SecureCRT Scripting 技术文档的基本内容,希望对你有所帮助。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值