1.功能概述
目前在從事運(yùn)維工作,因?yàn)榭吹骄W(wǎng)絡(luò)設(shè)備巡檢工作比較重復(fù)單調(diào),于是萌生了寫腳本將巡檢命令自動輸入的念頭。雖然目前運(yùn)維有更多高級的方法,可是SecureCRT界面簡單,使用方便,仍然有其用武之地。目前網(wǎng)上資料并不多,在實(shí)現(xiàn)的過程中也遇到不少困難,因?yàn)闆Q定寫一篇文章分享一下自己的經(jīng)驗(yàn),給有需要的小白們一些參考。
筆者工作的場景主要使用的網(wǎng)絡(luò)設(shè)備有Juniper SRX3600,H3C S12518,F(xiàn)5-2400,因此為這些設(shè)備實(shí)現(xiàn)了自動巡檢,主要檢查的項(xiàng)目有:
Juniper SRX 3600: (1)機(jī)箱告警(chassis alarms) (2)系統(tǒng)告警(system alarms) (3)系統(tǒng)啟動時(shí)間(system uptime) (4)路由引擎(chassis routing-engine) (5)集群狀態(tài)(chassis cluster status) (6)板卡狀態(tài)(chassis fpc pic-status) (7)電源狀態(tài)(chassis environment pem) (8)60sCPU利用率(security monitoring performance spu) (9)CPU利用率、內(nèi)存利用率、Flow session占用率(security monitoring) S12518: (1)CPU利用率(cpu-usage) (2)內(nèi)存利用率(memory) (3)啟動時(shí)間(version) (4)板卡狀態(tài)(device) (5)溫度(environment) (6)告警信息(alarm) (7)風(fēng)扇狀態(tài)(fan) (8)同步情況(ntp-service status) (9)IRF鏈路狀態(tài)(irf link) F5-2400: (1)連接情況(sys performance connection) (2)系統(tǒng)情況(sys performance system) (3)吞吐量情況(sys performance throughput)
SecureCRT的腳本編寫用VBScript和Python的比較多,因此筆者采用了VBScript。如果只是簡單的腳本,不用編寫,直接錄制下次也可以使用。
注意:在使用SecureCRT連接F5時(shí),可能會出現(xiàn)public-key authentication with server 失敗的情況,使用quick connect也會有此情況。此時(shí)雖然點(diǎn)擊skip可以繼續(xù),但是終究無法全自動運(yùn)行。經(jīng)查閱多方面資料,是由于public key鑒權(quán)失敗導(dǎo)致,只要在session的Authentication選項(xiàng)里去掉
publickey即可,為了一勞永逸,需要在option——>global option——>Default Session——>Edit Default Setting中的ssh2設(shè)置中,Authentication選項(xiàng)里去掉publickey即可。
2.詳細(xì)代碼
#$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")
'存放設(shè)備信息的文件路徑
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)
'登錄設(shè)備
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 用戶名 密碼 設(shè)備類型,各字段之間以空格分隔。設(shè)定好設(shè)備信息文件路徑和log文件路徑之后,運(yùn)行腳本,就可以得到各個設(shè)備巡檢結(jié)果的log了。
全部代碼可參考我的GitHub:https://github.com/virtualzzf/AutoInspect
|