14

I want to send a growl to myself after SuSE server finishes the boot process. I got this message socket_sendto(): unable to write to socket [101]: Network is unreachable in.

How can I check from command line if the network is reachable and wait in case it is not?


1
  • Linux one-liner to answer yes or no, are we connected to the internet: ping -c 1 www.yourtrustedserver.com | grep " 0% packet loss". What this does is ping the server with one packet, and greps the output for the string " 0% packet loss". (the space before the 0% is important) If the command returns a row, you are connected, otherwise, not connected. – Eric Leschinski Dec 8 '13 at 2:55
13

ping to an outside host can fail for a multitude of reasons, only a few of which actually say anything useful about the state of your own network.

As a first step, open a terminal window and type ip route ls

You should see an output along the lines of

shadur@equinox:~$ ip route ls
192.168.15.0/24 dev eth0  proto kernel  scope link  src 192.168.15.102 
default via 192.168.15.1 dev eth0 

This indicates that your local network is an ethernet connection (eth0) with the address 192.168.15.0, and that its default gateway through which it accesses the rest of the internet can be found at 192.168.15.1.

Next, you can try to ping that address:

shadur@equinox:~$ ping 192.168.15.1
PING 192.168.15.1 (192.168.15.1) 56(84) bytes of data.
64 bytes from 192.168.15.1: icmp_req=1 ttl=255 time=0.352 ms
64 bytes from 192.168.15.1: icmp_req=2 ttl=255 time=0.269 ms
^C
--- 192.168.15.1 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1000ms
rtt min/avg/max/mdev = 0.269/0.310/0.352/0.045 ms

If you see something similiar to the above, your own local network is, at least, fine. At this point you can start looking with more advanced tools like traceroute to see where your connection to the destination might be failing.

However, after a quick google check of what growl is actually supposed to be I get the feeling that there's something else going wrong. Can you expand your question to give us a few more details on what you're trying to do, how you're attempting it, and the full error output? The line you're currently giving us gets cut off abruptly...

1
  • 3
    ip route get 1.2.3.4 is more reliable than just doing ip route ls. The former will take into effect any rules that are in place in addition to routes. It also removes the possibility of misreading the routing table(s). – phemmer Jul 17 '12 at 12:16
5

A very simple and quick way is to use the ping command.

You can simply type

 $ ping yahoo.com

(or cnn.com or any other host) and see if you get any output back. This assumes that hostnames can be resolved (ie dns is working). If not, you can hopefully supply a valid IP address/number of a remote system and see if it can be reached.

Here's the ping man page.

Update:

As a quick example, you can check the return value (e.g., with "echo $?") from ping to see if the command succeeded (you could always pipe the output of the command to > /dev/nul). Note I use -c 1 here, but you could use more.

$ ping -c 1 yahoo.com
PING yahoo.com (72.30.38.140) 56(84) bytes of data.
64 bytes from ir1.fp.vip.sp2.yahoo.com (72.30.38.140): icmp_seq=1 ttl=52 time=83.5 ms

--- yahoo.com ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 83.593/83.593/83.593/0.000 ms

echo $?
0


$ ping -c 1 unicorns.are.here
ping: unknown host unicorns.are.here

$ echo $?
2
8
  • But how can I realize from using ping that everything is ok? – Radek Jul 17 '12 at 3:23
  • 1
    @Radek Have you tried running it yet? ping tells you if it gets replies. – jw013 Jul 17 '12 at 3:26
  • What if the server is on intranet only and doesn't have access to outside world? – Radek Jul 17 '12 at 3:35
  • @jw013: I haven't tried...I don't know to use ping from an unattended script. – Radek Jul 17 '12 at 3:36
  • 3
    @Radek it's quite simple. I assume by 'connectivity' you mean it can talk to some other host on the intranet. So run ping -c 2 -n <other host's IP> and operate on the return code: if true (0), then it has connectivity; if false (1), then it can't talk to that machine. – lxop Jul 17 '12 at 5:00
2

mtr is a tool that is similar to ping but gives you more information such as packet lose, statistics on packet travel times, etc...

0

I'd use netcat to the desired service.

For example, testing an IP port, succeding

> nc -zv 192.168.1.1 80
Connection to 172.26.0.1 80 port [tcp/http] succeeded!

...and failing

> nc -zv 192.168.1.1 22
netcat: connect to 172.26.0.1 port 22 (tcp) failed: Connection refused

with 10s timeout

> nc -zv -w 10 10.0.0.1 80
netcat: connect to 10.0.0.1 port 80 (tcp) timed out: Operation now in progress
1
  • Hi Muihlinn, welcome to U&L! Could you give an example of how you would do this? – Cyclic3 May 14 '20 at 15:46
0

I use the following function ... It defaults to quad9 but you can pass a different domain. It will timeout after 15 minutes of trying

wait_for_dns() {

   local fqhn="quad9.net"
   fqhn=${1:-$fqhn}

   printf "waiting"
   for i in {1..15}; do
      local a_record=$(dig +short +time=2 $fqhn)

      if [[ $a_record != '' ]]; then
         printf "\n"
         return 0
      fi

      printf '.'
      sleep 60
   done

   printf "\n"
   return 1
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.