# $language = "VBScript" # $interface = "1.0" '先定義各廠商設備的備份腳本模塊 '定義華爲腳本內容 Function Huawei_DIS '執行dis cur命令 crt.Sleep 1000 crt.screen.send Chr(13) crt.Sleep 1000 crt.Screen.Send "system" & Chr(13) crt.Screen.Send "user-interface vty 0 4" & Chr(13) crt.Screen.Send "screen-length 0" & Chr(13) crt.Screen.Send "display current-configuration" & Chr(13) crt.Screen.waitForString "vty0-4]" crt.Screen.Send "undo screen-length" & Chr(13) crt.Screen.Send "return" & Chr(13) crt.Screen.waitForString ">" '執行 save命令 crt.Sleep 1000 crt.screen.send Chr(13) crt.Screen.Send "save" & Chr(13) crt.Sleep 1000 crt.Screen.Send "Y" & Chr(13) crt.Sleep 1000 crt.screen.send Chr(13) crt.Sleep 1000 crt.Screen.Send "Y" & Chr(13) crt.Screen.waitForString "success" '華爲防火牆必須等待主牆同步保存到備牆完畢後再進行備牆備份配置,所以增長等待「>」 crt.Screen.waitForString ">" '斷開鏈接 crt.Session.Disconnect End Function '定義華爲vsys腳本內容,華爲虛擬牆 Function Huawei_vsys_DIS (vsys) '執行dis cur命令 crt.Sleep 1000 crt.screen.send Chr(13) crt.Sleep 1000 crt.Screen.Send "system" & Chr(13) crt.Screen.Send "switch vsys " & vsys & Chr(13) crt.Screen.Send "display current-configuration" & Chr(13) crt.Screen.Send " " Do While (crt.Screen.WaitForString ("---- More ----",1)<>False) crt.Screen.Send " " Loop crt.screen.send Chr(13) crt.Screen.waitForString ">" '執行 save命令 crt.Sleep 1000 crt.screen.send Chr(13) crt.Screen.Send "save" & Chr(13) crt.Sleep 1000 crt.Screen.Send "Y" & Chr(13) crt.Sleep 1000 crt.Screen.Send "Y" & Chr(13) crt.Screen.waitForString "success" '華爲防火牆必須等待主牆同步保存到備牆完畢後再進行備牆備份配置,所以增長等待「>」 crt.Screen.waitForString ">" '斷開鏈接 crt.Session.Disconnect End Function '定義H3C腳本內容 Function H3C_DIS '執行dis cur命令 crt.Sleep 1000 crt.screen.send Chr(13) crt.Sleep 1000 crt.Screen.Send "screen-length disable" & Chr(13) crt.Screen.Send "display current-configuration" & Chr(13) crt.Screen.waitForString ">" '執行 save命令 crt.Sleep 1000 crt.screen.send Chr(13) crt.Screen.Send "save force" & Chr(13) crt.Sleep 1000 crt.Screen.waitForString "success" crt.Screen.waitForString ">" '斷開鏈接 crt.Session.Disconnect End Function '定義ZTE腳本內容 Function ZTE_SHOW '執行show run命令 crt.Sleep 1000 crt.screen.send Chr(13) crt.Sleep 1000 crt.Screen.Send "show run" & Chr(13) crt.Sleep 1000 crt.Screen.Send " " Do While (crt.Screen.WaitForString ("--More--",1)<>False) crt.Screen.Send " " Loop crt.screen.send Chr(13) crt.Sleep 1000 crt.screen.send Chr(13) crt.Sleep 1000 crt.Screen.Send "write" & Chr(13) crt.Sleep 1000 crt.Screen.waitForString "OK",3 crt.Screen.waitForString "Success",3 crt.Screen.waitForString "#" '斷開鏈接 crt.Session.Disconnect End Function '定義A10腳本內容 Function A10_SHOW '執行show run命令 crt.Sleep 1000 crt.screen.send Chr(13) crt.Sleep 1000 crt.Screen.Send "enable" & Chr(13) & Chr(13) crt.Screen.Send "show running-config all-partitions" & Chr(13) crt.Sleep 1000 crt.Screen.Send " " Do While (crt.Screen.WaitForString ("--MORE--",1)<>False) crt.Screen.Send " " Loop crt.screen.send Chr(13) crt.Screen.waitForString "#" '斷開鏈接 crt.Session.Disconnect End Function '定義ISCOM腳本內容 Function ISCOM_SHOW '執行show run命令 crt.Sleep 1000 crt.screen.send Chr(13) crt.Sleep 1000 crt.Screen.Send "show run" & Chr(13) 'crt.Sleep 1000 crt.Screen.Send " " Do While (crt.Screen.WaitForString ("--More--",1)<>False) crt.Screen.Send " " Loop crt.Sleep 1000 crt.screen.send Chr(13) crt.Sleep 1000 crt.Screen.Send "write" & Chr(13) crt.Sleep 1000 crt.Screen.waitForString "success" crt.Screen.waitForString "#" '斷開鏈接 crt.Session.Disconnect End Function '定義FiberHome腳本內容 Function FiberHome_SHOW '執行show run命令 crt.Sleep 1000 crt.screen.send Chr(13) crt.Sleep 1000 crt.Screen.Send "enable" & Chr(13) crt.Screen.Send "show running-config" & Chr(13) 'crt.Sleep 1000 crt.Screen.Send " " Do While (crt.Screen.WaitForString ("--More--",1)<>False) crt.Screen.Send " " Loop crt.Screen.Send Chr(13) crt.Screen.Send "write" & Chr(13) crt.Sleep 1000 crt.Screen.waitForString "OK" crt.Screen.waitForString "#" '斷開鏈接 crt.Session.Disconnect End Function '定義F5腳本內容 Function F5_SHOW '執行SHOW RUN命令 crt.Sleep 1000 crt.screen.send Chr(13) crt.Sleep 1000 crt.Screen.Send "tmsh" & chr(13) crt.Sleep 1000 crt.Screen.Send chr(13) crt.Sleep 1000 crt.Screen.Send "show running-config"& chr(13) crt.Sleep 1000 crt.Screen.Send "y" & chr(13) Do While (crt.Screen.WaitForString ("--(less ",1)<>False) crt.Screen.Send " " '保證效果,多一次空格 crt.Screen.Send " " crt.Screen.Send Chr(13) Loop crt.Sleep 1000 crt.screen.send Chr(13) crt.Sleep 1000 crt.Screen.waitForString "(END)" '斷開鏈接 crt.Session.Disconnect End Function '定義TOPSEC腳本內容 Function TOPSEC_SHOW '執行SHOW RUN命令 crt.screen.send Chr(13) crt.Sleep 1000 crt.Screen.Send "show-running nostop" & chr(13) crt.Sleep 1000 crt.Screen.waitForString "system config implement" crt.Sleep 1000 crt.Screen.Send chr(13) '保存配置 crt.Screen.Send "save" & chr(13) crt.Screen.waitForString "save config success" crt.Sleep 1000 crt.screen.send "####" & Chr(13) '輸入###爲了log記錄完整日誌 '斷開鏈接 crt.Screen.waitForString "####" crt.Session.Disconnect End Function '定義Forti腳本內容 Function Forti_SHOW '執行SHOW RUN命令 crt.screen.send Chr(13) crt.Sleep 1000 crt.Screen.Send "show full-configuration" & chr(13) crt.Sleep 1000 crt.Screen.Send " " Do While (crt.Screen.WaitForString ("--More--",1)<>False) crt.Screen.Send " " Loop crt.Screen.Send chr(13) '保存配置 crt.Sleep 1000 crt.screen.send "####" & Chr(13) '輸入###爲了log記錄完整日誌 '斷開鏈接 crt.Screen.waitForString "####" crt.Session.Disconnect End Function 'SSH2登陸方式 Function SSH_2(usr,psw,ip) crt.Session.Connect "/SSH2 /ACCEPTHOSTKEYS /L " & usr & " /PASSWORD " & psw & " /C 3DES /M SHA1 " & ip End Function 'SSH1登陸方式 Function SSH_1(usr,psw,ip) crt.Session.Connect "/SSH1 /ACCEPTHOSTKEYS /L " & usr & " /PASSWORD " & psw & " /C 3DES /M MD5 " & ip End Function 'TELNET登陸方式 Function TELNET(usr,psw,ip) crt.Session.Connect "/TELNET " & ip '輸入用戶名 crt.screen.WaitForString "Username:",1 crt.screen.send usr & Chr(13) '輸入密碼 crt.Screen.WaitForString "Password:" crt.Screen.Send psw & Chr(13) End Function Function SSH2_F5(usr,psw,ip) 'F5的SSH登錄設備SHA2模式keyboard-interactive crt.Session.Connect "/SSH2 /ACCEPTHOSTKEYS /auth keyboard-interactive /L " & usr & " /PASSWORD " & psw & " /C 3DES /M SHA1 " & ip End Function '進入主功能程序 '讀取設備信息文件並CRT登陸 Function GET_list_read(list_path,log_path) Const ForReading = 1, ForWriting = 2, ForAppending = 8 'ForReading 1 打開一個只讀文件。不能對此文件進行寫操做 'ForWriting 2 打開一個可讀寫操做的文件,並刪除原有文本內容 'ForAppending 8 打開一個文件並寫到文件的尾部 Dim fso,file1,line,str1,params 'FSO是FileSystemObject,用於操做磁盤、文件夾或文本文件 Set fso = CreateObject("Scripting.FileSystemObject") '設備登陸信息文件路徑,.OpenTextFile打開並讀文件,第二個參數1表示只讀打開,第三個參數表示目標文件不存在時是否建立 Set file1 = fso.OpenTextFile(list_path,Forreading, False) '同步打印在CRT上 crt.Screen.Synchronous = True Dim i '定義一個值,並賦值0,爲了下面讀取文本時跳過首行 i=0 Do While file1.AtEndOfStream <> True '.AtEndOfStream若是文件指針位於 TextStream 文件末,則返回 True;不然若是不爲只讀則返回 False line = file1.ReadLine '讀取每一行 'readline是讀取一行數據返回字串類型 '列1爲日誌名稱,列2爲設備IP,列3爲登陸帳戶,列4爲登陸密碼,列5位廠商名稱,列5爲登陸方式 i=i+1 'ReadLine讀取一行後數值加1 If i>1 Then '若是i>1則進行數值讀取和CRT操做,所以'ReadLine第一次讀取不作操做,跳過了首行 params = Split (line,",")'每行讀取的數值存到數組params裏,每行以逗號分隔,這是CSV的標準格式 the_class=params(0) the_devname=params(1) the_ip=params(2) the_usr=params(3) the_psw=params(4) the_vendor=params(5) the_login_type=params(6) the_huawei_vsys=params(7) crt.session.LogFileName = log_path & the_class & "\" & the_devname & ".log" '記錄日誌路徑=輸入的路徑+the_class,文件名稱the_devname.log crt.session.Log(true) 'ssh登陸 If the_login_type="SSH2" Then SSH_2 the_usr,the_psw,the_ip ElseIf the_login_type="SSH1" Then SSH_1 the_usr,the_psw,the_ip ElseIf the_login_type="SSH2-F5" Then SSH2_F5 the_usr,the_psw,the_ip Else TELNET the_usr,the_psw,the_ip End If If the_vendor="Huawei" Then '若是設備類型是Huawei,執行華爲的命令腳本 Huawei_DIS ElseIf the_vendor="Huawei_vsys" Then '若是設備類型是Huawei,執行華爲的命令腳本 Huawei_vsys_DIS(the_huawei_vsys) ElseIf the_vendor="H3C" Then '若是設備類型是H3C,執行H3C的命令腳本 H3C_DIS ElseIf the_vendor="ZTE" Then '若是設備類型是ZTE,執行ZTE的命令腳本 ZTE_SHOW ElseIf the_vendor="ISCOM" Then '若是設備類型是ISCOM,執行ISCOM的命令腳本 ISCOM_SHOW ElseIf the_vendor="F5" Then '若是設備類型是F5,執行F5的命令腳本 F5_SHOW ElseIf the_vendor="FiberHome" Then '若是設備類型是FiberHome,執行FiberHom的命令腳本 FiberHome_SHOW ElseIf the_vendor="A10" Then '若是設備類型是A10,執行A10的命令腳本 A10_SHOW ElseIf the_vendor="TOPSEC" Then '若是設備類型是TOPSEC,執行TOPSEC的命令腳本 TOPSEC_SHOW ElseIf the_vendor="Forti" Then '若是設備類型是Forti,執行A10的命令腳本 Forti_SHOW Else MsgBox("沒用定義"&the_vendor&"這個設備類型!") End If End If Loop crt.Screen.Synchronous = False End Function Sub Main GET_list_read "D:\配置備份\設備列表\設備列表.csv","D:\配置備份\日誌\" End Sub
設備列表格式爲csv格式,即文本格式的表格:
最後一列能夠無,即刪除 the_huawei_vsys=params(7)這個代碼,這個是由於我本身這裏華爲虛擬牆須要備份
設備分類 設備名稱 設備IP 用戶名 密碼 廠商 登錄方式 華爲vsys
XX XXX X.X.X.X XXX XXXX TOPSEC SSH2 none數組