首页 > 解决方案 > 通过 Excel ping 多台计算机,并在单独的列中具有 IP 地址

问题描述

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

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

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

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

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

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

当然,不一定要通过 excel 来完成,如果你有一个更好的解决方案,可以很好地查看我需要清楚地看到的所有 3 件事(comp name、、、 ) comp 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

我的 Excel 表格的图像

标签: excelvbaautomationpingsystem-administration

解决方案


这对我有用

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

推荐阅读