MacOS – Can’t connect to Internet in bash using Mac OS

bashcommand linemacosNetworkterminal

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?

Best Answer

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