您的位置:首页 > 理论基础 > 计算机网络

SecureCrt VBS自动登录备份网络设备配置脚本

2019-10-15 20:46 3651 查看
# $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

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: