locked
Problem With Ping In VBS Script RRS feed

  • Question

  • I am writing a script with VBScript to query printers using SNMP to pull configuration information from them.  In my script, I first attempt to ping the printers to verify that I can communicate with the IP address using the following code.

    Dim vPingFlag, vIP
    Set WshShell = CreateObject("WScript.Shell")
    vIP = "192.168.1.20"
    vPingFlag = Not CBool(WshShell.run("ping -n 1 " & vIP,0,True))
    The problem that I'm having is that when a printer is turned off, sometimes the ping will return a True value instead of False as it should.  This will cause the script to error out when the SNMP query (olePrn.oleSNMP) is then sent to the printer inadvertently since a True value was generated.  A network trace shows that the cause of the problem appears to be that a device is responding back with an ICMP destination unreachable packet which is being interpreted as a positive ping response from the printer.  Does anyone know how to resolve this?  I know that I could open a cmd window, ping the printer, pipe the results to a file and then parse the file for the responses.  However, I rather not have to go that route unless I absolutely have to.  Any help will be appreciated.
    Wednesday, September 25, 2013 1:23 AM

Answers

  • Hi,

    You could use WMI to check for an ICMP response...here is an example, hope that helps.

    Cheers Matt

    Dim hostName
    hostname = "printer1"
    If Not CheckConnection(hostName) Then
       MsgBox hostName & " did not respond to an ICMP request", vbCritical
    Else
       MsgBox hostName & " responded to an ICMP request", vbInformation
    End If
    '---------------------------------------------------------------------------------------------------------
    'Name       : CheckConnection -> Checks a remote host using WMI ping.
    'Parameters : hostName        -> Hostname of computer system to verify network connectivity with.
    'Return     : Boolean         -> True if hostname replies. False otherwise.
    '---------------------------------------------------------------------------------------------------------
    Function CheckConnection(hostName)
       Dim ping, response, replied
       Set ping = GetObject("winmgmts:{impersonationLevel=impersonate}").ExecQuery _
                           ("select * from Win32_PingStatus where address = '" & hostName & "'")
       For each response in ping
          replied = Not IsNull(response.StatusCode) And response.StatusCode = 0
       Next
       CheckConnection = replied
    End Function
    '---------------------------------------------------------------------------------------------------------

    • Edited by MatthewBeattie Wednesday, September 25, 2013 1:40 AM formatting
    • Proposed as answer by jrv Wednesday, September 25, 2013 2:01 AM
    • Marked as answer by eagle3089 Wednesday, September 25, 2013 10:54 PM
    Wednesday, September 25, 2013 1:39 AM

All replies

  • Hi,

    You could use WMI to check for an ICMP response...here is an example, hope that helps.

    Cheers Matt

    Dim hostName
    hostname = "printer1"
    If Not CheckConnection(hostName) Then
       MsgBox hostName & " did not respond to an ICMP request", vbCritical
    Else
       MsgBox hostName & " responded to an ICMP request", vbInformation
    End If
    '---------------------------------------------------------------------------------------------------------
    'Name       : CheckConnection -> Checks a remote host using WMI ping.
    'Parameters : hostName        -> Hostname of computer system to verify network connectivity with.
    'Return     : Boolean         -> True if hostname replies. False otherwise.
    '---------------------------------------------------------------------------------------------------------
    Function CheckConnection(hostName)
       Dim ping, response, replied
       Set ping = GetObject("winmgmts:{impersonationLevel=impersonate}").ExecQuery _
                           ("select * from Win32_PingStatus where address = '" & hostName & "'")
       For each response in ping
          replied = Not IsNull(response.StatusCode) And response.StatusCode = 0
       Next
       CheckConnection = replied
    End Function
    '---------------------------------------------------------------------------------------------------------

    • Edited by MatthewBeattie Wednesday, September 25, 2013 1:40 AM formatting
    • Proposed as answer by jrv Wednesday, September 25, 2013 2:01 AM
    • Marked as answer by eagle3089 Wednesday, September 25, 2013 10:54 PM
    Wednesday, September 25, 2013 1:39 AM
  • Hi Matt,

    Thanks for the response and the WMI method appears to be working.  A trace shows that a single ICMP echo request is sent vs the four that ping.exe sends out.  However, I am not seeing the ICMP destination unreachable message being sent back so I'm not sure how the WMI method will handle it so I'll keep testing.

    Thanks again.

    Wednesday, September 25, 2013 11:46 PM
  • Hi Matt,

    Thanks for the response and the WMI method appears to be working.  A trace shows that a single ICMP echo request is sent vs the four that ping.exe sends out.  However, I am not seeing the ICMP destination unreachable message being sent back so I'm not sure how the WMI method will handle it so I'll keep testing.

    Thanks again.


    Study the basics of IP engineering.  The ping return will become clear.

    ¯\_(ツ)_/¯

    Thursday, September 26, 2013 12:04 AM