'' Author:  Steven Manross
''  Revision History
''   2016/03/01  1.0     initial version adapted from Microsoft KB articles and a non-working script found on the internet
''   2016/03/02  1.01    added the graceful shutdown option (c:\scripts\wmi_ping.stop file)
On Error Resume Next

Const ForAppending = 8

echo_logging_statements = True
write_log_to_file = 1
log_file_name = "c:\scripts\wmi_ping.log"



Set objFSO = CreateObject("Scripting.FileSystemObject")
Set IpDict = CreateObject("Scripting.Dictionary")

''initialize the IP Addresses (or DNS names) with a status of unreachable so they log once initially no matter what the outcome
''you can delete any of these lines if you don't want to monitor this large of a list of addresses or just put '' in front of the line.  :)

IpDict.Add "192.168.1.1", 999           ''router internal gateway (change as necessary)
IpDict.Add "192.168.100.1",999          '' cable modem  (change as necessary -- but this is usually the address for your cable modem providing you don't have xDSL)
IpDict.Add "xxx.xxx.xxx.xxx",999         ''internet gateway for outside IP Address  (change as necessary)
IpDict.Add "xxx.xxx.xxx.xxx",999         ''internet gateway (outside IP Address)  (change as necessary)
IpDict.Add "xxx.xxx.xxx.xxx", 999         ''wan address for some site I can ping without bothering anyone besides me (change as necessary)
IpDict.Add "someaddressontheinternet.whatever.com", 999   ''dns name of my wan address (change as necessary)

''make sure to include double quotes around the ip addresses you insert or change...  keep the 999 value as this initializes the IP Address to an unknown state
''  and the script will output results to the screen depending on whether or not the script can reach the IP or not. 

''  i developed this script because I was having weird issues on my internal lan and wanted to test important areas on my network as I changed things to try and
''  resolve an issue.

''it only outputs information if the state changes (Reachable to Unreachable, or Unreachable to Reachable)

''HTH
''Steven

x = 1
do while x = 1
  y = y + 1
  for each ip_addr in IpDict.Keys
    rtn = IsReachable(ip_addr,objPinger)
    if rtn <> IpDict.Item(ip_addr) Then
      IpDict.Item(ip_addr) = rtn
      if rtn = 0 Then 
        status_name = "Unreachable" 
      Else
        status_name = "Reachable" 
      End If
      if objPinger.ProtocolAddress <> ip_addr and objPinger.ProtocolAddress <> "" Then
        name_and_addr = ip_addr & " (" & objPinger.ProtocolAddress & ")"
    Else
      name_and_addr = ip_addr
    End If
    logitrtn = logit(name_and_addr & " is " & status_name & "!") 

    End If
  Next
  WScript.Sleep(2000)
  if y > 3600 Then
    logitrtn = logit("script still running after 3600 tests") 
    y = 0
  End If
  if objFSO.FileExists("c:\scripts\wmi_ping.stop") Then
    ''gracefully quit
    logitrtn = logit("graceful shutdown initiated because of c:\scripts\wmi_ping.stop file existance") 
    objFSO.DeleteFile("c:\scripts\wmi_ping.stop")
    WScript.Quit(1)
  End If
Loop

Function IsReachable(strAddress,objPing) 
 On Error Resume Next 
  
  Set objPing = GetObject("winmgmts:").Get("Win32_PingStatus.Address='" & strAddress & "'")

  If objPing.Statuscode = 0 Then 
    IsReachable = 1
  Else 
    IsReachable = 0 
  End If 
End Function 

Function logit (strng)
  On Error Resume Next
  If echo_logging_statements = True Then
    WScript.Echo Now & " -- " & strng
  End If
  
  '' unable to do good error checking in this routime since errors are getting passed into this routine
  ''   via Err.Number, and Err.Description

  if write_log_to_file = 1 Then
    Set objTextStream = objFSO.OpenTextFile(log_file_name,ForAppending,True)
    objTextStream.WriteLine(Now & " -- " & strng)
    objTextStream.Close
  End If
  logit = 1
End Function