1、Script--->Start Recording Script,进行登录脚本录制;

161221848.jpg

2、当脚本录制完成后,Script--->Stop Recording Script,停止脚本录制,并保存录制的脚本,以*.vbs保存;

161714788.jpg

3、Options--->Session Options--->Logon Actions,选中Logon script,然后导入刚才录制的脚本*.vbs


162239773.jpg



1.  autologin.vbs

#$language = " VBScript "
#$interface
= " 1.0 "
Sub main
   crt.Screen.Synchronous
= True

   crt.Screen.WaitForString
" Login UserID: "
   crt.Screen.Send
" humpic " & VbCr
   crt.Screen.WaitForString
" Login Password: "
   crt.Screen.Send
" 123456 " & VbCr

   crt.Screen.Synchronous
= False
End Sub

2. CMD

SecureCRT.EXE   /SCRIPT autologin.vbs /telnet 127.0.0.1 23


securecrt.exe /SSH2 /L root /P 22 /PASSWORD root 127.0.0.1


最常用的ActiveX script引擎有VBScriptJScript,都是微软的东东,所以SecureCRT支持这两种脚本语言。脚本的头部指定脚本与接口类型,脚本内容以main函数作为执行入口

一、脚本头部及主函数

# $language = "VBScript" # $language = "JScript" # $language = "Python"

# $interface = "1.0"

Sub Main function main() { def main():

.....

End Sub  }  main()

说明:

脚本头部

languageSecureCRT使用的脚本引擎,若为JavaScriptlanguage = "JScript"

interfaceSecureCRT的接口版本

Python:需SecureCRT6.6以上才支持。

主函数

Sub Main

' Display SecureCRT's version

MsgBox "SecureCRT version is: " & crt.Version

condition = DoSomething()

If condition = 0 Then

' Error, bailout

Exit Sub

End If

End Sub

二、SecureCRT内置对象

SecureCRT提供了内置对象与脚本交互,这些对象封装了属性与操作,可用来操纵程序行为。其中crt是顶级对象,代表了SecureCRT进程,其属性有ActivePrinter(打印机),DialogScreenSession(会话),VersionWindow(窗口)这些二级对象,操作有SleepQuit

1.Screen操作屏幕的输入输出

要注意的是,Screen只代表客户区的可见部分,这部分的大小可以通过会话选项里的逻辑行、列数来设定。而整个会话的输出记录是回滚区,没有对应的对象;

属性:

CurrentColumnCurrentRow(当前光标所在逻辑行)

因为逻辑行、列数限定了可见区的大小,所以当输出满屏时,CurrentRowRows的值其实是一样的,都是最大值,不会随着屏幕输出再改变,这种设计实在是有些egg hurt,不知能否通过设置来解决

ColumnsRows(可见区中的输出行数)

Synchronous(同步模式,据说为了避免某些情况下的数据丢失,建议开启)。

crt.Screen.Synchronous = True

crt.Screen.Synchronous = False

操作:

Clear 清掉所有输出,包括回滚与提示符,直接变白板;

crt.Screen.Clear

Get 获取某个矩形区域内的输出文本,用行列数当坐标来指定;

crt.Screen.Get(row1, col1, row2, col2)

Get2获取指定两行间的输出文本

crt.Screen.Get2(row1, row2)

ReadString获得命令的执行输出

char = crt.Screen.ReadString()

Send发送命令字符串(包括换行符);

crt.Screen.Send "ps -ef|grep AppServer|grep -v grep" & vbCr

SendSpecial用来发送SecureCRT的一些内置命令,如键映射

screen.SendSpecial "MENU_PASTE"

screen.SendSpecial "TN_BREAK"

screen.SendSpecial "VT_PF1"

除了WaitForStrings,其它均返回布尔值

WaitForString等待输出某字符串,支持超时;

WaitForStrings多个字符串中任何一个;

index = crt.Screen.WaitForStrings("#" , ">" , 10)

返回值为为字符串的索引,从1开始,超时则返回0

WaitForCursor等待光标移动,

WaitForKey等待任意键按下,

