locked
Ping remote routers with WMI RRS feed

  • Question

  • Hello everybody!

    I need help with a script. I need to ping multiple routers over Intranet with a script and send email to IT departement if a router is unreachable.

    I have the script so I can ping all routers and send mail but when the mail is send it tells me that " 192.168.1.1'' is down and I would like to say: "Router A is down"

    I dont know how to convert ip-s to hostname tha I like not to resolve from dns. Here is the script. How it can be done in this way, so that I can assign an Ip to a host that I want.

    Thanks in advance.

    strMachines = "192.168.1.1;192.168.1.2;192.168.1.3;192.168.1.4;192.168.1.5"

    modem ="192.168.1.1"
    RouterA="192.168.1.2"
    RouterB="192.168.1.3"
    RouterC="192.168.1.4"
    RouterD="192.168.1.5"


     
    aMachines = split(strMachines, ";")
     
    For Each machine in aMachines
        Set objPing = GetObject("winmgmts:{impersonationLevel=impersonate}")._
            ExecQuery("select * from Win32_PingStatus where address = '"_
                & machine & "'")
        For Each objStatus in objPing
            If IsNull(objStatus.StatusCode) or objStatus.StatusCode<>0 Then 
                WScript.Echo("Computer " & machine & " is not reachable") 
            End If
        Next
    Next

    Sunday, April 17, 2011 4:14 PM

Answers

  • This type of problem is usually solved by setting up a dictionary but in this case a parallel array is probably an easier solution:

    aAddresses = Split("192.168.1.1 192.168.1.2 192.168.1.3 192.168.1.4 192.168.1.5")
    aNames = Split("modem RouterA RouterB RouterC RouterD")

    For i = 0 To UBound(aAddresses)
        WScript.Echo "Device", aNames(i), "uses address", aAddresses(i)
    Next

    • Marked as answer by Bill_Stewart Monday, April 25, 2011 4:49 PM
    Sunday, April 17, 2011 6:41 PM

All replies

  • This type of problem is usually solved by setting up a dictionary but in this case a parallel array is probably an easier solution:

    aAddresses = Split("192.168.1.1 192.168.1.2 192.168.1.3 192.168.1.4 192.168.1.5")
    aNames = Split("modem RouterA RouterB RouterC RouterD")

    For i = 0 To UBound(aAddresses)
        WScript.Echo "Device", aNames(i), "uses address", aAddresses(i)
    Next

    • Marked as answer by Bill_Stewart Monday, April 25, 2011 4:49 PM
    Sunday, April 17, 2011 6:41 PM
  • It doesn't seem to work or I am not able to implement it in the script.

     

    Can you tell me how to do exactly how to do this with my script?

     

     

     

     

    Thanks in advance.

    Monday, April 18, 2011 1:06 PM
  • It doesn't seem to work or I am not able to implement it in the script.

    Can you tell me how to do exactly how to do this with my script?

    I love it when posters respond with "It does not seem to work". It leaves so much room for the imagination!

    Seriously - did you test the script I posted? Did you run it from a Command Prompt? What do you see? Do you understand what it does?

    Monday, April 18, 2011 1:34 PM
  • Why do you have 4 routers on the same subnet?
    Monday, April 18, 2011 4:59 PM
  • Assuming the addresses are examples, and combining your code with the suggestion by Pegasus, how about this:

    aAddresses = Split("192.168.1.1 192.168.1.2 192.168.1.3 192.168.1.4 192.168.1.5")
    aNames = Split("modem RouterA RouterB RouterC RouterD")
    
    For i = 0 To UBound(aAddresses)
      Set objPing = GetObject("winmgmts:{impersonationLevel=impersonate}")._ 
        ExecQuery("select * from Win32_PingStatus where address = '"_ 
          & aAddresses(i) & "'") 
      For Each objStatus in objPing 
        If IsNull(objStatus.StatusCode) or objStatus.StatusCode<>0 Then 
          WScript.Echo("Computer " & aNames(i) & " is not reachable") 
        End If 
      Next 
    Next

     

    It's up to you to make sure the two arrays have the same upper bound (number of elements).

     


    Richard Mueller - MVP Directory Services
    Saturday, April 23, 2011 10:21 PM