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

# $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

  • 1
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
SecureCRT是一款功能强大的终端仿真软件,可以用于远程管理网络设备。它支持自定义脚本编写,以便自动执行一系列任务,提升工作效率。 SecureCRTVBScript脚本语言是一种基于微软Visual Basic的脚本语言,可以通过脚本编写复杂的自动化任务。VBScript脚本提供了丰富的方法和属性,可以利用SecureCRT的API来实现各种操作,如发送命令、接收返回值、处理文本等。 通过VBScript脚本,可以实现以下一些功能: 1. 自动登录:可以编写脚本,在连接设备后自动输入用户名和密码,实现自动登录。 2. 批量执行命令:可以编写脚本,实现批量执行命令的功能,从而同时在多台设备上执行相同的操作。 3. 数据采集:可以编写脚本,在设备上执行命令并将返回结果保存为文件,以便后续分析和处理。 4. 自动配置:可以编写脚本,根据特定的规则自动配置网络设备,提高配置的准确性和一致性。 5. 错误处理:可以编写脚本,检测和处理命令执行过程中可能出现的错误,实现错误提示和自动重试等机制。 通过使用SecureCRTVBScript脚本,用户可以根据自己的需求定制各种任务和功能,提高工作效率,简化操作流程。但是需要注意的是,编写脚本需要一定的编程知识和经验,对SecureCRT的API也需要一定的了解。因此,对于初学者来说,可能需要花费一定的时间学习和实践,才能熟练地使用SecureCRTVBScript脚本功能。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值