2.Session提供当前会话接口。

属性:

Connected当前会话是否连接,返回bool

LocalAddress返回本PCIP

RemoteAddress返回会话IP

RemotePort返回会话端口

LogFileName:设置或返回当前会话日志目录

Logging:是否开启日志,返回bool

Path:当前会话开始的ini配置文件地址

操作:

Connect连接会话;

crt.Screen. Connect arg

crt.session.Connect("/s mysession")直接连接会话

crt.Session.Connect("/TELNET 214.25.28.25 23")  ‘telnet 23号端口

cmd = "/ssh2 /L " & user &" /PASSWORD " & pwd & " /C 3DES " & ip
crt.Session.Connect cmd
/S session_name:开始SecureCRT并打开会话连接。
/SSH1 [ssh_options] hostname:以SSH1协议打开hostname连接。
[/C cipher]标识加密技术,默认3DES;其他有NONE, DES, 3DES, RC4 and BLOWFISH
[/I identityfile]Specifies the location of the user's identity file. The identity file contains the private key needed to connect to the server using RSA authentication. The absence of this option causes password authentication to be used.
[/L username]用户名
[/P port]端口,默认22
[/PASSWORD password]密码
[/Z compressionlevel]密码压缩级别,1-9
/SSH2 [ssh_options] hostname:以SSH2协议打开hostname连接。
[/AUTH method]指定连接SSH2时的认证方法,多种方法用逗号隔开。
 i.e., /AUTH password,publickey,keyboard-interactive,gssapi
[/C cipher]标识加密技术,默认3DES;其他有NONE, 3DES, RC4 and TWOFISH
[/I identityfile]Specifies the location of the user's identity file. The identity file contains the private key needed to connect to the server using RSA authentication. The absence of this option causes password authentication to be used.
[/L username]用户名
[/LOCAL port:hostname:hostport]Forwards connection requests from port (on localhost) to hostport on hostname.
[/M MAC]指定消息认证码
[/P port]端口,默认22
[/PASSPHRASE passphrase]Logs on to the SSH2 server using passphrase as the passphrase for the identity file given with the /I option.
[/PASSWORD password]密码
[/REMOTE port:hostname:hostport]Forwards connection requests from port (on localhost) to hostport on hostname.
Requests the SSH2 server to accept connection requests on port (on the remote machine) and forward the requests to hostport on hostname
[/Z compressionlevel]密码压缩级别,1-9
/TELNET hostname [port]:开始SecureCRT并以Telnet协议打开hostname连接。
/RLOGIN hostname:以Rlogin协议打开hostname连接。
[/L username]用户名
/TAPI phone number:开始SecureCRT并以TAPI协议打开会话连接。
/SERIAL port:以port打开默认会话。

Disconnect断开会话;

crt.Session.Disconnect

Log启用或禁用日志;

crt.Session.Log(start[,append,[raw]])参数均为bool变量,默认false

3.Dialog提供了输入框和MessageBox

Prompt:提示用户输入

crt.Dialog.Prompt(message [, title [, default [, isPassword ]]])
Dim pass
pass = crt.Dialog.Prompt("Enter your password:", "Logon Script", "", True)
MessageBox:显示信息
crt.Dialog.MessageBox(message [, title [, buttons]])
' button 参数选项
Const ICON_STOP = 16 ' display the ERROR/STOP icon.
Const ICON_QUESTION = 32 ' display the '?' icon
Const ICON_WARN = 48 ' display a '!' icon.
Const ICON_INFO= 64 ' displays "info" icon.
Const BUTTON_OK = 0 ' OK button only
Const BUTTON_CANCEL = 1 ' OK and Cancel buttons
Const BUTTON_ABORTRETRYIGNORE = 2 ' Abort, Retry, and Ignore buttons
Const BUTTON_YESNOCANCEL = 3 ' Yes, No, and Cancel buttons
Const BUTTON_YESNO = 4 ' Yes and No buttons
Const BUTTON_RETRYCANCEL = 5 ' Retry and Cancel buttons
Const DEFBUTTON1 = 0 ' First button is default
Const DEFBUTTON2 = 256 ' Second button is default
Const DEFBUTTON3 = 512 ' Third button is default
' MessageBox() 的返回值
Const IDOK = 1 ' OK button clicked
Const IDCANCEL = 2 ' Cancel button clicked
Const IDABORT = 3 ' Abort button clicked
Const IDRETRY = 4 ' Retry button clicked
Const IDIGNORE = 5 ' Ignore button clicked
Const IDYES = 6 ' Yes button clicked
Const IDNO = 7 ' No button clicked
' 例子
result = crt.Dialog.MessageBox("Login Failed, Retry?", "Error", ICON_QUESTION Or BUTTON_YESNO Or DEFBUTTON2 )
If result = IDNO Then
Exit Sub
End If

