设备巡检脚本
VB设备巡检脚本配合CRT使用。
准备资料“设备信息表”,内容格式“127.0.0.1 user password 设备描述”,名字随意,会让你去选择信息存放位置,格式应该还算规整,需要的带走,巡检内容可自己修改,此处以抓取设备配置为例,不能通信的设备及用户名/密码错误的存放至“D:\ip.txt”,log保存路径“D:\devicebackup\”。
#$language = "VBScript"
#$interface = "1.0"
crt.Screen.Synchronous = True
Function Main
On Error Resume Next
Const ForReading = 1 , ForWriting = 2 , ForAppending = 8
Dim fso, tempFolder, tempName, path, BrowseForFile, file1, line , params, wordkey, f
Set fso = CreateObject( "Scripting.FileSystemObject" )
Set tempFolder = fso.GetSpecialFolder( 2 )
tempName = fso.GetTempName( ) & ".hta"
path = "HKCU\Volatile Environment\MsgResp"
With CreateObject( "WScript.Shell" )
With tempFolder.CreateTextFile( tempName)
.Write "<input type=file name=f>" & _
"<script>f.click();(new ActiveXObject('WScript.Shell'))" & _
".RegWrite(
"close();</script>"
.Close
End With
.Run tempFolder & "\" & tempName, 1 , True
BrowseForFile = .RegRead( path)
.RegDelete path
fso.DeleteFile tempFolder & "\" & tempName
End With
Set file1= fso.OpenTextFile( BrowseForFile, ForReading, False )
While file1.AtEndOfStream <> True
line = file1.ReadLine
params = Split ( line )
crt.Session.Connect "/telnet" & " " & params( 0 )
If Err.Number<> 0 Then
set f = fso.OpenTextFile( "D: \ip .txt", 8 , true )
f.WriteLine( params( 0 ) + " 设备连接失败 "+ params( 3 ) )
f.Close ( )
else
crt.Session.LogFileName = "D: \devicebackup\"+params(3)+" \"+ params(0) + chr(32)+Replace(Replace(Replace(date, " : ", ""), " / ", ""), " ", "") + " _log.txt"
call crt.Session.Log ( false )
call crt.Session.Log ( true )
wordkey = crt.Screen.WaitForStrings ( "login:" , "Username:" , "Password:" , 3 )
Select case wordkey
case 1
crt.Screen.Send params( 1 ) & chr ( 13 )
crt.Screen.WaitForString "Password:"
crt.Screen.Send params( 2 ) & chr ( 13 )
if ( crt.Screen.WaitForString ( "Error" , "refused" , 1 ) <> 0 ) then
set f = fso.OpenTextFile( "D: \ip .txt", 8 , true )
f.WriteLine( params( 0 ) + " 用户名密码错误 "+ params( 3 ) )
f.Close ( )
else
Do
bCursorMoved = crt.Screen.WaitForCursor( 1 )
Loop until bCursorMoved = False
crt.Screen.Send "dis cur" & chr ( 13 )
do while ( crt.Screen.WaitForString( "---- More ----" , 3 ) = true )
crt.Screen.Send " "
loop
end if
case 2
crt.Screen.Send params( 1 ) & chr ( 13 )
crt.Screen.WaitForString "Password:"
crt.Screen.Send params( 2 ) & chr ( 13 )
if ( crt.Screen.WaitForString ( "Error" , "refused" , 1 ) <> 0 ) then
set f = fso.OpenTextFile( "D: \ip .txt", 8 , true )
f.WriteLine( params( 0 ) + " 用户名密码错误 "+ params( 3 ) )
f.Close ( )
else
Do
bCursorMoved = crt.Screen.WaitForCursor( 1 )
Loop until bCursorMoved = False
crt.Screen.Send "dis cur" & chr ( 13 )
do while ( crt.Screen.WaitForString( "---- More ----" , 3 ) = true )
crt.Screen.Send " "
loop
end if
case 3
crt.Screen.Send params( 1 ) & chr ( 13 )
crt.Screen.WaitForString "Password:"
crt.Screen.Send params( 2 ) & chr ( 13 )
if ( crt.Screen.WaitForString ( "Error" , "refused" , 1 ) <> 0 ) then
set f = fso.OpenTextFile( "D: \ip .txt", 8 , true )
f.WriteLine( params( 0 ) + " 用户名密码错误 "+ params( 3 ) )
f.Close ( )
else
Do
bCursorMoved = crt.Screen.WaitForCursor( 1 )
Loop until bCursorMoved = False
crt.Screen.Send "dis cur" & chr ( 13 )
do while ( crt.Screen.WaitForString( "---- More ----" , 3 ) = true )
crt.Screen.Send " "
loop
end if
End Select
call crt.Session.Log ( false )
end if
crt.Session.Disconnect
wend
crt.Screen.Synchronous = False
End Function
创作不易,转发请注明出处。