Wait for Network Interface Before Executing Command

前端 未结 3 1335
情歌与酒
情歌与酒 2020-12-20 15:19

I have a couple ideas on how I would achieve this. Not sure how I would script it.

Method 1: (probably the better choice)

Create a loop that pings a server u

3条回答
  •  攒了一身酷
    2020-12-20 16:12

    I've tested this on a board which is configured via DHCP.

    The assumption is that if a default gateway exists on a specific interface (in this case eth0), then this is due to the fact that the board has gotten assigned an IP (and thus the default gateway) by the DHCP server, which implies that networking is up and running.

    My issue was that for me networking is considered to be up as soon as machines in the LAN/intranet can be accessed, even if there exists no internet connectivity (ie 8.8.8.8 or www.google.com is not accessible). I also don't want to ping specific IPs or domain names in the intranet because I don't want to make assumptions about the subnet or which devices will definitely be up and what their IP or domain name is.

    while ! ip route | grep -oP 'default via .+ dev eth0'; do
      echo "interface not up, will try again in 1 second";
      sleep 1;
    done
    

提交回复
热议问题