6

My browser works perfectly using the using Internet, however, when I tried to use this command in bash:

ping -q -w1 -c1 google.com &>/dev/null && echo online || echo offline

It gives me "offline" results. I have also tried another one in a different network:

ping -c 3 www.google.com

It returns:

PING www.google.com (74.125.193.147): 56 data bytes
Request timeout for icmp_seq 0
Request timeout for icmp_seq 1

--- www.google.com ping statistics ---
3 packets transmitted, 0 packets received, 100.0% packet loss

All of these seem to point out that the terminal couldn't reach to the internet. I have tried using wifi and lan cable, the outcomes are the same.

I need to run a program which requires connection to a server, I wonder if you have any solutions to switch it back to online. I'm on macOS 10.13 and am looking to determine from the command line if a network connection is viable.

Is this possible?

3
  • Please edit your question to fix this. Also add which browser you are using and the output of curl https://google.com/.
    – nohillside
    Commented May 28, 2019 at 14:37
  • I'm using chrome. Here's the output <HTML><HEAD><meta http-equiv="content-type" content="text/html;charset=utf-8"> <TITLE>301 Moved</TITLE></HEAD><BODY> <H1>301 Moved</H1> The document has moved <A HREF="google.com/">here</A>. </BODY></HTML> Commented May 28, 2019 at 16:03
  • 1
    If curl works I suspect some filtering done by your ISP
    – nohillside
    Commented May 28, 2019 at 16:10

3 Answers 3

5

I prefer to use the system configuration utility tool to test for reachability instead of using ping / host / nslookup or another proxy for determining if a network entity is or is not reachable.

scutil -r google.com
Reachable

The benefits of this are that if you have VPN connections, dial up, modem, or a routing conflict, this will actually test that you can reach the device and not just resolve the cached host name, etc... in my experience. (also, it's a lot harder to mess up the indirection, files, logic and you get a direct answer back in English)

Like all good command line tools, it returns 0 to let you know the answer it provides is confident and an error if you have problems testing reachability.

 -r [-W] { nodename | address | local-address remote-address }
     Check the network reachability of the specified host name, IP
     address, or a pair of local and remote IP addresses.  One or more of
     the following strings will be reported to standard output.

     Not Reachable         The specified nodename/address cannot be
                           reached using the current network configura-
                           tion.

     Reachable             The specified nodename/address can be reached
                           using the current network configuration.

     Transient Connection  The specified nodename/address can be reached
                           via a transient (e.g. PPP) connection.

     Connection Required   The specified nodename/address can be reached
                           using the current network configuration but a
                           connection must first be established.  As an
                           example, this status would be returned for a
                           dialup connection that was not currently active
                           but could handle network traffic for the target
                           system.

     Connection Automatic  The specified nodename/address can be reached
                           using the current network configuration but a
                           connection must first be established.  Any
                           traffic directed to the specified name/address
                           will initiate the connection.

     Local Address         The specified nodename/address is one associ-
                           ated with a network interface on the system.

     Directly Reachable Addresss
                           Network traffic to the specified node-
                           name/address will not go through a gateway but
                           is routed directly to one of the interfaces on
                           the system.

     The reachability can also be monitored by specifying the -W (watch)
     option.  This will result in the current status being reported as
     well as the status when/if the network configuration changes.

     A zero exit status will be returned when the reachability status is
     reported correctly.  A non-zero exit status will be returned if
     errors are detected with an error reported to standard error.

Since Apple's index of manual pages is a PITA to use, here's a hopefully more stable link to the entire manual page online: https://ss64.com/osx/scutil.html

As a bonus - here is another decent Q&A relating to scutil and checking resolution: nslookup & dig fail; ping, traceroute, and scutil -r work

5
  • 1
    This really doesn't have the same meaning as the command the question was about. The question centers around checking whether or not you have an internet connection by sending a packet to Google and seeing if you get a response back. Your answer does not send any packets, nor does it say whether or not you have an Internet connection at all. So depending on what this needs to be used for, it could fool the system into failing by assuming an Internet connection is there, but it is really not. Ofcourse in practice [...]
    – jksoegaard
    Commented May 28, 2019 at 18:07
  • [...] on home user systems, having google.com reachable usually means that they have an internet connection. But only usually.
    – jksoegaard
    Commented May 28, 2019 at 18:09
  • @jksoegaard I so appreciate the comments. I’ll try and mess with this and wireshark to confirm no packets leave in a test with my setup. I’ve never caught scutil being wrong, it I haven’t tried to break it or really validate. Thanks for planting a bug!
    – bmike
    Commented May 28, 2019 at 19:30
  • 2
    The way scutil reachability test works is specifically by not sending a packet out, but instead testing whether or not a packet would be sent out if a request was made. It is the same way the Reachability API works on iOS.
    – jksoegaard
    Commented May 28, 2019 at 19:36
  • So I really need to edit my answer as part seems incorrect or misleading.
    – bmike
    Commented May 28, 2019 at 19:48
4

Your problem is that you're using invalid options for the ping command. It seems likely that you have copied a command line intended for use on Linux, and tried to use it unmodified on macOS.

The specific problem here is that Linux uses "-w" to specify timeouts, whereas macOS uses "-t". This means that your command line should instead be this:

ping -q -t1 -c1 google.com &>/dev/null && echo online || echo offline
6
  • Shouldn't ping -c 3 www.google.com work in any case?
    – nohillside
    Commented May 28, 2019 at 15:14
  • Well, he wrote that he tried it on a "different network". That network seems to not have an internet connection, or have only a firewall/filtered internet connection. I have corrected the mistake in the command line he's trying to use, so that's why I did not focus on helping him with his debug commands as well.
    – jksoegaard
    Commented May 28, 2019 at 15:32
  • I have tried ping -q -t1 -c1 google.com &>/dev/null && echo online || echo offline, it also comes out as "offline". Commented May 28, 2019 at 16:02
  • 1
    @ZeyanZhong Try it without redirecting output to /dev/null, so you can actually see what's happening: ping -q -t1 -c1 google.com Commented May 28, 2019 at 17:51
  • @GordonDavisson This is the output: PING google.com (74.125.193.101): 56 data bytes --- google.com ping statistics --- 1 packets transmitted, 0 packets received, 100.0% packet loss Commented May 29, 2019 at 9:59
0

Your command ping -c 3 www.google.com should have produced 3 internet response packets of the type ICMP ECHO REPLY. The ping command sends ECHO packets using the ICMP protocol, and the responses (if any) are ECHO REPLY packets. The curl command on the other hand, sends HTTP packets using the TCP protocol. Since the latter works and the former doesn't, there is probably something between your machine and www.google.com that blocks the ICMP protocol. A lot of misconfigured firewalls do this, so that is where I would start looking for the cause.

Blocking ICMP is bad practice since it stops things like Path MTU Discovery from working (MTU = Maximum Transmission Unit size). This stops most traffic from working if the remote MTU is smaller than the local MTU. So it's an extremely bad idea to block ICMP in the firewall.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .