views:

663

answers:

2

I need to write a script to set ip address/mask/broadcast as an alias on eth0:0 plus set the default gateway.

This solution works:

ifconfig eth0:0 <ip> netmask <mask> up
ip route replace default via <ip>

but sometimes the second call gets an error "network unavailable".

Adding a sleep between them fixes it, but is unreliable. What is the proper way to wait for the network to be ready?

The best I came up with so far is retrying the ip call a couple times. This works, but feels ugly.

+1  A: 

You could use 'ping -c1 -w' on the gateway address in a loop until it comes up.

PiedPiper
A: 

I think it is a bit strange that the interface isn't up when ifconfig returns.. I would try to skip ifconfig and only use the 'ip' command:

ip address add <ip>/<mask> dev eth0
ip route replace default via <ip>

This doesn't create a new alias interface eth0:0, it just configures an additional ip address on the primary interface, visible with 'ip address list'. I'm not sure if this works better, but it is worth a try.

Anders Westrup
ip doesn't work that well because I need to set exactly the eth0:0
n-alexander