SecureCRT使用VBS脚本自动备份网络设备配置,目前支持部分H3C及dptech

来源:互联网 发布:全球发生交通事故数据 编辑:程序博客网 时间:2024/04/29 19:19


根据网络上的脚本自行捣鼓了下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

 


 

原创粉丝点击