{"id":4870,"date":"2023-03-17T16:24:00","date_gmt":"2023-03-17T21:24:00","guid":{"rendered":"https:\/\/www.incredigeek.com\/home\/?p=4870"},"modified":"2023-03-17T13:11:25","modified_gmt":"2023-03-17T18:11:25","slug":"mikrotik-dhcp-scripts","status":"publish","type":"post","link":"https:\/\/www.incredigeek.com\/home\/mikrotik-dhcp-scripts\/","title":{"rendered":"Mikrotik DHCP Scripts"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">The purpose of these scripts is to update the local DHCP lease table with a remote IP Address Management (IPAM) system.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Scheduler Script<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">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.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Change out the top three variables.  May also need to change out the URL depending on how the website receives data.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">:local url \"upload.incredigeek.com\"\n:local username \"myapiuser\"\n:local password \"passwordforapiuserwebsite\"\n\n\/ip\/dhcp-server\/lease\/\n:foreach i in=[find] do={ :put ([get $i address].\" \".[get $i mac-address])\n:local ipaddress ([get $i address])\n:local macaddress ([get $i mac-address])\n\/tool fetch url=\"https:\/\/$url\/api\/v1\/network\/ipam\/dynamic_ip_assignment?ip_address=$ipaddress&amp;mac_address=$macaddress&amp;expired=0\" mode=https keep-result=no user=$username password=$password\n:delay 1s;\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">DHCP Script<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">This script is to be used on the DHCP server script.  Can add it by going to DHCP Server -&gt; DHCP -&gt; Double Click Server -&gt; Script <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">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 &#8220;DHCP server&#8221;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">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.  <\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">:local username \"myapiuser\"\n:global password \"myapipassword\"\n:global url \"upload.incredigeek.com\"\n\n# The maximum retries\n:local max 60\n:local attempts 0\n:local success 0\n:do {\n  :set attempts ($attempts+1);\n  :if ($leaseBound = 0) do {\n    :do {\n      \/tool fetch url=\"https:\/\/$url\/api\/v1\/network\/ipam\/dynamic_ip_assignment?ip_address=$leaseActIP&amp;mac_address=$leaseActMAC&amp;expired=1\" mode=https keep-result=no user=$username password=$password\n      :set success 1;\n    } on-error={\n      :log error \"DHCP FAILED to send unassignment to $url on attempt $attempts out of $max for $leaseActMAC \/ $leaseActIP\";\n      :delay 10s;\n    }\n  } else {\n    :delay 1s;\n    # see note below\n    :local remoteID [\/ip dhcp-server lease get [find where address=$leaseActIP] agent-remote-id];\n    :do {\n      \/tool fetch url=\"https:\/\/$url\/api\/v1\/network\/ipam\/dynamic_ip_assignment?ip_address=$leaseActIP&amp;mac_address=$leaseActMAC&amp;expired=0\" mode=https keep-result=no user=$username password=$password\n      :set success 1;\n    } on-error={\n      :log error \"DHCP FAILED to send assignment to $url on attempt $attempts out of $max for $leaseActMAC \/ $leaseActIP\";\n      :delay 10s;\n    }\n  }\n  :if ($success) do {\n    :log info \"DHCP lease message successfully sent $leaseActMAC \/ $leaseActIP to $url\";\n    :set attempts $max;  # break out of the do..while loop\n  }\n} while ( $attempts &lt; $max )\n}<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>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 &hellip; <a href=\"https:\/\/www.incredigeek.com\/home\/mikrotik-dhcp-scripts\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[452],"tags":[301,388,173],"class_list":["post-4870","post","type-post","status-publish","format-standard","hentry","category-mikrotik","tag-mikrotik","tag-routeros","tag-script"],"_links":{"self":[{"href":"https:\/\/www.incredigeek.com\/home\/wp-json\/wp\/v2\/posts\/4870","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.incredigeek.com\/home\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.incredigeek.com\/home\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.incredigeek.com\/home\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.incredigeek.com\/home\/wp-json\/wp\/v2\/comments?post=4870"}],"version-history":[{"count":4,"href":"https:\/\/www.incredigeek.com\/home\/wp-json\/wp\/v2\/posts\/4870\/revisions"}],"predecessor-version":[{"id":4894,"href":"https:\/\/www.incredigeek.com\/home\/wp-json\/wp\/v2\/posts\/4870\/revisions\/4894"}],"wp:attachment":[{"href":"https:\/\/www.incredigeek.com\/home\/wp-json\/wp\/v2\/media?parent=4870"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.incredigeek.com\/home\/wp-json\/wp\/v2\/categories?post=4870"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.incredigeek.com\/home\/wp-json\/wp\/v2\/tags?post=4870"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}