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 am running a Debian Lenny server with two network interfaces. The first interface eth0 is connected to the LAN and receives an IP-address from the DHCP-server on this LAN. The second interface eth1 is connected directly to a NAS, on this NAS a DHCP server is installed so when the NAS is powered on, eth1 receives an IP-address from this.

I want to run a shell script automatically when eth1 has received an IP-adddress. How can this be done?

share|improve this question

3 Answers 3

up vote 4 down vote accepted

Put the shell script in an up argument for eth1's configuration stanza in /etc/network/interfaces.

share|improve this answer
    
Would this not result in the script running when the server boots and not when recieving an IP-address? Because i am quite sure that the interface is up all the time, just without any IP-address before the NAS is running. But of course i could be wrong? –  zx883 Nov 30 '09 at 22:18
    
From the manpage: "Run command after bringing the interface up" The interface is not deemed up until it has completed configuration -- in this case, getting it's address from DHCP. –  womble Nov 30 '09 at 22:25

Your system is already running a script when it receives an IP address via DHCP. This is exactly how the information provided by the DHCP server is used to actually configure things like your ip address, resolver configuration, routing, and so forth.

If you're using the ISC dhcp client (called "dhclient"), then the script is typically something like /etc/dhclient-script. If you're using another DHCP client (pump, etc), it probably has similar functionality.

share|improve this answer

you can also run a cronjob to check your ipadress frequently.

you can use a script with the following content:

if [ $(ifconfig  eth1 |grep "inet a"| cut -f 2 -d ":" | cut -f 1 -d " ") = "youripadress" ];
then 
     echo "oh, i got an ip";
     #do some nasty stuff here!

fi

but you have to build something around in a way the script is only executed 1 time. e.g. save ip to a file and check if the ip changed or something like that. The answer from womble would be the better solution if you just want to check if the interface gets up exactly one time.

share|improve this answer
    
This would work, but the cronjob would then have to be configured to run every minute, this would run the script 10080 times in a week, maybe the NAS is only running every 14 day.. –  zx883 Nov 30 '09 at 22:26
    
I did not see the paragraph at the bottom, when posting my last comment. –  zx883 Nov 30 '09 at 22:46

Your Answer

 
discard

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