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.
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.
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