(5条消息) 基于VBS实现SecureCRT的命令自动输入和自动记录
1.功能概述
目前在从事运维工作,因为看到网络设备巡检工作比较重复单调,于是萌生了写脚本将巡检命令自动输入的念头。虽然目前运维有更多高级的方法,可是SecureCRT界面简单,使用方便,仍然有其用武之地。目前网上资料并不多,在实现的过程中也遇到不少困难,因为决定写一篇文章分享一下自己的经验,给有需要的小白们一些参考。
笔者工作的场景主要使用的网络设备有Juniper SRX3600,H3C S12518,F5-2400,因此为这些设备实现了自动巡检,主要检查的项目有:
Juniper SRX 3600:
(1)机箱告警(chassis alarms)
(2)系统告警(system alarms)
(3)系统启动时间(system uptime)
(4)路由引擎(chassis routing-engine)
(5)集群状态(chassis cluster status)
(6)板卡状态(chassis fpc pic-status)
(7)电源状态(chassis environment pem)
(8)60sCPU利用率(security monitoring performance spu)
(9)CPU利用率、内存利用率、Flow session占用率(security monitoring)
S12518:
(1)CPU利用率(cpu-usage)
(2)内存利用率(memory)
(3)启动时间(version)
(4)板卡状态(device)
(5)温度(environment)
(6)告警信息(alarm)
(7)风扇状态(fan)
(8)同步情况(ntp-service status)
(9)IRF链路状态(irf link)
F5-2400:
(1)连接情况(sys performance connection)
(2)系统情况(sys performance system)
(3)吞吐量情况(sys performance throughput)
2.详细代码
#$language = "VBScript"#$interface = "1.0"crt.Screen.Synchronous = True' This automatically generated script may need to be' edited in order to work correctly.Sub MainSet fso = CreateObject("Scripting.FileSystemObject")'存放设备信息的文件路径Set file = fso.OpenTextFile("E:\AutoInspect\equipmentInfo.txt")'窗口最大化crt.Window.Show 3Do Until file.AtEndOfStream line = file.ReadLinecommendAndLog lineLoop'退出SecureCRTcrt.Quit()End SubSub commendAndLog(line)equipmentInfo = Split(line)ip = equipmentInfo(0)uname = equipmentInfo(1)pwd = equipmentInfo(2)equip_type = equipmentInfo(3)Call crt.Session.Log(False)'登录设备crt.session.ConnectInTab "/SSH2 /L "&uname&" /PASSWORD "&pwd&" "&ip'以IP为文件名记录logcrt.Session.LogFileName = "E:\AutoInspect\Logs\%H.txt"Call crt.Session.Log(True)crt.Screen.Synchronous = Trueselect case equip_typecase "FW"call Juniper_SRX3600Commendcrt.screen.WaitForString ">"case "SW"call H3C_12518crt.screen.WaitForString ">"case "F5"call F5_2400crt.screen.WaitForString "#"case Elsemsgbox "Data Error"end select'crt.screen.WaitForString ">"Call crt.Session.Log(False)crt.session.disconnectEnd SubSub Juniper_SRX3600Commend()crt.screen.WaitForString ">"crt.Screen.Send "show chassis alarms | no-more" & chr(13)crt.screen.WaitForString ">"crt.Screen.Send "show system alarms | no-more" & chr(13)crt.screen.WaitForString ">"crt.Screen.Send "show system uptime | no-more" & chr(13)crt.screen.WaitForString ">"crt.Screen.Send "show chassis routing-engine | no-more" & chr(13)crt.screen.WaitForString ">"crt.Screen.Send "show chassis cluster status | no-more" & chr(13)crt.screen.WaitForString ">"crt.Screen.Send "show chassis fpc pic-status | no-more" & chr(13)crt.screen.WaitForString ">"crt.Screen.Send "show chassis environment pem | no-more" & chr(13)crt.screen.WaitForString ">"crt.Screen.Send "show security monitoring performance spu | no-more" & chr(13)crt.screen.WaitForString ">"crt.Screen.Send "show security monitoring | no-more" & chr(13)End SubSub H3C_12518()crt.screen.WaitForString ">"crt.Screen.Send "screen-length disable" & chr(13)crt.screen.WaitForString ">"crt.Screen.Send "display cpu-usage" & chr(13)crt.screen.WaitForString ">"crt.Screen.Send "display memory" & chr(13)crt.screen.WaitForString ">"crt.Screen.Send "display version" & chr(13)crt.screen.WaitForString ">"crt.Screen.Send "display device" & chr(13)crt.screen.WaitForString ">"crt.Screen.Send "display environment" & chr(13)crt.screen.WaitForString ">"crt.Screen.Send "display alarm" & chr(13)crt.screen.WaitForString ">"crt.Screen.Send "display fan" & chr(13)crt.screen.WaitForString ">"crt.Screen.Send "display ntp-service status" & chr(13)crt.screen.WaitForString ">"crt.Screen.Send "display irf link" & chr(13)crt.screen.WaitForString ">"crt.Screen.Send chr(13)End SubSub F5_2400()crt.screen.WaitForString "#"crt.Screen.Send "tmsh" & chr(13)crt.screen.WaitForString "#"crt.Screen.Send "show sys performance connection" & chr(13)crt.screen.WaitForString "#"crt.Screen.Send "show sys performance system" & chr(13)crt.screen.WaitForString "#"crt.Screen.Send "show sys performance throughput" & chr(13)crt.screen.WaitForString "#"crt.Screen.Send chr(13)End Sub
其中equipmentInfo文件格式如下,为IP 用户名 密码 设备类型,各字段之间以空格分隔。设定好设备信息文件路径和log文件路径之后,运行脚本,就可以得到各个设备巡检结果的log了。
全部代码可参考我的GitHub:https://github.com/virtualzzf/AutoInspect