I have a script running that will start up an interface with a new IP.
If the script comes up with an IP that is already in use i am in trouble.
Is it possible that the script set up the new Ip, detects that this IP is already in use and then check the next one, until it finds an ip that works ?
B=192.168.1.
I=1
while [ "$I" != "256" ]
do
ifconfig eth0:1 $B.$I
# check here that IP is Ok !
let "I=I+1"
done
I cannot use DHCP.
No, if you use an IP that's already in use, unpredictable behaviour will follow. Either your network or the other machine's network will cease to work, or will work randomly, etc. The right thing to do is use DHCP.
You can use
arping
like this:And check the return value (
$?
):0
: no conflict1
: conflictYou can execute it before the
ifconfig
as it does not need an IP address (being ARP). So no 'unpredictable' behaviour expected.Make a mapping of all your new server MAC addresses and what IP you will assign them. Have the script find the MAC and look up its IP. You need only update the mapping table for each new server. You have to copy the script to each new server anyway, so copying the table also and updating it is no big deal.
If this sounds like a lot of work, congratulations, you have re-implimented DHCP, very poorly.