基于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)

      SecureCRT的脚本编写用VBScript和Python的比较多,因此笔者采用了VBScript。如果只是简单的脚本,不用编写,直接录制下次也可以使用。 
     注意:在使用SecureCRT连接F5时,可能会出现public-key authentication with server 失败的情况,使用quick connect也会有此情况。此时虽然点击skip可以继续,但是终究无法全自动运行。经查阅多方面资料,是由于public key鉴权失败导致,只要在session的Authentication选项里去掉 publickey即可,为了一劳永逸,需要在option——>global option——>Default Session——>Edit Default Setting中的ssh2设置中,Authentication选项里去掉publickey即可。

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 Main
	Set fso = CreateObject("Scripting.FileSystemObject")
	'存放设备信息的文件路径
	Set file = fso.OpenTextFile("E:\AutoInspect\equipmentInfo.txt")
	'窗口最大化
	crt.Window.Show 3
	Do Until file.AtEndOfStream 
		line = file.ReadLine
		commendAndLog line
	Loop
	'退出SecureCRT
	crt.Quit()

End Sub

Sub 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为文件名记录log
	crt.Session.LogFileName = "E:\AutoInspect\Logs\%H.txt"
	Call crt.Session.Log(True)
	crt.Screen.Synchronous = True
	select case equip_type
		case "FW"
			call Juniper_SRX3600Commend
			crt.screen.WaitForString ">"
		case "SW"
			call H3C_12518
			crt.screen.WaitForString ">"
		case "F5"
			call F5_2400
			crt.screen.WaitForString "#"
		case Else
			msgbox "Data Error"
	end select
	'crt.screen.WaitForString ">"
	Call crt.Session.Log(False)
	crt.session.disconnect
End Sub

Sub 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 Sub

Sub 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 Sub

Sub 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了。
基于VBS实现SecureCRT的命令自动输入和自动记录_第1张图片

    全部代码可参考我的GitHub:https://github.com/virtualzzf/AutoInspect

你可能感兴趣的:(运维技术)