Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

Before marking as a duplicate: this is not about dynamic DNS or similar things.

I know how to run a script every time my networks goes up; it is a simple matter of adding a script in the directory /etc/NetworkManager/dispatcher.d like this one:

#!/bin/bash

IF=$1
STATUS=$2

case "$2" in
        up)
        logger -s "NM Script up $IF triggered"
        su rmano -c /home/romano/bin/myscript 
        ;;
        down)
        logger -s "NM Script down $IF triggered"
        ;;
        *)
        ;;
esac

In my case, myscript is a very simple ifconfig -a > ~/Dropbox/myifconfig.txt --- I use it because I need to know my local IP from everywhere in the University, and it will change often.

So far so good, the system works ok. But unfortunately the DHCP here is configured so that the IP sometime is changed without a down/up of the interface. In that case the script is (logically) not run, and the file is not updated.

I cannot use a DynDNS approach because the change is in the local IP, not the externally visible one.

I could poll, by simply putting the script in cron and execute it every minute or write a slightly more complex one (...if the IP is changed write the file otherwise do nothing) and putting it again as a background task, but it is not elegant. So the question is:

Is there any way to trigger a script when my local IP changes?

UPDATE 1

I tried to put a script in /etc/dhcp/dhclient-enter-hooks.d/ , based on the existing /etc/dhcp/dhclient-enter-hooks.d/resolvconf, but it will not be triggered. I suspect (confirmation needed) that NM (network manager) is doing the dhcp negotiation by itself, without calling the dhcp command...

share|improve this question
1  
it looks like it should be possible via a dhclient-enter-hooks.d script... but I've never tried it! The existing /etc/dhcp/dhclient-enter-hooks.d/resolvconf script might be helpful in terms of syntax and what signals to look for ("$reason" == "BOUND" maybe?) –  steeldriver Mar 20 at 16:18
    
@steeldriver it seems that the script is not triggered. I suspect that NM takes care of DHCP handling... will look around. Thanks anyway. –  Rmano Mar 20 at 17:07

2 Answers 2

up vote 5 down vote accepted
+50

According to the man page for NetmorkManager, one of the events is

dhcp4-change
          The DHCPv4 lease has changed (renewed, rebound, etc).

I think you can simply change

up) 

to

dhcp4-change|up)
share|improve this answer
    
It (sort-of) works. The script is triggered even a bit too much; even a renew without changes in IP will trigger it. I imagine I will have to do a bit more bash scripting. Moral: read all the content of the man page! –  Rmano Mar 26 at 15:53
    
Will wait a bit (you never know...) and then assign the bounty. Thanks! –  Rmano Mar 26 at 15:54

Although NetworkManager is using dhclient, it provides its own binaries as a replacement for the dhclient-scripts. (For reference: you can find the NM binary at /usr/lib/NetworkManager/nm-dhcp-client.action).

Maybe you could take a different approach: NM is issuing an DBus signal on all events. You could listen on the system DBus for the appropiate event and trigger your script based on this...

share|improve this answer

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.