《大话SecureCRT脚本》之批量设备维护工具(doCheck.vbs)

本文介绍了一个利用VBScript编写的自动化工具——doCheck.vbs,该工具用于批量对网络设备进行巡检。它会在尝试3次登录设备后退出,如果登录成功则执行巡检操作并将结果记录到日志和结果文件中。在脚本执行完毕后,还会列出所有登录失败的设备IP。

执行巡检动作的代码。

功能:3次登录设备,不成功则退出,成功则执行巡检动作并保存到日志、结果文件。

           在程序最后,列出登录不成功的设备IP。

#$language = "VBScript"
#$interface = "1.0"

'created by tcy02, 2018-7-2
crt.Screen.Synchronous = True
Dim g_objTab,g_szSkippedTabs,userName,pwd
Set g_objTab = crt.GetScriptTab
g_szSkippedTabs = ""
g_objTab.Screen.Synchronous = True

Dim arrDeviceIP(), ipNumber
ipNumber = 0 '记录IP站点的个数

Sub Main
	    If crt.Dialog.MessageBox("check after 3s?" & vbcrlf & vbcrlf ,"check Start", _
        vbyesno) <> vbyes then exit Sub

    '读取ipSetings文件参数
    Dim currentPath, objFSO, MyArray, binArray, R86xYesNo
      Set objFSO = CreateObject("Scripting.FileSystemObject")
      currentPath = createobject("Scripting.FileSystemObject").GetFolder(".").Path + "\"
      Include currentPath& "libGlobal.vbs"
	  On Error Resume Next
      checkLog= currentPath&"doCheck" & ".log"

	Set objFile = objFSO.OpenTextFile(currentPath&"IpSeting.txt", 1)
    Do Until objFile.AtEndOfStream
    	line=objFile.ReadLine
       if instr(line,"ip=") then  '1, 设备登录IP
          MyArray = Split(line, "=", -1, 1)
          line = MyArray(1)
          Redim Preserve arrDeviceIP(ipNumber)  '添加IP
          arrDeviceIP(ipNumber) = line
          ipNumber = ipNumber + 1
       end if
       if instr(line,"username=") then  '2, 设备的用户名
          MyArray = Split(line, "=", -1, 1)
          line = MyArray(1)
          userName=line
       end if
       if instr(line,"userpwd=") then  '3, 设备的密码
          MyArray = Split(line, "=", -1, 1)
          line = MyArray(1)
          pwd=line
       end if
  Loop

  checkResult=currentPath & "Result.csv"
  writeResultFile checkResult,"1, 2, 3, 4, checkTime--" & now '写入报头
  KillExcelProcess
  crt.Sleep 20

    objFile.Close
    Set objFSO =Nothing
    For i=0 To UBound(arrDeviceIP)-LBound(arrDeviceIP) '1th#,
        writeLogFile checkLog, "egrPortTable_check NE IP-" & arrDeviceIP(i)
	   If crt.Session.Connected Then crt.Session.Disconnect        '若已建立的连接则断开
       cmd = "/ssh2 /ACCEPTHOSTKEYS /L " &userName & " /PASSWORD " &pwd &" /C 3DES " &arrDeviceIP(i)
       numVar=0
       do while numVar < 3  '3次重试
         err.clear
         if numVar=0 then
           crt.Session.ConnectInTab cmd   '第一次在tab中连接
         else
           crt.Session.Connect cmd		  '第2,3次在本窗口中连接
         end if
         If Err.Number <> 0 Then  'case 1, bad
             numVar=numVar+1
    	     if numVar > 3 or numVar =3 then
    	       writeLogFile checkLog, "Exit for 3 times fail!-" & arrDeviceIP(i)
               if g_szSkippedTabs = "" then
                   g_szSkippedTabs = crt.Window.Caption  & vbcrlf '索引号为nIndex
               else
                   g_szSkippedTabs = g_szSkippedTabs & "," & crt.Window.Caption & vbcrlf
               end if
			      str_1=crt.Window.Caption & ", /, Device cannot be connected"
           	      writeResultFile checkResult, str_1 '保存到文件
    	       exit do '结束
    	     End if
          Else   'case 2, ok
    	   numVar =3
           Dim nIndex
           nIndex = 1
               Set objCurrentTab = crt.GetTab(nIndex)
               objCurrentTab.Activate
               if objCurrentTab.Session.Connected = True then
       	         'do sth, start
       	          szData = CaptureOutputOfCommand(nIndex, "Date", "#")
				  writeLogFile checkLog, "Date --" & szData
       	          SendExpect nIndex,"exit", "#"
                  writeResultFile checkResult, szData '保存到Result文件
       	        'do sth, end
              End if
		 end if
		Loop
    Next
    g_objTab.Activate

    if g_szSkippedTabs <> "" Then
       g_szSkippedTabs = vbcrlf & vbcrlf & "Device cannot be connected" & _
            vbcrlf & vbtab & g_szSkippedTabs
    end If

    crt.Dialog.MessageBox _
        vbtab & g_szSkippedTabs & vbcrlf & _
		 "check finish, result file is at- " &checkResult,"check finish", BUTTON_OK
	'CreateObject("WScript.Shell").Run "taskkill /f /im SecureCRT.exe",0,True
