【问题标题】:Continuous ping with updates to Exel from list of hostnames via VBScript通过 VBScript 从主机名列表更新 Excel 连续 ping
【发布时间】:2017-01-24 00:54:57
【问题描述】:

以下 VBScript 将从主机名列表到 Excel 电子表格中显示单个 ping 命令的状态:

Set objExcel = CreateObject("Excel.Application")

objExcel.Visible = True

objExcel.Workbooks.Add

intRow = 2


'# Define Labels 

objExcel.Cells(1, 1).Value = "Node"

objExcel.Cells(1, 2).Value = "Status"


'# Create file system object for reading the hosts from text file


Set Fso = CreateObject("Scripting.FileSystemObject")

Set InputFile = fso.OpenTextFile("MachineList.Txt")

'# Loop thru the text file till the end 

Do While Not (InputFile.atEndOfStream)

HostName = InputFile.ReadLine

'# Create shell object for Pinging the host machines

Set WshShell = WScript.CreateObject("WScript.Shell")

Ping = WshShell.Run("ping -n 1 " & HostName, 0, True)


objExcel.Cells(intRow, 1).Value = HostName

'# use switch case for checking the machine updown status

Select Case Ping

Case 0
    objExcel.Cells(intRow, 2).Value = "Up"
    objExcel.Cells(intRow, 2).Interior.Color = vbGreen
Case 1
    objExcel.Cells(intRow, 2).Value = "Down"
    objExcel.Cells(intRow, 2).Interior.Color = vbRed

End Select


intRow = intRow + 1

Loop

'# Format the excel

objExcel.Range("A1:B1").Select

objExcel.Selection.Interior.ColorIndex = 19

objExcel.Selection.Font.ColorIndex = 11

objExcel.Selection.Font.Bold = True

objExcel.Cells.EntireColumn.AutoFit

此代码按预期工作。

我想通过 Excel 的实时更新不断地 ping 主机名。我尝试将-t 添加到 ping 命令中 - 而不是像在命令提示符中那样添加-n 1,但脚本会挂起,因为它会在列表中的第一个主机名处停止,并且不会进一步。

我的问题:如何实现连续ping命令或重复运行脚本?

在网上找了几天解决办法,没找到。

【问题讨论】:

  • 您可以在 Excel 中使用Application.OnTime 以特定时间间隔循环运行宏。编辑:由于您使用的是 vbscript,您可以将代码放入循环中并使用 WScript.Sleep() 添加延迟虽然您需要更改管理结果显示的方式...
  • @TimWilliams,您能否详细介绍一下循环概念以及我们在这里讨论的结果显示有多少变化?在这条开发道路上,我一直在想出一个想法,将输出变成一个 HTML 文件。我知道这个问题有点偏离主题,但如果我必须转向,这就是我想进入的方向 - 就像一个注释一样。

标签: excel vbscript ping


【解决方案1】:

使用wscript.sleep 并将结果写入文本文件

Dim txt, fso, InputFile, OutputFile, arr, h, Ping, sep

Set fso = CreateObject("Scripting.FileSystemObject")
Set WshShell = wscript.CreateObject("WScript.Shell")

'# load the hosts from a text file into an array
Set InputFile = fso.OpenTextFile("MachineList.Txt")
arr = Split(InputFile.readall(), vbCrLf)
InputFile.Close

Do
    txt = Now & vbcrlf 'add a timestamp
    sep = ""
    For Each h In arr
        Ping = WshShell.Run("ping -n 1 " & h, 0, True)

        txt = txt & sep & h & ","
        If Ping = 1 Then
            txt = txt & "Up"
        ElseIf Ping = 0 Then
            txt = txt & "Down"
        Else
            txt = txt & "???"
        End If
        sep = vbCrLf
    Next 

    Set OutputFile = fso.createTextFile("Results.Txt")
    OutputFile.write txt
    OutputFile.Close

    wscript.sleep 1000 * 60 '60 sec
Loop

【讨论】:

  • 如何缩短睡眠时间?我需要让它尽可能低。此外,我必须将 If Ping = 1 Then 更改为 Down,反之亦然,以获得 Ping 结果的正确输出。输出是否有替代方案,或者必须是 .txt 文件?如果没有,我也许可以指向 .html 文件,以便稍后从 .txt 中提取结果。
  • 您可以将结果放在您喜欢的任何位置。写入文本文件只是一个例子。查看 Sleep 的文档 - Google 是您所知道的......
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-03-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-07-11
相关资源
最近更新 更多