Script to ping a host and if host replies script should exit.

Discussion in 'Scripting' started by Spin, Jul 27, 2008.

  1. Spin

    Spin Guest


    I am trying to write a script which will ping a host, and if the remote host
    replies, the script will exit. Any thoughts? This could be either batch or
    Spin, Jul 27, 2008
    1. Advertisements

  2. Try this batch file:
    @echo off
    ping SomeHost | find /i "bytes=" && goto :eof
    Pegasus \(MVP\), Jul 27, 2008
    1. Advertisements

  3. Spin

    J Ford Guest

    @echo off
    ping -n 1 <MachineName>
    if %errorlevel% equ 0 goto :eof
    J Ford, Jul 28, 2008
  4. ???

    Pegasus \(MVP\), Jul 28, 2008
  5. Spin

    J Ford Guest

    ping it once, if its successful goto eof

    J Ford, Jul 28, 2008
  6. Yes, I know this, seeing that your reply is a long-hand
    copy of my own suggestion.
    Pegasus \(MVP\), Jul 28, 2008
  7. Spin

    J Ford Guest

    Same posting level... didn't look at yours before I typed mine.
    J Ford, Jul 29, 2008
  8. Spin

    Spin Guest

    ping SomeHost | find /i "bytes=" && goto :eof

    doesn't work too well. If the 'SomeHost' is un-reachable, I need the script
    to proceed to the next line and execute a certain command. If 'SomeHost' is
    successfully reachable, only then do I need it to go to :eof. Currently,
    just goes to :eof regardless of whether or not 'SomeHost' is reachable.
    Spin, Jul 31, 2008
  9. The batch file
    @echo off
    ping SomeHost | find /i "bytes=" && goto :eof
    echo Not found!

    will generate the console message "Not found" when
    SomeHost is not reachable. I think you have a typing
    error in your own command.
    Pegasus \(MVP\), Jul 31, 2008
  10. Use Win32_PingStatus WMI class

    var objLocalWMI = GetObject("Winmgmts:");
    var enumPingStatus = new Enumerator(objLocalWMI.ExecQuery("Select StatusCode
    from Win32_PingStatus Where Address=''"));
    if(enumPingStatus.item().StatusCode == 0)
    WScript.Echo("It's allright");
    WScript.Echo("Host is unreachable");
    //to do something....

    Set objLocalWMI = GetObject("Winmgmts:")
    Set colItems = objLocalWMI.ExecQuery("Select StatusCode from
    Win32_PingStatus Where Address=''",,48)
    For Each objItem In colItems
    If objItem.StatusCode = 0 Then
    WScript.Echo "It's allright"
    WScript.Echo "Host is unreachable"
    'to do something
    End If
    Andrew Mishechkin, Aug 13, 2008
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.