SecureCRT下的批命令脚本

来源:互联网 发布:清风网络 编辑:程序博客网 时间:2024/04/28 00:53
下面的脚本是在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运行时库参考