3
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Excel マクロ VBA Macro Ping ネットワーク監視 / 記録シートの例

Posted at

This article explains how Excel macro VBA may be used to monitor servers or network and automatically save status into the Excel file.

Excelで多数のサーバー、ネットワーク機器のpingを監視し自動上書き保存するVBAマクロの例です。高価な監視ツールに頼ることなく興味があるシステムのみに絞り込みExcelなのでコメントを追記できますし管理者権限も入りませんから定期チェックやシステム変更作業前後にご利用ください。

前提条件:
・Sheet1というシート名があること
・B3セルから下方向にサーバー名またはIPアドレスが途切れることなく列挙されていること
・C3セルからC999セルまでが結果の記録セルで毎回記録内容のリセットがされること
・A2セルに最終更新日を書き込むこと
・ファイルは自動上書き保存されること

C列の3行目以降で最終行までは表示条件設定を行い文字列が Connected の場合には水色の背景、そうでなければ赤色の背景にすることで目立たせることをお勧めします。

条件は必要に応じ書き換えください。

マクロ VBA の例

Function GetPingResult(Host)
   Dim objPing As Object
   Dim objStatus As Object
   Dim Result As String

   Set objPing = GetObject("winmgmts:{impersonationLevel=impersonate}"). _
       ExecQuery("Select * from Win32_PingStatus Where Address = '" & Host & "'")

   For Each objStatus In objPing
      Select Case objStatus.StatusCode
         Case 0: strResult = "Connected"
         Case 11001: strResult = "Buffer too small"
         Case 11002: strResult = "Destination net unreachable"
         Case 11003: strResult = "Destination host unreachable"
         Case 11004: strResult = "Destination protocol unreachable"
         Case 11005: strResult = "Destination port unreachable"
         Case 11006: strResult = "No resources"
         Case 11007: strResult = "Bad option"
         Case 11008: strResult = "Hardware error"
         Case 11009: strResult = "Packet too big"
         Case 11010: strResult = "Request timed out"
         Case 11011: strResult = "Bad request"
         Case 11012: strResult = "Bad route"
         Case 11013: strResult = "Time-To-Live (TTL) expired transit"
         Case 11014: strResult = "Time-To-Live (TTL) expired reassembly"
         Case 11015: strResult = "Parameter problem"
         Case 11016: strResult = "Source quench"
         Case 11017: strResult = "Option too big"
         Case 11018: strResult = "Bad destination"
         Case 11032: strResult = "Negotiating IPSEC"
         Case 11050: strResult = "General failure"
         Case Else: strResult = "Unknown host"
      End Select
      GetPingResult = strResult
   Next

   Set objPing = Nothing

End Function

Sub GetIPStatus()

  Dim Cell As Range
  Dim ipRng As Range
  Dim Result As String
  Dim Wks As Worksheet

Set Wks = Worksheets("Sheet1")

Set ipRng = Wks.Range("B3")
Set RngEnd = Wks.Cells(Rows.Count, ipRng.Column).End(xlUp)

Range("C3:C999").ClearContents


Set ipRng = IIf(RngEnd.Row < ipRng.Row, ipRng, Wks.Range(ipRng, RngEnd))

  For Each Cell In ipRng
    Result = GetPingResult(Cell)
    Cell.Offset(0, 1) = Result
  Next Cell

Range("A2").Value = Date

ActiveWorkbook.Save

End Sub

3
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?