SecureCRT下的批命令指令碼

來源:互聯網
上載者:User
下面的指令碼是在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執行階段程式庫參考

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.