12

Is there a way to check for successful network interface link for multiple interfaces in a bash script before continuing?

Something like:

eth0 eth1 eth2 eth3 network interfaces are brought up
Wait for link detection on all 4 interfaces
Proceed
user1527227
  • 2,068
  • 5
  • 25
  • 36
  • 1
    in a loop try 1x ping in every second to some known host what are in the each network, or check ifconfig output... – clt60 Jul 25 '14 at 19:13
  • If they are up, it doesn't necessarily mean that the other hosts respond to pings. – LtWorf Jun 27 '17 at 12:05

1 Answers1

25

You can check if the network interfaces' names appear after running ifconfig -s.

Something like:

if [ $( ifconfig -s | grep eth0 ) ]; then echo "eth0 is up!"

Check this link for more details.


To make this test ongoing, you can do something like what @jm666 said:

while ! ping -c 1 -W 1 1.2.3.4; do
    echo "Waiting for 1.2.3.4 - network interface might be down..."
    sleep 1
done
jimm-cl
  • 5,124
  • 2
  • 25
  • 27
  • This is great if I just want to test once, but what if I need to actively wait for until it comes online and then proceed? – user1527227 Jul 25 '14 at 19:35
  • Why do you have a `-W` flag in there? You didn't specify a timeout for a single ping.... – user1527227 Jul 29 '14 at 20:03
  • `-c 1 -W 1` means "*try 1 echo, and wait for it for 1 second at most*". If it does not receive the echo back, then enters the if-block, shows the message, and wait for one second before trying again. – jimm-cl Jul 29 '14 at 20:16
  • If you didn't specify `-W 1`... how long would it wait for? I dont see a descrp in the man pages – user1527227 Jul 29 '14 at 20:20
  • As far as I understand, `-c 1` alone will just send one echo and wait for it to return, no matter how long it takes. Adding the `-W 1` will prevent this from waiting too long, allowing just 1 second of time to get a response. – jimm-cl Jul 29 '14 at 20:23
  • 1
    The while loop as presented (`while ! ping -c 1 -W 1 1.2.3.4;`) doesn't work for me on Ubuntu 18... It will correctly continue if the network is not available but will also continue if I get a valid ping result. I had to change it to `while ! (ping -c 1 -W 1 1.2.3.4 | grep -q 'statistics');` – logidelic Dec 18 '19 at 15:01