How to trigger a script on dhcp lease renew?

I want to update a dyndns service by doing curl only when the DHCP lease is renewed with new IP. I came across dhcpcd exit-hook to call the script on renew, but the dhcpcd is creating a new secondary IP lease every time. This also causes a new route addition and it fails the lan routing of another service, which works fine if there is only one IP without dhcpcd.

I also tried disabling Network manager before enabling dhcpcd service but the same issue.

If there are any other alternatives to dhcpcd exit hook for this trigger, I am happy to try. Thanks

ip addr

2: wlan0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default qlen 1000
    link/ether dc:1b:xx:xx:xx brd ff:ff:ff:ff:ff:ff
    inet 10.194.126.133/19 brd 10.194.127.255 scope global dynamic noprefixroute wlan0
       valid_lft 21924sec preferred_lft 21924sec
    inet 10.194.123.85/19 brd 10.194.127.255 scope global secondary dynamic noprefixroute wlan0
       valid_lft 23386sec preferred_lft 20459sec

systemctl status dhcpcd@wlan0.service
● dhcpcd@wlan0.service - dhcpcd on wlan0
     Loaded: loaded (/usr/lib/systemd/system/dhcpcd@.service; enabled; vendor preset: disabled)
    Drop-In: /etc/systemd/system/dhcpcd@.service.d
             └─no-wait.conf
     Active: active (running) since Tue 2022-03-22; 24s ago
    Process: 17032 ExecStart=/usr/bin/dhcpcd -b -q wlan0 (code=exited, status=0/SUCCESS)
   Main PID: 17034 (dhcpcd)
      Tasks: 7 (limit: 9309)
     Memory: 2.1M
        CPU: 62ms
     CGroup: /system.slice/system-dhcpcd.slice/dhcpcd@wlan0.service
             β”œβ”€17034 "dhcpcd: wlan0 [ip4] [ip6]"
             β”œβ”€17035 "dhcpcd: [privileged proxy] wlan0 [ip4] [ip6]"
             β”œβ”€17036 "dhcpcd: [network proxy] wlan0 [ip4] [ip6]"
             β”œβ”€17037 "dhcpcd: [control proxy] wlan0 [ip4] [ip6]"
             β”œβ”€17038 "dhcpcd: [network proxy] fe80::c77f::"
             β”œβ”€17043 "dhcpcd: [BPF ARP] wlan0 10.194.123.85"
             └─17064 "dhcpcd: [network proxy] 10.194.123.85"

Mar 22 11:57:02 ceph systemd[1]: Started dhcpcd on wlan0.
Mar 22 11:57:02 ceph dhcpcd[17035]: wlan0: connected to Access Point: some_wifi
Mar 22 ceph dhcpcd[17035]: wlan0: IAID xx:xx:xx:xx
Mar 22 ceph dhcpcd[17035]: wlan0: soliciting an IPv6 router
Mar 22 ceph dhcpcd[17035]: wlan0: rebinding lease of 10.194.123.85
Mar 22 ceph dhcpcd[17035]: wlan0: probing address 10.194.123.85/19
Mar 22 ceph dhcpcd[17035]: wlan0: leased 10.194.123.85 for 23414 seconds
Mar 22 ceph dhcpcd[17035]: wlan0: adding route to 10.194.96.0/19
Mar 22 ceph dhcpcd[17035]: wlan0: adding default route via 10.194.96.1
inxi

System:
  Kernel: 5.16.15-zen1-1-zen x86_64 bits: 64 compiler: gcc v: 11.2.0
    Desktop: KDE Plasma 5.24.3 Distro: EndeavourOS base: Arch Linux
Machine:
  Type: Laptop System: ASUSTeK product: VivoBook_ASUSLaptop X412FAC_X412FA
    v: 1.0 serial: <superuser required>
  Mobo: ASUSTeK model: X412FAC v: 1.0 serial: <superuser required>
    UEFI: American Megatrends v: X412FAC.303 date: 07/17/2020
Network:
  Device-1: Intel Comet Lake PCH-LP CNVi WiFi driver: iwlwifi v: kernel
    bus-ID: 00:14.3
  IF: wlan0 state: up mac: <filter>
  IF-ID-1: docker0 state: down mac: <filter>

I also have dnscrypt-proxy running and made the resolv.conf immutable

Having two network management tools is going to be a bad thing; I’d stick with Network Manager and make that one work rather than add another into the mix.

Network Manager has a dispatcher function that can run scripts when network connections are established or change (in this case the dhcp4-change action will come in handy).

Useful reading:

Thank you. I haven’t noticed dhcp4-change earlier

1 Like

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.