3

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.

2
  • 3
    Try a for loop: for I in {1..255} then you won't have to increment the variable yourself. Apr 20, 2010 at 12:15
  • Perhaps if you fully explained why you can't use DHCP for this, we could better advise you.
    – mfinni
    Apr 20, 2010 at 14:07

3 Answers 3

10

You can use arping like this:

arping -D -I interface -c 2 ip_address > /dev/null

And check the return value ($?):

  • 0: no conflict
  • 1: conflict

You can execute it before the ifconfig as it does not need an IP address (being ARP). So no 'unpredictable' behaviour expected.

1
  • 1
    Be aware that there is no persistence here, if a server is rebooting or otherwise temporarily off the network, it will come back to find its static IP taken.
    – kmarsh
    Apr 20, 2010 at 17:29
13

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.

1
  • 8
    +1 that's exactly what DHCP is for ! Apr 20, 2010 at 11:13
2

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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .