OpenWrt Forum Archive

Topic: Running a script when WAN ip changes

The content of this topic has been archived on 27 Apr 2018. There are no obvious gaps in this topic, but there may still be some posts missing at the end.

Hi,
How can I run a script every time my internet facing public ip address changes (this happens 2-4 times a day with my ISP)?
Is there a way to subscribe to this event?

(If it matters, in this script I need only to make a single HTTP get request)

You could add a cron entry with a script that check every tot minutes if the wan ip is not the same, with a simple if condition to execute an external script.
Hope it helps.
Regards

(Last edited by kalauz on 20 Apr 2014, 11:33)

Yeah I have thought about that... But I was hoping there is a better way. I am not an expert on this but I think Tomato had an event like WAN UP or something that allowed me to hook into and run a script. Doesn't OpenWRT have something similar?

(Last edited by itaysk on 20 Apr 2014, 11:42)

there is a hotplug trigger for wan-up initiated by udhcpc script

have a look at udhcpc.user scripts which is called from udhcpc on dhcp requests-answers

there are some solutions already to wan IP changes a.s.o just search

regards
3zl

3zl wrote:

there is a hotplug trigger for wan-up initiated by udhcpc script

have a look at udhcpc.user scripts which is called from udhcpc on dhcp requests-answers

there are some solutions already to wan IP changes a.s.o just search

regards
3zl

Thank you 3zl, I am new to OpenWRT and to this level of linux so I took a few days to familiarize myself with the topic before I respond, as well as setup a VM with OpenWRT for my experiments.
I now understand how hotplug works. However I can't get the expected behaviour.

(1) For debugging, I have added 2 hotplug scripts under iface and net.
Both just log the hotplug parameters (action, interface,device)
After boot, I see this in the log:

(from net) add lo
(from iface) ifup loopback lo
(from net) add eth0
(from net) add eth1
(from net) add br-lan
(from iface) ifup lan br-lan

so far it's OK.
now I try to ifconfig eth0 down and then up, but the log remains the same.
What am I missing?

(2) Regarding udhcpc - I didn't find the udhcpc.user file in my openWRT. Is it there by default?
(3) If it were there, are you sure it would have helped me? I understand this is the dhcp server for the router, so when a computer in the network gets a new ip maybe it would have triggered an event. but i am interested in the ip change of the wan interface of the router, not a device on the network.

(4) As far as your suggestion to search for an existing solution - I haven't found any references.
The closest thing I have found is the ddns.scripts package. From reading the source, I understand they are scanning for ip change every 10 minutes. I don't understand why they chose this implementation instead of responding to an event.

Thanks again for helping!

(Last edited by itaysk on 24 Apr 2014, 17:57)

udhcpc is the dhcpc client daemon running for every dhcp-configuread interface  like wan
seen with command 'ps' and its script.

It does give a hotplug event on dhcp-events on the interface  ( renew,bound,defconfig )

Your hoock to take appr. actions might be in the /etc/udhcp.user with following example to get more insight

#executed on every udhcpc event to log the parameters given from udhcpc
logger "p1:$1 HOME:$HOME PATH:$PATH iface:$interface ip:$ip siaddr:$siaddr" logger "subn:$subnet router:$router dns:$dns namesrv:$namesrv"
  
my_interface=wan
  . /usr/share/libubox/jshn.sh  #just out of interest
  json_load "$(ubus call network.interface.${my_interface} status)" #get real interface(device) ->
  json_get_var my_device device
  test "$interface" == "$my_device" || exit 0  #do nothing

case "$1" in                        
        deconfig)
            #switch batman-adv client->server
        logger "udhcpc.user p1:$1 interface:$interface device:$device"
        logger "action here "
   
    ;;                       
        renew|bound)             
              logger "udhcpc.user p1:$1 interface:$interface device:$device"
        logger "action here if ip change"
    ;;                       
esac

code snippet is not tested, so take with a grain of salt ..should work though.
put that in /etc/udhcpc.user and logread to see the parameters of actions (ifup ifdown IP-change)
At "renew|bound" should be the code to react on your wan IP-change

Further reading :
   https://dev.openwrt.org/browser/trunk/p … ?rev=34704
   http://dev.man-online.org/man8/udhcpc/

I did some udhcpc-related programming 2 years ago, so i hope it did not change too much.

regards
3zl

The discussion might have continued from here.