在SecureCRT里,用得最多的应该就是crt.Screen,基本上很多操作都是基于屏幕的返回字来决定下一步该作何操作:
第一:crt.Screen.WaitForString("KeyString",timewaiting)
该函数是单字符串判断,KeyString是需要查找的关键字,timewaiting是一个超时阀值,例如:crt.Screen.WaitForString("people:",5)该行代码的意思就是在5秒内没有检测到people:出现,就执行下一条语句,如果改成:crt.Screen.WaitForString("people:")那就是指直到people:出现才执行下一行代码。
WaitForString是有返回值的,返回值是True 或者 False。因此,可以根据返回值进行条件判断以确定一下条代码。例如:
If (crt.Screen.WaitForString ("current state : UP",1)<>False) Then
portStatus="PortUP"
Else
portStatus="PortDown"
End Ifmsgbox portStatus
这段代码用于判断端口状态情况并记录下来.
第二:crt.Screen.WaitForStrings("KeyString1","KeyString2",...,timeout)
用于多个字符串的判断,timeout的作用是一样的。例如:
crt.Screen.WaitForStrings("cisco","huawei","H3C",5)
意思就是在5秒内有检测到相应的字符时,返回相应的索引号(索引号是从1开始的)。如果都没有检查到,则返回0.因此,该函数的使用可以如下:
Dim SwitchKey
SwitchKey=crt.Screen.WaitForStrings("cisco","huawei","H3C",5)
Select case SwitchKey
case 1
MsgBox "思科设备"
case 2
MsgBox "华为设备"
case 3
MsgBox "华三设备"
case else
MsgBox "未知设备"
End Select
其实SecureCRT支持的脚本语言就是VBS,这个脚本语言与VB有较大的不同,对于界面的支持性较差。不过也有几个对话性的函数
1、InputBox :提示用户输入参数
temp = inputbox("提示用户你输入参数的名称","对话框的名称") :需要将输入的参数赋值给某一个参数进行使用。
2、输出函数 msgbox
msgbox “给用户输出的信息对话框”
eg.求正方形面积的脚本
dim r,s
r=inputbox("请输入正方形的边长:","求正方形面积的程序")
s=r*r
msgbox(s)
下面就我的工作性质,跟我同事一块制作的一个脚本程序——维护着N台路由器设备,每天需要对这些设备进行例行巡检。下面是程序的所有代码,希望对各位有所帮助********************************************************************************************************************************************************************************************************************#$language = "VBScript"
#$interface = "1.0"'说明:
'本代码可在CRT工具中实现Cosico,华为,爱立信路由器交换机数据的自动配置或作为设备巡检之用;Set screen = crt.Screen '创建屏幕对象
'定义全局变量:
'Device变量为操作设备对象数组,作为存储设备名用;
'CiscoAggreOfCommands变量为思科设备命令集合数组,作为存储思科设备命令用;
'NE40EAggreOfCommands变量为华为NE40E设备命令集数组,作为存储华为NE设备命令用;
'SE800AggreOfCommands变量为爱立信SE800设备命令集数组,作为存储爱立信SE800设备命令用;
'ArrreOfRiskCommands变量为各厂家设备存在风险性操作命令的数组,作为存储过滤风险操作命令之用;
'DeviceNumth变量为Device数组序号;
'CommandNumth变量为各厂家命令数组的序号;
'ResultOfType变量为判断设备类型标识符;
'TypeOfCommand变量作为存储当前命令类型用;
Dim Device,CiscoAggreOfCommands,NE40EAggreOfCommands,SE800AggreOfCommands,ArrreOfRiskCommands
Dim DeviceNumth , CommandNumth ,ResultOfType
Dim TypeOfCommand
'爱立信设备集合
'Device = Array("CE35-ERICSE800","CE36-ERICSE800","CE37-ERICSE800","CE38-ERICSE800")
'华为设备集合
Device = Array( "CE07-HWNE40E","CE08-HWNE40E",_
"CE09-HWNE40E","CE10-HWNE40E",_
"CE11-HWNE40E","CE12-HWNE40E",_
"CE21-HWNE40E","CE22-HWNE40E",_
"CE23-HWNE40E","CE24-HWNE40E",_
"CE25-HWNE40E","CE26-HWNE40E",_
"CE29-HWNE40E","CE30-HWNE40E",_
"CE33-HWNE40E","CE34-HWNE40E",_
"CE45-HWNE40E","CE46-HWNE40E",_
"CE47-HWNE40E","CE48-HWNE40E",_
"CE49-HWNE40E","CE50-HWNE40E")'思科设备命令集合
CiscoAggreOfCommands = Array("show cdp",_
"show conf",_
"show hard",_
"show interface",_
"show ip route",_
"show conf")'华为NE40E设备命令集合
'健康检查
'告警检查
'日志检查
'单板运行状态检查
'CPU占用率检查
'内存占用率检查
'接口和链路状态以及流量检查
'NE40E接口状态检查
'OSPF邻居状态检查
'路由信息检查
NE40EAggreOfCommands = Array("display health",_
"display trapbuffer",_
"display logbuffer",_
"display device",_
"display cpu-usage",_
"display memory-usage",_
"display interface",_
"dis ospf peer",_
"display ip routing-table")
'爱立信SE800设备命令集合
'单板注册信息
'办卡加电自检
'单板运行状态检查
'检查背板运行状态
'检查主控板冗余性
'存储设备利用率
'CPU利用率
'内存利用率检查
'端口运行状态检查
'端口数据收发状态检查
'电源使用情况检查
'检查OSPF状态
SE800AggreOfCommands = Array("show chassis",_
"show diag on-demand detail",_
"show hardware",_
"show backplane-status",_
"show redundancy",_
"show disk",_
"show process",_
"show memory",_
"show port detail",_
"show port counters detail",_
"show chassis power",_
"context ChinaMobile_NGN_SG",_
"show ospf neighbor",_
"context ChinaMobile_NGN_Media",_
"show ospf neighbor")'风险命令过滤集合
ArreOfRiskCommands = Array("reb","rel")'CE汇聚网管平台登陆子函数
Function LandIPNET
screen.Send ""&chr(13)
screen.WaitForString "ogin:"
screen.Send "XXXXXXXXXX" & chr(13)
screen.WaitForString "Password:"
screen.Send "XXXXXXXXXX"& chr(13)
End Function'各厂家设备选择登陆子函数
Function LandDevice
screen.Send ""&chr(13)
screen.WaitForString "Console>>"
screen.Send "open " & Device(DeviceNumth)
ResultOfType = screen.WaitForStrings("Cisco","NE40E","SE800",4)
If ResultOfType = 1 Then
TypeOfCommand = CiscoAggreOfCommands
'CommandNumth=0
ElseIf ResultOfType = 2 Then
TypeOfCommand = NE40EAggreOfCommands
'CommandNumth=0
ElseIf ResultOfType = 3 Then
TypeOfCommand = SE800AggreOfCommands
'CommandNumth=0
Else
MsgBox "Error Type Of Device"
Do
Loop
End If
screen.Send ""&chr(13)
screen.WaitForStrings "Username:","ogin:"
screen.send "XXXXXXXXXX"& chr(13)
screen.WaitForString "Password:"
screen.Send "XXXXXXXXXX"& chr(13)
Do
Loop Until (screen.WaitForStrings("NE40E>","SE800#",5)<>False)
End Function'退出设备子函数
Function ExitDevice
If ResultOfType = 1 Then
ElseIf ResultOfType = 2 Then
screen.Send " "&"quit" & chr(13)
Do
screen.Send ""&chr(13)
Loop Until (screen.WaitForString("Console>>",1)<>False)
ElseIf ResultOfType = 3 Then
screen.Send " "&"exit" & chr(13)
Do
screen.Send ""&chr(13)
Loop Until (screen.WaitForString("Console>>",1)<>False)
Else
End If
End Function
Function ShowAll
Do while (screen.WaitForStrings("More","more",3)<>False)
crt.Screen.Send " "
Loop
End Function'风险命令过滤子函数
Function RiskCommands
Dim result
result = screen.WaitForStrings(ArreOfRiskCommands,1)
If result <> False Then
MsgBox "The command you are importing exists risk!"
Do
Loop
End If
End FunctionSub Main
crt.Screen.Synchronous = True
LandIPNET
For DeviceNumth=LBound(Device) To UBound(Device)
LandDevice
CommandNumth=0
For CommandNumth = LBound(TypeOfCommand) To UBound(TypeOfCommand)
screen.Send " "&TypeOfCommand(CommandNumth)
RiskCommands
screen.Send "" & chr(13)
ShowAll
If screen.WaitForString("More",1)<>False Then ShowAll
crt.Sleep 4000
screen.Send "" & chr(13)
Next
ExitDevice
Next
crt.Screen.Synchronous = False
End Sub