Add Static DHCP Leases to ARP List

From MikroTik Wiki
Jump to: navigation, search

This script will update the ARP List with all static, enabled leases. It makes the changes in place (i.e. it doesn't delete all the ARP entries first) to minimize disruptions. Limitation of the script is that the interface for the ARP entry needs to be hardcoded for now.

:local wanInterfaceName "ether1_wan";

# Remove ARP entries that do not have static DHCP leases or are disabled

:foreach arpId in=[/ip arp find] do={

#Don't remove the dynamic entry on the WAN side
  :if ([/ip arp get $arpId interface] != $wanInterfaceName) do={


#If there is no matching entry in the lease table remove it
    :local mac [/ip arp get $arpId mac-address];
    :local leaseId [/ip dhcp-server lease find where mac-address=$mac];
    :if ($leaseId="") do={
      /ip arp remove $arpId;
      :log info ("Removing old ARP entry");

      } else={

        :if ([/ip dhcp-server lease get $leaseId disabled]) do={
         /ip arp remove $arpId;
         :log info ("Removing disabled ARP entry");


        }
       }
    }
  }

:foreach leaseId in=[/ip dhcp-server lease find where !dynamic] do={
  :local mac  [/ip dhcp-server lease get $leaseId mac-address];
  :local arpId [/ip arp find where mac-address=$mac];

  :if ($arpId="" && ![/ip dhcp-server lease get $leaseId disabled]) do={
    :local ip [/ip dhcp-server lease get $leaseId address];
    :local comment  [/ip dhcp-server lease get $leaseId comment];
#interface should not be hard coded but couldn't figure out what to do
    :local interface lan_wlan_bridge;
    /ip arp add address= $ip mac-address= $mac comment= $comment interface= $interface;
   :log info ("Adding new ARP entry");
  }
}