下面的脚本是在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运行时库参考
相关文章
- mac系统下redis的启动关闭等命令
- UNIX命令,统计当前目录(含子目录)下所有后缀为.log的文件中ERROR出现的行数
- linux ls 参数列表过长,linux下ls mv命令参数列表过长的解决办法
- (转)linux下vi命令修改文件及保存的使用方法
- linux下vi命令修改文件及保存的使用方法
- Linux下的常用的打包和解压缩命令
- 【Linux常用命令之find查找文件】使用find查找当前路径以及子路径下的某个文件
- Windows 原生 cmd 窗口下编译 C++(cl命令)出现的问题及解决方法
- Linux命令行下的文本编辑器
- linux gedit怎么运行,linux下的gedit命令使用方法与技巧