End Sub
''~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
'在脚本之间共享变量,Include  "libGlobal.vbs"
Sub Include(sInstFile)
Dim oFSO, f, s
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set f = oFSO.OpenTextFile(sInstFile)
s = f.ReadAll
f.Close
ExecuteGlobal s
End Sub

 配套的库文件libGlobal.vbs:

'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Function SendExpect(nIndex,szSend, szExpect)
    Set objCurrentTab = crt.GetTab(nIndex)
    timeVar=0
    if objCurrentTab.Session.Connected <> True then exit function
        
    Do Until timeVar > 2
      objCurrentTab.Screen.Send szSend & vbcr
      If objCurrentTab.Screen.WaitForString(szExpect, 10) <> True Then
        timeVar=timeVar+1
      else
        timeVar=3
      End If
    Loop

    SendExpect = True
End Function

'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Function CaptureOutputOfCommand(nIndex, szCommand, szPrompt)
   Set objCurrentTab = crt.GetTab(nIndex)
   if objCurrentTab.Session.Connected <> True then
       CaptureOutputOfCommand = "[ERROR: Not Connected.]"
       exit function
   end if
    
    timeVar=0
    Do Until timeVar > 2
     objCurrentTab.Screen.Send szCommand & vbcr
     objCurrentTab.Screen.WaitForString vbcr
     CaptureOutputOfCommand = objCurrentTab.Screen.ReadString(szPrompt,3)
     If CaptureOutputOfCommand &" " =" " Then
     	  timeVar=timeVar+1
     Else
         timeVar=3
     End If
    Loop
End Function
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Function writeFile(filePath,  log)
    dim fso, f
    set fso =CreateObject("Scripting.FileSystemObject")
    set f = fso.CreateTextFile(filePath, True) '第二个参数表示同名文件存在时是否覆盖
    'f.Write("写入内容")
    'f.WriteLine("写入内容并换行")
    'f.WriteBlankLines(3) '写入三个空白行(相当于在文本编辑器中按三次回车)
    f.Write(log)
    f.Close()  'close是必要的,不要省
    set f = nothing
    set fso = Nothing
    writeFile = True
End Function
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Function displayArray_L1(binArray)  '显示数组内容
    	 Dim str_1
    	 str_1=""
    	 For i3=0 To UBound(binArray,1)
        str_1=str_1 & binArray(i3) &Chr(13) '循环遍历数组,并输出数组值
       Next
    	 MsgBox "Array:"  & Chr(13) & str_1
    	 displayArray = True
