1

I'm trying to implement the following scene on a Raspberry Pi with wheezy in a bash script. I do that because I want a device always get a new IP if the DHCP is online, and at the same time I want it to keep the last used IP in the case the DHCP is offline. On top, IP address could change, and I have many many devices, so simply setup static IPs is not my choice:

  • Boot (not in the script ;) )
  • Switch on DHCP and try to get IP address
  • Check if I got one

   a) Got IP (DHCP successful)

       -> Save IP infos to file

   b) Didn't get IP (DHCP failed)

       -> Do nothing here

  • Set fixed IP address based on the information in the file

Now the point is that this works pretty awesome if I run the script manually, but if I let it run automatically after startup, the DHCP will fail but will still give me an IP address. Of course my script thinks that the DHCP got an ip address, but it's not the right one!
sys.log tells me that:

dhclient: No DHCPOFFERS received.
dhclient: Trying recorded lease 192.xxx.xx.xx
ifplugd(eth0)[1602]: client: Trying recorded lease 192.xxx.xx.xx

Now the question is:

  • Can I deactivate the DHCP client service (I think it is ifplugd with dhclient) giving me that fake IP address?
  • Or can I elsewise detect, that the DHCP request was not successful (and forfeit the fake address)?

Script:

#!/bin/bash

echo Switch off Ethernet >> log.log
ifconfig eth0 down
echo Set DHCP mode for Ethernet >> log.log
ifconfig eth0 up
dhclient -r eth0
dhclient eth0
echo Now check if we got ip address >> log.log
countLines=$(ifconfig eth0 | grep "inet addr" | wc -l)
if [ $countLines -ne 0 ]; then
  echo Found ip address via DHCP - Save it to ip.txt >> log.log
  ifconfig eth0 | grep "inet addr" > ip.txt
  echo Also save the resolv.conf - DNS settings >> log.log
  cp /etc/resolv.conf dns.txt
  #grep domain-name-servers /var/lib/dhcp/dhclient.eth0.leases | tail -1 > dns.txt
else
  echo No new ip address - no DHCP online! Use the old settings instead... >> log.log
fi
echo Switch off DHCP - and release the current lease >> log.log
dhclient -r eth0
ipaddress=$(<ip.txt)
ipaddress=${ipaddress#*addr:}
ipaddress=${ipaddress% Bcast*}
broadcast=$(<ip.txt)
broadcast=${broadcast#*Bcast:}
broadcast=${broadcast% Mask*}
netmask=$(<ip.txt)
netmask=${netmask#*Mask:}
dnsserver=$(<dns.txt)
dnsserver=${dnsserver#*servers }
dnsserver=${dnsserver%;*}
echo Set static ip address now ip: $ipaddress, broadcast: $broadcast, netmask: $netmask... >> log.log
ifconfig eth0 $ipaddress broadcast $broadcast netmask $netmask
echo Set the nameserver >> log.log
cp dns.txt /etc/resolv.conf

It really puzzles me, that the script works out of the bash, but not after started automatically with system startup!

Micky
  • 499
  • 6
  • 12
  • Have you looked at this answer: http://stackoverflow.com/questions/12727175/set-static-ip-if-not-obtained-from-dhcp-script ? – djondal Mar 11 '14 at 13:00

1 Answers1

2

Ok Mate,

Your problem is that your script believes that ifconfig will get no IP address if the DHCP is not found.

If dhclient fail to discover the DHCP server, eth0 will get 169.254.*.* address (self assigned)

You need to save what you got before run the dhclient.

Eg. OLDIP=ifconfig eth0 | sed ... Inet ... #come with your SED statement

Then you continue with:

dhclient eth0
If [$? !=0];then
    Ifconfig eth0 $OLDIP $OLDMASK

I'm writing from my phone so obviously the syntax is not correct.

Hope it helps

Miheretab Alemu
  • 956
  • 2
  • 20
  • 43
Gian
  • 21
  • 2