Ping test - display message if fail

2019-08-08 02:14发布

问题:

I wish to run a script at logon each day to test if a PC on the local network is online or not. A message should only be displayed if the PC is offline.

For test purposes using a batch file the closest I have got is this -

@echo off  
ping -n 1 192.168.0.6 >nul &&(
  START CMD /C "ECHO online && PAUSE"
)||(
  START CMD /C "ECHO offline && PAUSE"
)

which opens a window relative to the 'online' message but if I change the IP address to an incorrect one I still get the same message box.

In case it is important both PC's are running windows 10

Thanks

回答1:

you probably get Reply from <localhost>: destination unreachable, which is "success". Better search for TTL=:

ping -n 1 <address> |find "TTL=" && (
  echo Online
) || (
  echo Offline
)