语言:VBS
背景:SecureCRT(用于TELET,相信搞网维的人都很熟悉)
功能介绍:该段代码为我日常工作时所用,每天晚上从两台交换机中取出数据,存入表格及保存LOG文件。功能很简单,代码只实现了每晚自动取,懒人会很感兴趣。
流程介绍:对CRT进行指令输入,截取结果,存入表格或LOG;也可以用EXCEL中或TXT中取数据供CTR输入,然后得到结果。如果在公网,配合上飞信接口的VBS代码,实时监控,会给你有意想不到的结果。这段代码首先输入指令,得到结果,截取字符,存入EXCEL,并将所有的LOG,以当天为文件名存入LOG文件。
缺点:代码一次写完,实现了结果,就没有修改,未分模块,可读性差,本想全部加上注释,因工作忙,也没有很大的意义。我将控制CTR的VBS命令附上,及定制VBS计划任务的批处理,希望对大家的日常办公有所帮助。- '网络的价值,在于你我的共享及经验的分享'
- '让你我的努力促进网络的发展,从而让自己从中受益。尊重作者,尊重原创……'
- ' ---------------yuihi---------------2012-3-12
- # $language = "VBScript"
- # $interface = "1.0"
- Const ForReading = 1
- Const ForWriting = 2
- Const ForAppending = 8
-
- Sub Main
- dingshi=hour(now)
- if dingshi<23 then
- exit sub
- end if
- crt.Screen.Synchronous = True 'CRT可见
- Dim fso, file,chengname,dingshi,zmvi,hlr2,hlr3,hlr4,all,app,wb,ws '定义变量
-
- chengname=Year(now)&"年"&Month(now)&"月"&Day(now)&"日"'取当前日期赋给变量chengname
- Set fso = CreateObject("Scripting.FileSystemObject")
- Set file = fso.OpenTextFile("D:\漫游用户\" &chengname &".txt", ForAppending, True)
-
- crt.Screen.Send "***SSG2" & chr(13)
- crt.Screen.WaitForString "ENTER USERNAME < " & chr(8) & " "
- crt.Screen.Send "****" & chr(13)
- crt.Screen.WaitForString "ENTER PASSWORD < " & chr(8) & " "
- crt.Screen.Send "************" & chr(13)
- crt.Screen.WaitForString "< " & chr(8) & " "
- crt.Screen.Send "ZMVI;" & chr(13) & Chr(10)
-
-
-
- crt.Screen.WaitForStrings("COMMAND EXECUTED")
- screenrow = crt.screen.CurrentRow - 2
- readline = crt.Screen.Get(screenrow, 14, screenrow, 20 )
- zmvi=readline
-
-
- crt.Screen.Send "ZMVF::HLR=861376649000;" & chr(13) & Chr(10)
- crt.Screen.WaitForStrings("COMMAND EXECUTED")
- screenrow = crt.screen.CurrentRow - 2
- readline = crt.Screen.Get(screenrow, 8, screenrow, 14 )
- hlr2=readline
-
-
- crt.Screen.Send "ZMVF::HLR=861387930000;" & chr(13)
- crt.Screen.WaitForStrings("COMMAND EXECUTED")
- screenrow = crt.screen.CurrentRow - 2
- readline = crt.Screen.Get(screenrow, 8, screenrow, 14 )
- hlr3=readline
-
-
- crt.Screen.Send "ZMVF::HLR=861518034000;" & chr(13)
- crt.Screen.WaitForStrings("COMMAND EXECUTED")
- screenrow = crt.screen.CurrentRow - 2
- readline = crt.Screen.Get(screenrow, 8, screenrow, 14 )
- hlr4=readline
-
-
- crt.Screen.Send "ZMVF::LAC=ALL,;" & chr(13)
- crt.Screen.WaitForStrings("COMMAND EXECUTED")
- screenrow = crt.screen.CurrentRow - 2
- readline = crt.Screen.Get(screenrow, 8, screenrow, 14 )
- all=readline
-
-
- Set app = CreateObject("Excel.Application")
- app.DisplayAlerts=FALSE
- app.visible=FALSE
- Set wb = app.Workbooks.Open("D:\漫游用户\GS2漫游数每日登记表.xls")
- Set ws = wb.Worksheets(1)
- app.WorkSheets(1).Activate
-
- app.range("A2").Value ="GS2"
- app.range("B2").Value =now
- app.range("C2").Value =zmvi
- app.range("D2").Value =hlr2
- app.range("E2").Value =hlr3
- app.range("F2").Value =hlr4
- app.range("G2").Value =all
- app.range("H2").Value ="=G2-F2-E2-D2"
- app.rows(2).insert
- app.save
- wb.close
- app.quit
- set app=nothing
- set wb=nothing
- set ws=nothing
-
- crt.Screen.Send "Z;"
- crt.Screen.Send chr(13)
- crt.Screen.WaitForString "< " & chr(8) & " "
- crt.Screen.Send "Z;" & chr(13)
- crt.Screen.WaitForString "srwork >"
- crt.Screen.Send "***SSG1" & chr(13)
- crt.Screen.WaitForString "ENTER USERNAME < " & chr(8) & " "
- crt.Screen.Send "****" & chr(13)
- crt.Screen.WaitForString "ENTER PASSWORD < " & chr(8) & " "
- crt.Screen.Send "*******" & chr(13)
- crt.Screen.WaitForString "< " & chr(8) & " "
- crt.Screen.Send "ZMVI;" & chr(13) & Chr(10)
- crt.Screen.Send "ZMVF::HLR=861376649000;" & chr(13)
- crt.Screen.Send "ZMVF::HLR=861387930000;" & chr(13)
- crt.Screen.Send "ZMVF::HLR=861518034000;" & chr(13)
- crt.Screen.Send chr(13)
- crt.Screen.Send "ZMVF::LAC=ALL,;" & chr(13)
-
- crt.Screen.WaitForString Chr(10)
- crt.Screen.Send "Z;"
- Dim waitStrs
- waitStrs = Array( Chr(10), "Z;" )
- Dim row, screenrow, readline, items
- row = 1
- Do
- While True
- result = crt.Screen.WaitForStrings( waitStrs )
- If result = 2 Then
- Exit Do
- End If
- screenrow = crt.screen.CurrentRow - 1
- readline = crt.Screen.Get(screenrow, 1, screenrow, 50 )
- file.Write readline & vbCrLf
- Wend
- Loop
-
- crt.Screen.Send chr(13)
- crt.Screen.WaitForString "< " & chr(8) & " "
- crt.Screen.Send "Z;" & chr(13)
- 'crt.Screen.WaitForString "< " & chr(8) & " "
- crt.Screen.WaitForString "srwork >"
- crt.Screen.Send "***SSG2" & chr(13)
- crt.Screen.WaitForString "ENTER USERNAME < " & chr(8) & " "
- crt.Screen.Send "****" & chr(13)
- crt.Screen.WaitForString "ENTER PASSWORD < " & chr(8) & " "
- crt.Screen.Send "************" & chr(13)
- crt.Screen.WaitForString "< " & chr(8) & " "
- crt.Screen.Send "ZMVI;" & chr(13) & Chr(10)
- crt.Screen.Send "ZMVF::HLR=861376649000;" & chr(13)
- crt.Screen.Send "ZMVF::HLR=861387930000;" & chr(13)
- crt.Screen.Send "ZMVF::HLR=861518034000;" & chr(13)
- crt.Screen.Send chr(13)
- crt.Screen.Send "ZMVF::LAC=ALL,;" & chr(13)
- crt.Screen.Send "ZZZ;"
- crt.Screen.WaitForString Chr(10)
-
- waitStrs = Array( Chr(10), "ZZZ;" )
-
- row = 1
- Do
- While True
- result = crt.Screen.WaitForStrings( waitStrs )
- If result = 2 Then
- Exit Do
- End If
- screenrow = crt.screen.CurrentRow - 1
- readline = crt.Screen.Get(screenrow, 1, screenrow, 50 )
- file.Write readline & vbCrLf
- Wend
- Loop
- crt.Screen.Send chr(13)
- crt.screen.synchronous = false
- crt.Screen.WaitForStringS "srwork >"
- crt.session.disconnect
- CRT.QUIT
- End Sub
复制代码 SecuerCRT的VBS命令集:- TabCount = crt.GetTabCount() ‘获取当前已打开的标签总数
- Set tab = crt.GetTab(index)获取相应索引的标签
- tab = crt.session.Connectintab("/s IP专网/AR")’打开会话中的标签
- tab.Session.Connect标签连接
- tab.activate激活标签
- tab.Session.Connected = True判断标签会话是否已连接
- Name = tab.Caption获取标签的名称
- crt.session.Connect "/telnet 10.178.153.194 8023" TELNET方式连接IP加端口
- crt.session.disconnect 中断会话连接
- CRT.QUIT 退出CRT
- crt.sleep 1000 等待1秒
- crt.screen.send "show interface description" &vbCr 向屏幕发送指令加回车
- crt.Screen.WaitForString "[JXSHR-MC-CMNET-BRAS01-ME60]" 在屏幕上等待字符串,然后再运行下一条
- screenrow = crt.screen.CurrentRow – 1 定义屏幕当前行数
- readline = crt.Screen.Get(screenrow, 8, screenrow, 14 ) 截取屏幕上相应的字符
复制代码 用批处理定时打开CRT中指令的脚本- "C:\Program Files\SecureCRT\SecureCRT.EXE" /T /SCRIPT 取BAS利用率.vbs
复制代码
|