The purpose of these scripts is to update the local DHCP lease table with a remote IP Address Management (IPAM) system.
Scheduler Script
This little script is added to the scheduler and goes through the entire DHCP lease table and uploads each MAC address and IP address pair to a website.
Change out the top three variables. May also need to change out the URL depending on how the website receives data.
:local url "upload.incredigeek.com" :local username "myapiuser" :local password "passwordforapiuserwebsite" /ip/dhcp-server/lease/ :foreach i in=[find] do={ :put ([get $i address]." ".[get $i mac-address]) :local ipaddress ([get $i address]) :local macaddress ([get $i mac-address]) /tool fetch url="https://$url/api/v1/network/ipam/dynamic_ip_assignment?ip_address=$ipaddress&mac_address=$macaddress&expired=0" mode=https keep-result=no user=$username password=$password :delay 1s; }
DHCP Script
This script is to be used on the DHCP server script. Can add it by going to DHCP Server -> DHCP -> Double Click Server -> Script
Any time a new DHCP lease is obtained, this script is fired. Note that some of the variables like $leaseBound are specific to the script being used by the “DHCP server”
Also helpful to note that the script only runs if a new lease is obtained, or a lease expires and it disappears from the leases page. A DHCP renew does not trigger the script.
:local username "myapiuser" :global password "myapipassword" :global url "upload.incredigeek.com" # The maximum retries :local max 60 :local attempts 0 :local success 0 :do { :set attempts ($attempts+1); :if ($leaseBound = 0) do { :do { /tool fetch url="https://$url/api/v1/network/ipam/dynamic_ip_assignment?ip_address=$leaseActIP&mac_address=$leaseActMAC&expired=1" mode=https keep-result=no user=$username password=$password :set success 1; } on-error={ :log error "DHCP FAILED to send unassignment to $url on attempt $attempts out of $max for $leaseActMAC / $leaseActIP"; :delay 10s; } } else { :delay 1s; # see note below :local remoteID [/ip dhcp-server lease get [find where address=$leaseActIP] agent-remote-id]; :do { /tool fetch url="https://$url/api/v1/network/ipam/dynamic_ip_assignment?ip_address=$leaseActIP&mac_address=$leaseActMAC&expired=0" mode=https keep-result=no user=$username password=$password :set success 1; } on-error={ :log error "DHCP FAILED to send assignment to $url on attempt $attempts out of $max for $leaseActMAC / $leaseActIP"; :delay 10s; } } :if ($success) do { :log info "DHCP lease message successfully sent $leaseActMAC / $leaseActIP to $url"; :set attempts $max; # break out of the do..while loop } } while ( $attempts < $max ) }