【问题标题】:Ping multiple computers through Excel and have IP addresses in a separate column通过 Excel ping 多台计算机,并在单独的列中具有 IP 地址
【发布时间】:2021-10-19 01:28:59
【问题描述】:

我需要一个自动 ping 工具,因为我的网络中有 70 台计算机,我厌倦了每次都通过 cmd ping 它们。所以我有这张我已经完成的工作表(大部分)。

A 列包含我手动编写的计算机名称

B 列:它应该(但不)在 A 列中写出计算机的 IP 地址

C 列:根据 ping 显示计算机是在线还是离线。

D 列:我也手动输入了一些内容,以了解谁坐在哪台计算机后面,只是用户名。

我有两个按钮,pingstop ping。他们工作正常。我怎样才能通过按下ping 按钮,让它通过column A 中的所有计算机,并同时显示它们的IP 以及它们是否在线?

当然,不必通过 excel 来完成,如果您有更好的解决方案,可以很好地了解我需要清楚地看到的所有 3 件事(comp namecomp iponline/offline)和有用户名的第四件事会很高兴。

请帮帮我,我很绝望:

Dim objshell, boolcode
Set objshell = CreateObject("Wscript.Shell")
boolcode = objshell.Run("ping -n 1 -w 1000 " & strip, 0, True)
If boolcode = 0 Then
    Ping = True
Else
    Ping = False
End If
End Function
'_________________________
Sub PingSystem()
Dim strip As String
Do Until Sheet1.Range("G9").Value = "STOP"
Sheet1.Range("G9").Value = "Ping"
For introw = 1 To ActiveSheet.Cells(65536, 2).End(xlUp).Row
    strip = ActiveSheet.Cells(introw, 2).Value

    If Ping(strip) = True Then
        ActiveSheet.Cells(introw, 3).Interior.ColorIndex = 0
        ActiveSheet.Cells(introw, 3).Font.Color = RGB(0, 0, 0)
        ActiveSheet.Cells(introw, 3).Value = "Online"
        ActiveSheet.Cells(introw, 3).Font.Color = RGB(0, 200, 0)
    Else
        ActiveSheet.Cells(introw, 3).Interior.ColorIndex = 0
        ActiveSheet.Cells(introw, 3).Font.Color = RGB(200, 0, 0)
        ActiveSheet.Cells(introw, 3).Value = "Offline"
        ActiveSheet.Cells(introw, 3).Interior.ColorIndex = 6
    End If
    If Sheet1.Range("G9").Value = "STOP" Then
        Exit For
    End If
Next
Loop
Sheet1.Range("G9").Value = "Stop ping"
End Sub

Sub stop_ping()
    Sheet1.Range("G9").Value = "STOP"
End Sub

【问题讨论】:

  • 您说“A 列:包含我手动编写的计算机名称”,但您的代码 strip = ActiveSheet.Cells(introw, 2).Value 正在 ping B 列中的值?
  • 那是暂时存在的,因为我首先尝试读取静态 IP。同时它被改变了,我只是忘了从这个粘贴的版本中移动它。感谢您的关注。

标签: excel vba automation ping system-administration


【解决方案1】:

这对我有用

Sub temp()
    Set WshShell = CreateObject("WScript.Shell")
    RowCount = Worksheets("Sheet1").UsedRange.Rows.Count

For i = 1 To RowCount
    Url = Worksheets("Sheet1").Cells(i, 1).Value
    cmd = "ping " + Url
    Set WshShellExec = WshShell.Exec(cmd)
    result = WshShellExec.StdOut.ReadAll
    Worksheets("Sheet1").Cells(i, 2).Value = getIP(result)
    
    If (InStr(result, "Received = 4")) Then
        Worksheets("Sheet1").Cells(i, 3).Value = "Online"
    End If
Next
End Sub

Function getIP(result)
    ip = Split(result, vbNewLine)(1)
    startIndex = InStr(ip, "[") + 1
    endIndex = InStr(ip, "]")
    getIP = Mid(ip, startIndex, endIndex - startIndex)
End Function

【讨论】:

  • 它在 B 列的每个字段中的 A 列中添加每台给定计算机的 IP 地址?因为对我来说它什么也没做,它只表明每台计算机都处于离线状态。你把你提供的这段代码放在哪里了?
  • 是的,它从 A 列读取 url,更新 B 列中的 IP 地址,并在 C 列中设置状态。由于我的网络中没有其他计算机,因此使用两个测试示例网址 - google.com 和 youtube.com
  • 它真的不适合我。我还能如何尝试使其工作?您是否将此代码放在我发布的内容的下方、上方或中间?
  • 我没有使用你的代码,但尝试了一下。不过,当我现在尝试你的代码时,它对我有用,没有任何改变。你能手动得到结果吗?
  • 那么,你能不能把整个代码提供给我?因为我拥有的和我从你那里添加的永远不能一起工作。或者,您可以尝试以某种方式将您的代码与我的代码合并吗?自从您发布它以来,我整天都在这里捣蛋,找不到解决方案。我真的很感激。我很感激你到目前为止所做的一切,你给了我希望,但我就是无法得到它。
【解决方案2】:

查看 Excel 网络工具,使用 Excel 插件,您可以从 Excel ping 多个 IP 地址。

【讨论】:

  • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center
猜你喜欢
  • 2015-11-26
  • 1970-01-01
  • 1970-01-01
  • 2012-11-29
  • 1970-01-01
  • 1970-01-01
  • 2018-08-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多