SecureCrt VBS自動登錄備份網絡設備配置腳本

# $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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章