Community discussions

MikroTik App
 
CR24
Frequent Visitor
Frequent Visitor
Topic Author
Posts: 61
Joined: Wed Dec 04, 2013 10:32 pm

DHCP Binding Triggers Script

Tue Dec 11, 2018 9:37 pm

I currently have a script that adds bound DHCP entries to an address list, this script is set to run every 30 seconds. Rather than have it scheduled to run every 30 seconds is it possible to have another script that parses the logs and searches for an entry with "Msg-Type = ack" in it then runs the first script? If so can someone assist with the creation of that script?
Last edited by CR24 on Tue Dec 11, 2018 9:57 pm, edited 1 time in total.
 
User avatar
sebastia
Forum Guru
Forum Guru
Posts: 1782
Joined: Tue Oct 12, 2010 3:23 am
Location: Antwerp, BE

Re: DHCP Binding Triggers Script

Tue Dec 11, 2018 9:51 pm

Hey, you could attach a script to dhcp server, to do something on ip assignment.

See https://wiki.mikrotik.com/wiki/Manual:I ... er#General -> lease-script (string; Default: "")
 
CR24
Frequent Visitor
Frequent Visitor
Topic Author
Posts: 61
Joined: Wed Dec 04, 2013 10:32 pm

Re: DHCP Binding Triggers Script

Tue Dec 11, 2018 10:24 pm

I tried this, no luck, did I do something wrong?
#Add Lease to Address List
#DCHP Script
 
 
:if ($leaseBound = "1") do={
    /ip firewall address-list add list=DHCP-WL address=($leaseActIP) timeout="03:00:00"
}
Last edited by CR24 on Tue Dec 11, 2018 10:34 pm, edited 1 time in total.
 
User avatar
sebastia
Forum Guru
Forum Guru
Posts: 1782
Joined: Tue Oct 12, 2010 3:23 am
Location: Antwerp, BE

Re: DHCP Binding Triggers Script

Tue Dec 11, 2018 10:32 pm

To start with (debug phase), try logging the values received.
You could also test the script by invoking it manually, and when happy paste/call it.
 
CR24
Frequent Visitor
Frequent Visitor
Topic Author
Posts: 61
Joined: Wed Dec 04, 2013 10:32 pm

Re: DHCP Binding Triggers Script

Tue Dec 11, 2018 10:34 pm

It worked actually, just only for new entries, I was reconnecting a current entry to the network and the lease was refreshing but it was not triggering the script. Do you know what tweaks I would need to make to get it to refresh current leases?
 
User avatar
sebastia
Forum Guru
Forum Guru
Posts: 1782
Joined: Tue Oct 12, 2010 3:23 am
Location: Antwerp, BE

Re: DHCP Binding Triggers Script

Tue Dec 11, 2018 10:46 pm

If it behaves like the dhcp client part, it will only be triggered on assignment, de-assignment & change (!=previous ip).
Hack: remove all current leases ;-)
 
CR24
Frequent Visitor
Frequent Visitor
Topic Author
Posts: 61
Joined: Wed Dec 04, 2013 10:32 pm

Re: DHCP Binding Triggers Script

Tue Dec 11, 2018 10:50 pm

My worry is once the timeout expires on the address list, will it get re-added to the address list if the lease is still active?
 
CR24
Frequent Visitor
Frequent Visitor
Topic Author
Posts: 61
Joined: Wed Dec 04, 2013 10:32 pm

Re: DHCP Binding Triggers Script

Tue Dec 11, 2018 10:57 pm

Instead of running the script at the DHCP script I just had to call the system script that I was running on a schedule before. Got it working as I need.

Thanks for the help.





#Add Lease to Address List
 
 
:if ($leaseBound = "1") do={
    /system script run 0
}
 
User avatar
sebastia
Forum Guru
Forum Guru
Posts: 1782
Joined: Tue Oct 12, 2010 3:23 am
Location: Antwerp, BE

Re: DHCP Binding Triggers Script

Tue Dec 11, 2018 11:05 pm

Good to hear. Note: you might want to run script by name, so you won't brake it in future
/system script run <name>

Edit: indeed a "typo"
Last edited by sebastia on Wed Dec 12, 2018 1:41 am, edited 1 time in total.
 
User avatar
CZFan
Forum Guru
Forum Guru
Posts: 2098
Joined: Sun Oct 09, 2016 8:25 pm
Location: South Africa, Krugersdorp (Home town of Brad Binder)
Contact:

Re: DHCP Binding Triggers Script

Tue Dec 11, 2018 11:37 pm

Good to hear. Note: you might want to run script by name, so you won't brake it in future
/system run script <name>
/system script run <name> :-)
 
User avatar
eset
Member Candidate
Member Candidate
Posts: 108
Joined: Tue Dec 15, 2015 5:15 pm
Contact:

Re: DHCP Binding Triggers Script

Sun Jan 09, 2022 10:45 pm

When triggers those scripts to run? I removed a static entry from lease. Re-added and host bounded , received IP and still the scripts under /dhcp-server lease-script didn't run

[admin@Mike[1]] /user ssh-keys private> /ip dhcp-server print detail
Flags: D - dynamic, X - disabled, I - invalid
 0    name="USER_LAN" interface=USER lease-time=10m address-pool=static-only bootp-support=dynamic bootp-lease-time=forever authoritative=after-2sec-delay use-radius=no
      lease-script=:foreach i in=[/ip dhcp-server lease find where server="USER_LAN"] do={ \\r\n:local ip [/ip dhcp-server lease get value-name=address $i]; \\r\n:local hostname [/ip dhcp-server lease get value-name=comment $i];
             \\r\n:system ssh user=konrad address=10.0.1.1 \\r\ncommand="check=\$(sudo pdnsutil list-zone hw.sonic 2> /dev/null | grep ^$hostname | awk '{print \$5}'); \r\nif [[ -n \$check ]]; then \\r\nif [ \$check != $ip ]; then echo
             \"Replacing with new\" $ip \"IP\"; \\r\nsudo pdnsutil replace-rrset hw.sonic $hostname A 60 $ip > /dev/null 2>&1; \\r\nelse echo $hostname \"already exists\"; fi; \\r\nelse echo \"Adding\" $hostname $ip; \\r\nsudo pdnsutil
             add-record hw.sonic $hostname A $ip > /dev/null 2>&1; fi"}

from what I see in the manual
Script that will be executed after lease is assigned or de-assigned. Internal "global" variables that can be used in the script:
leaseBound - set to "1" if bound, otherwise set to "0"
leaseServerName - dhcp server name
leaseActMAC - active mac address
leaseActIP - active IP address
lease-hostname - client hostname
lease-options - array of received options

I can modify my script to perform only for that Bound which connects yes ? Rather than make foreach loop on all entries. But first why the script isn't triggered

Who is online

Users browsing this forum: No registered users and 15 guests