Community discussions

MikroTik App
 
User avatar
BrasDeutscher
newbie
Topic Author
Posts: 43
Joined: Sat Sep 03, 2016 12:31 am
Location: Brazil

Make Automatic static dhcp-leases

Sat Sep 03, 2016 2:35 am

## Part-1 Create automatic Static dhcp Leases ##
## Written by:BrasDeutscher,PA,Brazil ##
## Tested RouterOS v637rc27 ##
{
## If You not run Hotspot than You can set Your own FlatRate below example "1m/10m" ##
## If no leaf it in blanc, do nothing below ##
:global r ""


## Enter the dhcp server name here ##
:global sv "1"


## Enter your lease Validity Number here, after this the unused lease will be removed ##
:global l "6"

## Enter the Validity letter here, example m or h or d, my default set it 6h ##
:global lx "h"

## !!-- The Number in comments is the number of already assigned Leases --!! ##
## !!-- PLEASE CHANGE NOTHING MORE BELOW --!! ##
:global ip 0.0.0.1
:global ipx
:global ipy
:global mac F1:F1:F1:F1:F1:F1
:global macx
:global lt 10m1s
:global gc
:global cc
:global sc
:global mks
:global a
:global a1
:global date [/system clock get date ]
:global la ("$l" . "$lx")
:global laf
:global lafx
:set mks [/ip dhcp-server lease make-static [find where server="$sv" dynamic ]];
:if ([/ip dhcp-server lease find where server="$sv" address=$ip ! disabled ] = "" ) do={
/ip dhcp-server lease add block-access=yes mac-address=$mac address=$ip server="$sv" rate-limit="$r" comment=0;
}
## Part-2 Create automatic Static dhcp Leases ##
:foreach s in=[/ip dhcp-server lease find where server="$sv" ! dynamic ! disabled ] do={
:if ([/ip dhcp-server lease get $s comment ] = "" ) do={
:set macx [/ip dhcp-server lease get $s mac-address ];
:set ipx [/ip dhcp-server lease get $s address ];
:set gc [/ip dhcp-server lease get [find where address=$ip ] comment ];
:set cc ("$gc"+"1" );
:set a [/ip dhcp-server lease set [find where server="$sv" mac-address=$macx address=$ipx ] use-src-mac=yes rate-limit="$r" \
lease-time="$lt" address-lists="$date" comment="$cc" ];
:set sc [/ip dhcp-server lease set [find where address=$ip ] comment=$cc ];
:log warning ("Make Static Mac:$macx IP:$ipx on Server:$sv Rate-Limit:$r # $cc");
} else={
## Part-3 Removing unused Leases ##
:set laf [/ip dhcp-server lease get $s last-seen ];
:set lafx [:len $laf ];
:if ("$lafx" > 5 ) do={
:if ("$laf" > "$la" ) do={
:set ipy [/ip dhcp-server lease get $s address ];
:set a1 [/ip dhcp-server lease remove [find where address=$ipy ]];
:log warning ("Liberate IP:$ipy nach $laf");
}}}}}
You do not have the required permissions to view the files attached to this post.
 
Zero3K
just joined
Posts: 17
Joined: Sat Apr 22, 2017 11:25 pm
Location: Louisville, KY, USA

Re: Make Automatic static dhcp-leases

Tue May 16, 2017 3:42 am

I am getting a syntax error in the script that is used in the 2nd part when running it on the latest RouterOS RC.

Who is online

Users browsing this forum: No registered users and 15 guests