下面的指令碼是在SecureCRT下啟動並執行,採用vbs語言編寫。把所有的配置命令放在一個文字檔中,然後使用測試指令碼來執行這些命令,簡化測試過程中的配置過程。
這個指令碼在SecureCRT Version 4.0 (build 358)上調試通過。
執行前,根據自己的需要,修改一下CliPrompt、CliError、ShowError等常量。
CliPrompt是CLI的命令提示字元
CliError是配置出錯時顯示的出錯提示資訊,用來判別命令的執行結果
ShowError表示在出錯時,是不是顯示一個提示框
在執行時,會提示輸入一個命令列表檔案名稱,然後執行這個檔案中的所有命令。預設的列表檔案名稱是cmds.txt。
命令列表檔案的格式很簡單,每條命令單獨放在一行裡
如果一行的第一個字元是"#",表示這行是注釋行,在執行時被跳過
如果一行中沒有任何字元,或者只有空格,則認為是空白行,在執行時被跳過
希望對大家有協助。
# $language = "VBScript"
# $interface = "1.0"
' $Id$
Option Explicit
' constant
Const CliPrompt = "$" ' CLI prompt
Const CliError = "Error" ' the prompt when command exec error
Const ShowError = 1 ' If you dont want to see error msg, change to 0
Sub Main
Dim fso, f, cmdFile
Dim ln, cmd, pmt, sel, rc
Const ForReading = 1, ForWriting = 2, ForAppending = 8
' When error occured, continue to execute
On Error Resume Next
' Open file
cmdFile = crt.Dialog.Prompt("Enter your cmds filename:", "ExecCmds Script", "cmds.txt" )
Set fso = CreateObject("Scripting.FileSystemObject" )
Set f = fso.OpenTextFile(cmdFile, ForReading )
Do While f.AtEndOfStream <> true
' Read line
ln = f.ReadLine
' Trim blankspace
cmd = Trim(ln)
' Skip blank line and comment lines
If len(cmd) <> 0 And Left(cmd, 1) <> "#" Then
' For debug
' MsgBox("Line: " & cmd)
' Exec command
cmd = cmd & vbCrLf
Crt.Screen.Send cmd
rc = Crt.Screen.WaitForStrings(CliPrompt, CliError, 10)
If (showError = 1 And rc <> 1) Then
' error occured
pmt = "Error occured when exec:" & vbCrLf & cmd & vbCrLf & "Continue?"
sel = msgbox(pmt, vbOKCancel, "Information" )
If sel = vbCancel Then
Exit Do
End If ' rc = vbCancel
End If ' ShowError = 1 And rc <> 1
End If ' len(cmd) <> 0 And ...
Loop
f.Close
End Sub
參考資料
<1> SecureCRT help file
<2> Visual Basic Scripting執行階段程式庫參考