End Function
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Function displayArray_L2(binArray)  '显示数组内容,i行j列
    	 Dim str_1
    	 str_1="Array:"
    	 For i3=0 To UBound(binArray,1)'遍历行,第一维
    	   For j3=0 To UBound(binArray,2)'遍历列,第二维
          str_1=str_1 & "[" & i3 & ", " & j3 & "]- " & binArray(i3,j3) '循环遍历数组,并输出数组值
         Next
        str_1=str_1 & Chr(13) '一列处理完成,换行
       Next
    	 MsgBox str_1
    	 'writeFile checkResult, str_1
    	 displayArray_L2 = True
End Function
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Function KillExcelProcess()
    on error resume Next
    CreateObject("WScript.Shell").Run "taskkill /f /im EXCEL.EXE ",0,True
    'kill所有的wps excel进程
    CreateObject("WScript.Shell").Run "taskkill /f /im et.exe ",0,True

    'CreateObject("WScript.Shell").Run "taskkill /f /im Wscript.exe"
End Function
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
'打开FHAIP程序完整性检查记录
'fileName——带路径的文件名
'logStr——记录的内容
Function writeLogFile(fileNameStr, logStr)
    'KillExcelProcess
	Set binFileObject=CreateObject("Scripting.FileSystemObject")
    dim fLog
    If True = binFileObject.fileExists(fileNameStr) Then   '是否存在
      Set objFile = binFileObject.GetFile(fileNameStr)
       if objfile.Size >= 10000000 Then '文件大于10M?
         Set fLog = binFileObject.OpenTextFile(fileNameStr, 2, false) '第二个参数2表示重写
       else
	     Set fLog = binFileObject.OpenTextFile(fileNameStr, 8, false) '8 表示追加
	   end if
    Else
	  Set myfile = binFileObject.CreateTextFile(fileNameStr, true) '第二个参数表示目标文件存在时是否覆盖,不存在则创建
	  myfile.Close
	  Set fLog = binFileObject.OpenTextFile(fileNameStr, 8, false) '第二个参数8 表示追加
    End If

    fLog.WriteLine(Now & ": " & logStr)
	fLog.Close '退出文件

  Set binFileObject = Nothing   '释放文件操作对象
End Function
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
'memory检查结果记录
'fileName——带路径的文件名
'logStr——记录的内容
Function writeResultFile(fileNameStr_1, logStr_1)
    'KillExcelProcess
	Set binFileObject_1=CreateObject("Scripting.FileSystemObject")
    dim fLog_1
    If True = binFileObject_1.fileExists(fileNameStr_1) Then   '是否存在
      Set fLog_1 = binFileObject_1.OpenTextFile(fileNameStr_1, 8, false) '第二个参数2表示重写,如果是 8 表示追加
    Else
	  Set myfile_1 = binFileObject_1.CreateTextFile(fileNameStr_1, true) '第二个参数表示目标文件存在时是否覆盖,不存在则创建
	  myfile_1.Close
	  Set fLog_1 = binFileObject_1.OpenTextFile(fileNameStr_1, 8, false) '第二个参数8 表示追加
    End If

    fLog_1.WriteLine(logStr_1) '不打印时间
	fLog_1.Close '退出文件

  Set binFileObject_1 = Nothing   '释放文件操作对象
End Function
''~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
'####################################################################  
'判断操作系统为中文或英文
Function language() 
 language="None"
 strComputer = "." 
 Set objWMIService = GetObject("winmgmts://" &strComputer &"/root/CIMV2") 
 Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_OperatingSystem") 
 For Each objItem In colItems   
   language = objItem.OSLanguage 
   If language = "1033" Then 
     'Language = "EN" 
     language = "English" 
   elseif language = "2052" then 
     'Language = "CN" 
     language = "Chinese" 
   End If     
 Next 
End Function            
''~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
'在脚本之间共享变量,以及相互调用函数。
'使用方法:Include  "libGlobal.vbs"
Sub Include(sInstFile) 
Dim oFSO, f, s 
Set oFSO = CreateObject("Scripting.FileSystemObject") 
Set f = oFSO.OpenTextFile(sInstFile) 
s = f.ReadAll 
f.Close 
ExecuteGlobal s 
End Sub 

 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值