4.Window:窗口

属性:

lActivate:标识当前SecureCRT窗口是否激活,返回bool

lCaption:设置或返回SecureCRT窗口的标题

lState:返回SecureCRT窗口的显示状态。

0 - hidden

1 - visible (normal)

2 - minimized

3 - maximized

操作:

Activate:激活SecureCRT窗口,置于桌面最顶层

Show:各种状态显示SecureCRT窗口。

0 - hidden

1 - visible (normal)

2 - minimized

3 - maximized

如果脚本不需要交互的话,可以设置crt.show 0,隐藏窗口执行

5. Arguments:参数

访问参数,参数通过/ARG命令行传递。' CRT.exe /ARG myhostname /ARG 5555

属性:

Count:传递给SecureCRT的参数个数。

crt.Arguments.Count

操作:

GetArg:激活SecureCRT,置于桌面最顶层

crt.Arguments.GetArg(number) ‘number0开始

crt.Arguments(number)

三、SecureCRT6.6新增对象

相比5.1crt多了些对象属性:Arguments(命令行参数),Clipboard(剪贴板),FileTransfer,操作上则添加了tab操作(标签页属性)和加强了错误处理(处理脚本出错但继续执行的情况),GetScriptTab比较常用,用来获得执行脚本所在的tab对象。其下的二级对象也加了不少属性和操作

object.Get2(row1, col1, row2, col2),和Get的参数一个样。看描述,Get获取的的是矩形区域内的文本,空白也被包括,而Get2会根据换行\r\n来判断行文本是否已结束,换行也被包括

ReadString非常有用,可用来获得命令的执行输出,惯用法:

1char = crt.Screen.ReadString()  //无参式,从输出中一次取一个字符

2str = crt.Screen.ReadString(StringToWaitFor, TimeOutSeconds)  //超时可省略

3str = crt.Screen.ReadString(["home", "work"], 10)  // [stringarray],类似WaitForStrings

四、SecureCRT应用程序运行选项

cmd = "SecureCRT.EXE /Script c:\test.vbs /arg 129.0.31.27 /arg zxiptv"

Shell cmd, 1

命令行运行选项参数:

/ARG argumnetSecureCRT运行参数

/SCRIPT filenameSecureCRT运行脚本。

/COLOR_SCHEME schemeSpecifies an existing color scheme A combination of session settings that specify a variety of colors associated with the terminal window, including foreground and background. to be used for the session A session is a set of options that are assigned to a connection to a remote machine. These settings and options are saved under a session name and allow the user to have different preferences for different hosts. being started. When this switch is used, any changes to the session color scheme will be ignored. If a nonexistent scheme is specified, the session configuration color scheme will be used.

/F folderpathnamePoints to an alternate configuration folder location. If there is no configuration file in the alternate folder, SecureCRT will create one. Without this option, SecureCRT will use the configuration information in the default folder location.

/FIREWALLConnects session using current global firewall settings. Replaces /PROXY and /SOCKS options.

/MAX_COLS nSecureCRT最大列数,范围,1321024

/NOMENU:隐藏菜单栏

/NOTOOLBAR:隐藏工具栏

/NOSAVE:设置不保存

/POS x ySecureCRT窗口位置

/TITLEBAR "Title to display"Sets the text in the title bar to the string given as argument.