利用SecureCRT的脚本功能自动定时执行任务

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

利⽤SecureCRT的脚本功能⾃动定时执⾏任务
SecureCRT是⼀款仿真终端⼯具,常⽤来telnet到路由器或交换机上,它还⽀持VBscript和Jscript脚本。

下边讲述⼀种在WINDOWS下利⽤SecureCRT的脚本功能⾃动定时执⾏⼀些登录到路由器或交换机来执⾏的任务:
1.写好要SecureCRT执⾏的脚本,可登录执⾏任何操作。

如se800.vbs,保存在d:\se800下。

内容附在最后。

2.写好启动SecureCRT执⾏脚本的批处理⽂件。

如add_cmd.bat,保存在d:\se800下
内容如下:
d:
cd \se800
"C:\Program Files\SecureCRT\SecureCRT.EXE" /T /SCRIPT se800.vbs
3.在WINDOWS的控制⾯板的任务计划⾥增加任务,如在23:30执⾏add_cmd.bat,⼤功告成。

====================
se800.vbs的内容:
====================
# $language = "VBScript"
# $interface = "1.0"
Function my_Date()
dim y,m,d,h,mm
Dim nowDate,mytime
nowDate=Now()
mytime=time()
y=Year(nowDate)
m=Month(nowDate)
d=Day(nowDate)
h=hour(mytime)
mm=minute(mytime)
my_Date=y & "-" & m & "-" & d
End Function
Sub main
Dim fso, fHost,fCMDbsn,fCMDscs, strLineHost,strLineCommand,strLogfile,strArray
Const ForReading = 1
Const Hostip=0
Const user=1
Const password=2
Const superpwd=3
Const hostname=0
Const NodeType=4
Const my_user="jhxsy"
Const my_pass="2381654"
Const ScriptPath="D:\se800\"
Const LogPath="D:\se800\log\"
Set fso = CreateObject("Scripting.FileSystemObject")
Set fHost = fso.OpenTextFile(ScriptPath& "ip.txt", ForReading, 0) '
Do While fHost.AtEndOfStream <> True
crt.Screen.Synchronous = True
strLineHost = Trim(fHost.Readline ) '获取fhost⽂件中的⼀⾏,如192.168.0.9 null password en_pwd BAS
strArray=split(strLineHost,vbtab,-1) '按tab将获取的⾏分割成⼀个数组strArray[0]=192.168.0.9 .... strArray[4]=BAS strLogfile=LogPath & strArray(hostname)& "-" & my_date() &".txt"
crt.Session.LogFileName = strLogfile
crt.Session.Log True
crt.Session.Connect "/telnet" & " " & strArray(Hostip) & vbcr 'telnet strArray[0] vbcr=回车
crt.screen.waitforstring "login:"
crt.screen.send my_user & vbcr 'strArray(user) & vbcr
crt.screen.waitforstring "assword:"
crt.screen.send my_pass & vbcr
If strArray(NodeType)="BAS" Then
Set fCMDbsn=fso.OpenT extFile(ScriptPath& "cmd.txt", ForReading, 0) '如果是bas 打开cmd.txt⽂件并按forreading⽅式读取Do While fCMDbsn.AtEndOfStream <> True '如果没结束
strLineCommand=Trim(fCMDbsn.Readline)'获取读取的⾏
Do While strLineCommand="wait"'?不解
crt.sleep 5000
strLineCommand=Trim(fCMDbsn.Readline)
Loop
crt.Screen.WaitforString "#"'等待出现超级权限的提⽰
crt.Screen.Send strLineCommand & vbcr'发送命令
Call CheckEnd()
Loop
Set fCMDbsn=nothing
Else
Set fCMDscs=fso.OpenT extFile(ScriptPath& "cmdscs.txt", ForReading, 0)
Do While fCMDscs.AtEndOfStream <> True
strLineCommand=Trim(fCMDscs.Readline)
crt.Screen.WaitforString ">"
crt.Screen.Send strLineCommand & vbcr
crt.Screen.Send "y" & vbcr
Loop
Set fCMDscs=nothing
End If
'Logout node
crt.screen.waitforstring "#"
'crt.Session.Log False
crt.screen.send " exit" & vbcr
'Stop log file action
'crt.screen.waitforstring "$"
'crt.screen.send vbcr
crt.Session.Log False
'crt.screen.send vbcr
crt.Session.Disconnect
crt.Screen.Synchronous = False
Loop
Set fso=nothing
crt.quit
End Sub
Sub CheckEnd()
Do Until crt.Screen.WaitForString ("#" , 1)=True
crt.Screen.Send " "
Loop
crt.Screen.Send vbcr
Exit Sub
End Sub
经常遇到需要对⼀批⽹络设备进⾏同⼀种操作的情况,⽐如备份,或者查找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
' 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
#$Interface = "1.0"
Sub Main
Dim host
host = "192.168.1.1"
Dim user
user="admin"
Dim password
password = "Admin@123"
cmd = "/SSH2 /L " & user & " /PASSWORD " & password & " /C 3DES /M MD5 " & host
crt.Session.Connect cmd
logpath = "C:\Users\xu_duo\Desktop\CmnetT ool\cmnet\backup\"
crt.Session.LogFileName = logpath & "XX-192.168.1.1.-" & Year(Now) & "-" & Month(Now) & "-" & Day(Now) & "-" & Hour(Now) & "-" & Minute(Now) & ".txt"
crt.Session.Log True
Do while (crt.Screen.WaitForString("[Y/N]:",3)<>False)
crt.Screen.send "N" & VbCr
Loop
crt.Screen.Send VbCr
crt.Screen.WaitForString ">"
crt.Screen.Send "dis cu" & VbCr
Do while (crt.Screen.WaitForString(" ---- More ----",3)<>False)
crt.Screen.Send " "
Loop
crt.Screen.Send VbCr
crt.Screen.WaitForString ">"
crt.Screen.Send "display clock"
crt.Screen.Send VbCr
crt.Screen.WaitForString ">"
crt.Session.Log False
crt.Session.Disconnect
crt.quit
End Sub。

相关文档
最新文档