backup_v4_SSH_TELNET.bat
@echo off
color 0a
title 备份交换机配置
rem 调整时间格式为:14:00:00
set time1=%time:~0,8%
rem 调整日期格式为:2017-09-01
set y=%date:~0,4%
set m=%date:~5,2%
set d=%date:~8,2%
set date1=%y%-%m%-%d%
rem 2.设置配置备份目录路径
set dir=backup_%y%-%m%-%d%
rem 3.自动计算需要备份的设备数量,即SWIP.txt文件的行数
set "file=SWIP_v4.txt"
for /f "delims=" %%a in (%file%) do set/a rows+=1
rem 4.命令窗口打印显示以下信息
echo.
echo ********************************************
echo 网络设备配置备份
echo ********************************************
echo 备份日期:%date1% %time1%
echo 设备数量:%rows%
echo 备份目录:%dir%
echo ********************************************
rem 5.启动CRT软件,并执行配置备份.vbs脚本
start "" "E:\SecureCRT.exe" /SCRIPT backup_v4_SSH_TELNET.vbs >nul
rem 6.预计备份时间
rem 通过多次测试,预计每台设备备份时间为5秒钟(不同设备收集配置时间可能不一样),这个时间乘以要备份的设备数量,就是总时间
set /a rows2=%rows%*5
echo 预计备份时间 %rows2% 秒
rem 7.进度条,纯属为了界面动态美观(可忽略)
rem echo 正在备份,请稍候
rem 进度条1
set /p= 正在备份,请稍候<nul
set /a a=1
:a
set /a a+=1
set /p=.<nul
rem for %%a in ( ">") do (set /p=%%a<nul &ping-n 2 127.1>nul)
ping -n 2 127.1>nul
if not %a%==%rows2% goto a
rem 8.备份完成后,结束CRT进程
taskkill /f /im SecureCRT.exe >nul
echo.
rem 9.统计配置文件数量,即备份完成后备份目录里的文件数量
set n=0
for /f "delims=" %%a in ('dir /a-d /b /s "%dir%" ') do (
set /a n+=1
)
rem 10.指定备份完成时时间
set time2=%time:~0,8%
rem 11.命令窗口显示
echo ********************************************
echo 备份完成,共 %rows% 台设备,已完成 %n% 台设备
echo 备份完成时间: %date1% %time2%
echo ********************************************
echo.
rem 12.为了方便查看备份记录,将此次的备份情况另外输出到日志文件
echo.>>swbackup.log
echo.>>swbackup.log
echo============================================>>swbackup.log
echo 备份开始时间:%date1% %time1%>>swbackup.log
echo 备份完成时间:%date1% %time2%>>swbackup.log
echo 设备 数量:%rows% >>swbackup.log
echo 当前备份数量:%n%>>swbackup.log
echo 备份 目录:%dir%>>swbackup.log
echo============================================>>swbackup.log
rem 13.BAT配置备份结束
backup_v4_SSH_TELNET.vbs
# $language = "VBScript"
# $interface = "1.0"
'从一个文件里面自动读取设备IP地址,用户名,密码,并创建日志文件,为记录配置信息做准备
Sub Main
'打开保存设备管理地址以及密码的文件
Const ForReading = 1,ForWriting = 2, ForAppending = 8
Dim fso,file1,line,str1,params
Set fso =CreateObject("Scripting.FileSystemObject")
Set file1 =fso.OpenTextFile("SWIP_v4.txt",Forreading,False)
crt.Screen.Synchronous= True
mydate = now()
myYear=Year(mydate)
myMon=Month(mydate)
if len(myMon)<2 then myMon="0"&myMon
myDay=Day(mydate)
if len(myDay)<2 then myDay="0"&myDay
backupDate = myYear & "-" & myMon & "-" &myDay
DO While file1.AtEndOfStream <> True
'读出每行
line =file1.ReadLine
'分离每行的参数设备名称、IP地址、用户名、密码,然后创建"设备名称+IP"为名的txt文本,作为配置记录文件
params =Split(line)
crt.session.LogFileName= "backup_" &backupDate & "\"& params(1) &"_" & params(2) &".txt"
call crt.session.Log(False)
call crt.session.Log(True) '开启日志
crt.Screen.Synchronous= True '开启屏幕同步
If params(0) ="ssh" Then
'判断设备登录类型ssh
'首先ssh到这个设备上,params(2)为文件中提取的设备IP,params(3)为用户名,params(4)为密码
crt.Session.Connect "/ssh2 /L " & params(3) &" /PASSWORD " & params(4) &" " & params(2)
elseif params(0) ="telnet" Then
'判断设备登录类型telnet
'首先telnet到这个设备上,params(2)为文件中提取的设备IP
crt.Session.Connect"/TELNET " & params(2)
'自动输入telnet用户名
'crt.Screen.WaitForString"Username:"
'crt.Screen.Sendparams(3) & vbcr
'自动输入telnet密码
crt.Screen.WaitForString"Password:"
crt.Screen.Send params(4) & vbcr
crt.Screen.Send chr(13)
end if
'收集设备运行信息
str1 ="display environment" & vbcr
crt.Screen.Send str1
'收集版本信息
str2 ="display version"
crt.Screen.Send str2 & vbcr
crt.Screen.Send " " & chr(13)
'crt.Screen.WaitForString"More"
'crt.Screen.Send " " & vbcr
'备份h3c配置
str3 ="dis cu"
crt.Screen.Send str3 & vbcr
crt.Screen.WaitForString"More"
crt.Screen.Send " " & vbcr
'备份完成后退出
crt.Screen.waitForString"return"
crt.Session.Disconnect()
loop
Call crt.Session.Log(False)
crt.Screen.Synchronous= False
End Sub