Community discussions

MikroTik App
 
RenzoG
just joined
Topic Author
Posts: 15
Joined: Mon Oct 17, 2016 9:37 pm

dhcp-server lease disable and enable numbers.

Mon Jun 04, 2018 11:15 pm

Hello, everybody. I'm trying to block unwanted dhcp client connections. I wouldn't use firewall because there are already a lot of rules. I'm trying this way but if I simply block an ip address it will have connectivity until server renews the lease. So I should disable and enable again the number to cut off that ip address.
But there's no way to make it work.
Thanks.

:local newDynamics value=[/ip dhcp-server lease find dynamic=yes];
:local newList value="";
:foreach i in=[$newDynamics] do={
:local dynamicIP [/ip dhcp-server lease get $i address];
:local dynamicMAC [/ip dhcp-server lease get $i mac-address];
:local dynamicHOST [/ip dhcp-server lease get $i host-name];
:local dynamicNUMBER [/ip dhcp-server lease get $i number];
:set $newList value=($newList."Assigned IP address: ".$dynamicIP."\r\n"."MAC Address: ".$dynamicMAC."\r\n"."Hostname: ".$dynamicHOST."\r\n"."IP Made Static."."\r\n"."______________________________________________________________________________"."\r\n"."\r\n");
/ip dhcp-server lease make-static [/ip dhcp-server lease find mac-address=$dynamicMAC];
:delay 2s;
/ip dhcp-server lease set block-access=yes [/ip dhcp-server lease find address=$dynamicIP];
:delay 2s;
/ip dhcp-server lease disable numbers=[/ip dhcp-server lease find number=$dynamicNUMBER];
:delay 2s;
/ip dhcp-server lease enable numbers=[/ip dhcp-server lease find number=$dynamicNUMBER];
};
 
Van9018
Long time Member
Long time Member
Posts: 558
Joined: Mon Jun 16, 2014 6:26 pm
Location: Canada - Abbotsford

Re: dhcp-server lease disable and enable numbers.  [SOLVED]

Tue Jun 05, 2018 9:45 am

How do you determine if a MAC is wanted? Manually?

Something to try is changing arp mode of your ether2 to read-only. This means 2 things:
- You must manually add a mac to the arp list for any static IP on your network.
- Other clients must use DHCP to get an IP.
There is a checkbox in the DHCP Server to "Add ARP for Leases". I'm not sure what this does, but perhaps leaving it unchecked when arp mode is read-only will cause the IPs to be handed out to clients, but communication won't work since ARP is a required function of ethernet. Then to enable a client, manually add an arp entry for the mac address. I haven't tried this. Just a thought.
 
RenzoG
just joined
Topic Author
Posts: 15
Joined: Mon Oct 17, 2016 9:37 pm

Re: dhcp-server lease disable and enable numbers.

Tue Jun 05, 2018 10:35 am

van9019, many thanks for your reply. Yes, I determine manually MAC addresses I want to allow. Yes, ARP is already read only. Simply I was using "get" method for "numbers", and it couldn't work. Here the working code:

:local newDynamics value=[/ip dhcp-server lease find dynamic=yes];
:local newList value="";
:foreach i in=[$newDynamics] do={
:local dynamicIP [/ip dhcp-server lease get $i address];
:local dynamicMAC [/ip dhcp-server lease get $i mac-address];
:local dynamicHOST [/ip dhcp-server lease get $i host-name];
:set $newList value=($newList."Assigned IP address: ".$dynamicIP."\r\n"."MAC Address: ".$dynamicMAC."\r\n"."Hostname: ".$dynamicHOST."\r\n"."IP Made Static."."\r\n"."______________________________________________________________________________"."\r\n"."\r\n");
/ip dhcp-server lease make-static [/ip dhcp-server lease find mac-address=$dynamicMAC];
:delay 5s;
/ip dhcp-server lease set block-access=yes [/ip dhcp-server lease find mac-address=$dynamicMAC];
:delay 5s;
/ip dhcp-server lease disable [/ip dhcp-server lease find mac-address=$dynamicMAC];
:delay 5s;
/ip dhcp-server lease enable [/ip dhcp-server lease find mac-address=$dynamicMAC];
};

Just tested and it's working.
Thank you again.

Who is online

Users browsing this forum: No registered users and 18 guests