自动备份华为交换机配置的SecureCRT的脚本
昨天升级了40多台华为交换机,幸亏有SecureCRT的快捷键(下篇文章另行介绍),要不然几个小时也搞
不定。
升级完后,对SecureCRT的脚本产生了兴趣,能不能让它自动执行诸如读取、备份配置,升级等操作呢?
从网上找了几个脚本,拼凑到一起,还真行,作了如下的备份交换机配置的脚本:
# $language = "VBScript"
# $interface = "1.0"
' This script shows how to read in a file, and it demonstrates how to' perform some
preprocessing on data (splitting the file data into ' separate strings) before sending it
to a server.
' Constant used by OpenTextFile()Const ForReading = 1
Sub main
' Open a file, read it in & send it one line at a time
Dim fso, f
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.OpenTextFile("d:/name.txt", ForReading, 0)
Dim line, params Do While f.AtEndOfStream <> True
' Read each line of the printers file. ' line = f.Readline
' Split the line up. Each line should contain 3 space-separated parameters params =
Split( line )
' params(0) holds parameter 1, params(1) holds parameter 2, etc. ' ' Send
"mycommand" with the appended parameters from the file with ' an appended CR. '
crt.Screen.Send "telnet " & params(0) & " " & vbCR 'wait for switch prompt
crt.Screen.WaitForString "Username:" crt.Screen.Send "admin" & VbCr ' Wait for a
tring that looks like "password: " or "Password: " crt.Screen.WaitForString "Password:"
' Send your password followed by a carriage return crt.Screen.Send "admin" & VbCr
crt.Screen.WaitForString ">" crt.Screen.Send "su" & VbCr crt.Screen.WaitForString
"Password:" crt.Screen.Send "huawei" & VbCr crt.Screen.WaitForString ">" '
execute some commands crt.Screen.Send "tftp 172.16.1.250 put vrpcfg.cfg " & params(0) &
".txt " & VbCr crt.Screen.Send "quit" & VbCr 'leave out switch,stay in linux
crt.Screen.WaitForString "#", 3 ' Cause a 3-second pause between sends by waiting for
something "unexpected" ' with a timeout value. Loop
End Sub
其中,name.txt文件就是一行一个交换机的IP地址,如:
172.16.1.1
172.16.1.2
脚本从该文件中读取一行,以params(0)的变量形式引入到脚本中,执行命令,然后循环读取下一行。
为了不让交换机在执行中产生排错行或者排错字符等,都是等华为交换机返回提示符后才输入下一条命
令。
由于初始登陆的终端是Linux的,所以交换机执行完“quit”后,会退到Linux的提示符#下,等待3秒后
执行下一个循环。
以前研究过Cisco设备下,用Perl脚本来执行批处理,那是用的net::telnet::cisco模块,等有空是用
net::telnet作个华为的脚本试试。