1. 程式人生 > >SecureCRT使用VBS指令碼自動備份網路裝置配置,目前支援部分H3C及dptech

SecureCRT使用VBS指令碼自動備份網路裝置配置,目前支援部分H3C及dptech


根據網路上的指令碼自行搗鼓了下crt自動備份,歡迎各網路管理員試用及提出優化建議。H3C備份方式主要為cfg通過TFTP匯出,dptech為螢幕記錄,請自行準備獨立TFTP工具。
#$language = "VBScript"
#$interface = "1.0"

Dim tftpip
Dim savedpath
Dim fso,file1,line,str1,params
Dim judgebrand '判斷裝置型別
dim configfiletype '判斷配置檔案型別
dim h3cconfig,h3cstartup' tftp備份檔案時引數
dim showrun
dim tftppath
showrun = "show running-config"

tftpname()
Set WshShell = CreateObject("WScript.Shell")
Set oExec = WshShell.Exec(tftppath) rem 啟動自定義TFTP伺服器

checkip()
checkpath() 

Sub Main

    Const ForReading = 1, ForWriting = 2, ForAppending = 8
    Set fso = CreateObject("Scripting.FileSystemObject")
 Set file1 = fso.OpenTextFile("D:\Users\administrator\Desktop\test backup vbs\list.txt",Forreading, False)       
    rem list檔案格式("IP" "使用者名稱" "密碼" "特權密碼" "裝置名稱")
 crt.Screen.Synchronous = True

 DO While file1.AtEndOfStream <> True
  line = file1.ReadLine '讀出每行
        params = Split (line) '分離每行的引數 IP地址 密碼 密碼

  'crt.session.LogFileName = savedpath & params(0) & ".txt" 螢幕文字抓取儲存路徑
  'crt.session.Log(true)  螢幕文字抓取開關      
  crt.Session.Connect "/TELNET " & params(0)        'Telnet到這個裝置上
  judgebrand = crt.Screen.WaitForStrings("H3C","cisco","dptech","Huawei-3Com",5,true)  rem 判斷裝置品牌,延遲5秒,不區分大小寫
  h3cconfig = "tftp"&chr(32)+tftpip&chr(32)+"put config.cfg"&chr(32)&params(4)+"("&params(0)+")"+".cfg"
  h3cstartup = "tftp"&chr(32)+tftpip&chr(32)+"put startup.cfg"&chr(32)&params(4)+"("&params(0)+")"+".cfg"   
  Select case judgebrand
   case 1 'h3c裝置備份
    h3cbackup()

   case 2 'cisco 裝置備份
    MsgBox "未知裝置"

   case 3  'dptech裝置備份
    crt.screen.waitForString "Username:"
    crt.Screen.Send params(1) & Chr(13)
    crt.screen.waitForString "Password:"
    crt.Screen.Send params(2) & Chr(13) 
    crt.session.LogFileName = savedpath & params(0) & ".txt" '螢幕文字抓取儲存路徑
    crt.session.Log(true)  '螢幕文字抓取開關
    if crt.screen.WaitForString(">",3,true)  then
     crt.Screen.Send showrun & Chr(13)
     srw()
    end if
   case 4
    h3cbackup()
   case else
    msgbox "出現未知品牌裝置,請從list列表中手動排除IP為" +params(0)+"及同批次裝置"
    exit sub
  End  Select
 loop
    crt.Screen.Synchronous = False

End Sub


function checkip '檢查TFTP伺服器IP地址
 tftpip = crt.Dialog.Prompt("例如192.168.1.1", "請輸入TFTP伺服器IP地址", "", false)
  If (Trim(tftpip) = "")  Or (tftpip = IDABORT) Then
   result = crt.Dialog.MessageBox("您沒有輸入tftp伺服器的IP地址或輸入的IP地址有錯誤", "提示資訊",ICON_INFO)
 checkip()  
  End If
end Function

function checkpath '通過螢幕抓取配置檔案方式儲存路徑
 savedpath = crt.Dialog.Prompt("例如d:\users\", "請輸入儲存路徑", "", false)
  If (Trim(savedpath) = "")  Or (savedpath = IDABORT) Then
   result = crt.Dialog.MessageBox("您沒有輸入儲存路徑或輸入的路徑有錯誤", "提示資訊",ICON_INFO)
 checkpath()  
  End If
end Function

function tftpname '外接TFTP程式
 tftppath = crt.Dialog.Prompt("例如d:\users\tftp.exe", "請輸tftp伺服器絕對路徑", "", false)
  If (Trim(tftppath) = "")  Or (tftppath = IDABORT) Then
   result = crt.Dialog.MessageBox("您輸入tftp路徑有錯誤", "提示資訊",ICON_INFO)
 tftpname()  
  End If
end Function

function srw()
 showrunwait = crt.screen.waitForStrings("--more--",">",3,true)  
 select case showrunwait
 case 1
  crt.Screen.Send Chr(32)
  srw()
 case else
  crt.Session.Disconnect 
 end select      
end function


function h3cbackup
 crt.screen.waitForString "Username:"
 crt.Screen.Send params(1) & Chr(13)
 crt.screen.waitForString "Password:"
 crt.Screen.Send params(2) & Chr(13) 
 if crt.screen.WaitForString(">",3,true)  then
  crt.screen.send "dir" & chr(13)& chr(13)
 end if
  configfiletype =crt.screen.waitForStrings("config.cfg","startup.cfg",3,true)
 select case configfiletype
  case 1  
   crt.screen.send h3cconfig & chr(13)
  case else  
   crt.screen.send h3cstartup & chr(13)
 end select
 crt.screen.waitForString("successfully")
 crt.screen.send Chr(13)
 crt.Session.Disconnect 
end function