- 论坛徽章:
- 0
|
用securecrt的脚本功能对网络设备自动操作
经常遇到需要对一批网络设备进行同一种操作的情况,比如备份,或者查找arp表等,用telnet一个个登上去看确实比较麻烦,于是想到是否有自动化的办法来做,编程序太麻烦改起来不方便,每天都在用securecrt这个软件做telnet客户端,有一次无意间在软件目录下发现一些脚本,原来securecrt也可以用脚本,于是有了下面这个脚本,用来自动登录到设备上去执行某个操作,并把屏幕内容记录到日志文件中供进一步处理。
DOS批处理loginall.bat:
for /f "tokens=1,2,3,4 delims= " %%i in (hostlist.txt) do securecrt /arg %%i /arg %%j /arg %%k /arg %%l /script login1.vbs
其中hostlist.txt是交换机的ip,用户名,登录密码,enable密码
举例:
172.22.1.253 user 123 123
172.22.2.253 user 123 123
172.22.3.253 user 123 123
login1.vbs是securecrt的脚本程序,仿照例子改的
#$language = "VBScript"
#$interface = "1.0"
Sub main
on error resume next
Dim passwd
' turn on synchronous mode so we don't miss any data
crt.Screen.Synchronous = True
If crt.Arguments.Count <> 4 Then
MsgBox "错误:参数个数不等于4"
Exit Sub
End If
'MsgBox "ip: " & crt.Arguments(0)
'MsgBox "username: " & crt.Arguments(1)
'MsgBox "loginpswd: " & crt.Arguments(2)
'MsgBox "enpswd: " & crt.Arguments(3)
dim ip,username,loginpswd,enpswd
ip=crt.Arguments(0)
username=crt.Arguments(1)
loginpswd =crt.Arguments(2)
enpswd =crt.Arguments(3)
'
' crt.Session.Connect "/s 172.20.3.253"
' Wait for my unix login prompt or for 5 seconds whichever
' comes first.
crt.Session.Connect "/TELNET "& ip &" 23"
if Err.Number <> 0 Then MsgBox "An error occurred: " & Err.Description
' handle the error here
' crt.Screen.WaitForString "linux$", 5
' Set the name of the log file name "YYMMDD.log"
Dim logfile
logfile = "C:\Log%H-%Y%M%D%h%m.log"
'这是日志文件名,可以按规律自动命名
' logfile = "C:\Log%Y%M%D%h.log"
crt.Session.LogFileName = logfile
' Enable logging
'
crt.Session.Log True
'
' Wait for a string that looks like "login: " or "Login: "
crt.Screen.WaitForString "ogin:"
' Send your username followed by a carriage return
crt.Screen.Send username & VbCr
' Wait for a tring that looks like "password: " or "Password: "
crt.Screen.WaitForString "assword:"
crt.Screen.Send loginpswd & VbCr
crt.Screen.Send "en" & VbCr
crt.Screen.Send enpswd & VbCr
crt.Screen.Synchronous = False
crt.Screen.Send "sh mac" & VbCr
'这里只是举个sh mac的例子,可以改成别的命令比如show run等
crt.Screen.Send "exit" & VbCr &"exit" & VbCr
' turn off synchronous mode to restore normal input processing
' Stop logging and disconnect.
'
' crt.Session.Log False
' crt.Session.Disconnect
crt.quit
End Sub |
|