Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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.

share|improve this question
3  
Try a for loop: for I in {1..255} then you won't have to increment the variable yourself. –  Dennis Williamson Apr 20 '10 at 12:15
    
Perhaps if you fully explained why you can't use DHCP for this, we could better advise you. –  mfinni Apr 20 '10 at 14:07
add comment

3 Answers

up vote 10 down vote accepted

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.

share|improve this answer
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 '10 at 17:29
add comment

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.

share|improve this answer
8  
+1 that's exactly what DHCP is for ! –  Antoine Benkemoun Apr 20 '10 at 11:13
add comment

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.