搜档网
当前位置:搜档网 › SecureCRT自动备份网络设备配置

SecureCRT自动备份网络设备配置

使用前请自备tftp服务端软件,命名为tftp.exe与本脚本内容放置于同一目录。
securecrt 测试环境为Version 6.5.0 (build 380)版本,低于此版本会出现脚本兼容性问题

自行编写list.txt文件放置于脚本同一目录。
list.txt文件格式如下,用空格区分每列
1ip地址 2用户名 3密码 4特权密码(cisco设备填写,没有随意填写) 5设备名称(保存文件时需要如不确定建议同IP地址,也可随意填写)
10.38.200.100 admin YTCC2016 enablepass YNPHOS_S7606R_SC_1
10.38.200.101 admin YTCC2016 enablepass YNPHOS_S7606R_SC_2



支持H3C config.cfg文件名 startup.cfg文件名
支持huawei vrpcfg.zip文件名
支持dptech无固定格式,以屏幕抓取方式保存
支持锐捷User Access Verification欢迎界面设备

在securecrt中点击脚本——执行——选中*.vbs文件
第一步输入脚本程序路径如:d:\abc\
第二步输入TFTP服务器IP地址,如1.1.1.1


-------------------------以下为脚本正式内容------------------------------------------------


#$language = "VBScript"
#$interface = "1.0"

Dim tftpip
Dim savedpath
Dim fso,file1,line,str1,params
Dim judgebrand '判断设备类型
dim configfiletype '判断配置文件类型
dim h3cconfig,h3cstartup,huaweivrp' tftp备份文件时参数
dim showrun
dim tftppath
dim h3cversion,dptechversion '设备登陆后根据username or login 判断下一执行条件
showrun = "show running-config"
ruijietext = "copy"&chr(32) + "startup-config" &chr(32)+ "tftp:"
checkpath()
checkip()
tftppath = savedpath& "\tftp.exe"

Set WshShell = CreateObject("WScript.Shell")
Set oExec = WshShell.Exec(tftppath) rem 启动自定义TFTP服务器

Sub Main

Const ForReading = 1, ForWriting = 2, ForAppending = 8
Set fso = CreateObject("Scripting.FileSystemObject")
Set file1 = fso.OpenTextFile(savedpath & "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","Huawei-3Com","dptech","Login authentication","User Access Verification",5,true) rem 判断设备品牌,延迟5秒,不区分大小写
h3cconfig = "tftp"&chr(32)+tftpip&chr(32)+"put config.cfg"&chr(32)¶ms(4)+"("¶ms(0)+")"+".cfg"
h3cstartup = "tftp"&chr(32)+tftpip&chr(32)+"put startup.cfg"&chr(32)¶ms(4)+"("¶ms(0)+")"+".cfg"
huaweivrp = "tftp"&chr(32)+tftpip&chr(32)+"put vrpcfg.zip"&chr(32)¶ms(4)+"("¶ms(0)+")"+".zip"

Select case judgebrand
c

ase 1 'h3c设备备份
h3cbackup()
case 2 '老华三
h3cbackup()

case 3 'dptech设备备份
dptechbackup()

case 4 '华为设备备份
huaweibackup()
case 5 '锐捷设备
ruijiebackup()
case else
msgbox "出现未知品牌设备,请从list列表中手动排除IP为" +params(0)+"及同型号设备"
exit sub
End Select
loop
crt.Screen.Synchronous = False

End Sub

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


function srw2()
crt.sleep 1000
showrunwait = crt.screen.waitForStrings("--More(CTRL+C break)--",">",3,true)
select case showrunwait
case 1
crt.Screen.Send Chr(32)
srw2()
case else
crt.Session.Disconnect
end select
end function

function ruijiebackup()
crt.Screen.Send params(1) & Chr(13)
crt.screen.waitForString "Password:"
crt.Screen.Send params(2) & Chr(13)
crt.screen.WaitForString ">"
crt.screen.send "en" & Chr(13)
crt.screen.waitForString "Password:"
crt.Screen.Send params(3) & Chr(13)
crt.screen.WaitForString"#"
crt.screen.send ruijietext & chr(13)
crt.screen.waitForString "Address of remote host []?"
crt.screen.send tftpip & chr(13)
crt.screen.waitForString "Destination filename [config.text]"
crt.screen.send params(4)+"("¶ms(0)+")"+".text" & chr(13)
crt.screen.waitForString("Transmission finished")
crt.screen.send Chr(13)
crt.Session.Disconnect
end function

function dptechbackup
dptechversion = crt.Screen.WaitForStrings("Username:","login:",5,true)
select case dptechversion
case 1
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)
srw1()
end if
case 2
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)
srw2()
end if
end select
end function



function h3cbackup
h3cversion = crt.Screen.WaitForStrings("Username:","login:",5,true) rem 判断H3C版本username一般是V3和V5,login是V7版本
Select case h3cversion
case 1
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)
case else
crt.Screen.Send params(1) & Chr(13)
crt.screen.waitForString "Password:"
crt.Screen.Send params(2) & Chr(13)
crt.screen.WaitForString">"
crt.screen.send h3cstartup & chr(13)
crt.screen.waitForString("successfully")
crt.screen.send Chr(13)
end select
crt.Session.Disconnect
end function

function huaweibackup
crt.Screen.Send params(1) & Chr(13)
crt.screen.waitForString "Password:"
crt.Screen.Send params(2) & Chr(13)
crt.screen.WaitForString">"
crt.screen.send huaweivrp & chr(13)
crt.screen.waitForString("successfully")
crt.screen.send Chr(13)
crt.Session.Disconnect
end function

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

REM function tftpname '绝对路径方式获取TFTP路径。现已更新脚本逻辑,此函数无用
REM tftppath = crt.Dialog.Prompt("例如d:\users\tftp.exe", "请输tftp服务器绝对路径", "", false)
REM If (Trim(tftppath) = "") Or (tftppath = IDABORT) Then
REM result = crt.Dialog.MessageBox("您输入tftp路径有错误", "提示信息",ICON_INFO)
REM tftpname()
REM End If
REM end Function

相关主题