So this stems from using PPPoE and loving the /32 assignment of public IP addresses and really wanting to have that assignment in a dhcp/no encapsulation/no protocol overhead/no client side config form.
It is possible to setup /32 networking without this script. This script automates the adding and removing of the ip address needed on routeros to correctly route to and from said host.
When I started looking up how to do this I was unable to find information on exactly how so now that I have figured out a way to make this work I wanted to share it, and get feedback to hopefully make this better. Because I am 100% sure it can be done better.
There are a few things to setup on the router:
- DHCP server
- address-pool static only (I haven't tested using a pool don't, see why it wouldn't work though)
source-address - used in the script to set the local address for the IP address entry. (there might be a better way, I haven't found it yet)
- This website is awesome, It creates the value for you, just copy and paste: https://ip-pro.eu/en/mikrotik_dhcp_option_121_generator
- address-pool static only (I haven't tested using a pool don't, see why it wouldn't work though)
This allows for standardized configurations as you will see below.
But the principals apply the same if you wanted to make a network entry for a single IP. Just use /32 instead of /24
- Create a Network entry in the dhcp server for the Network This needs to be rather specific for for the 69.69.69.69/24 range:
- Network: 69.69.69.69/24 (intended address range for this statement, typically our public ip block)
Gateway: 10.10.10.1 (DHCP server Src address)
NetMask: 32 (this is critical to not breaking the internet for the client)
DNS servers: (dhcp src or whatever you want to use)
DHCP Options: Default-Route (this is option 121 that we setup)
- Network: 69.69.69.69/24 (intended address range for this statement, typically our public ip block)
Meaning, in my network at least, I could slap this on any router and start handing our /32 leases.
Its important to remember that routeros can, without this script, assign /32 leases. But in order for routing to work a IP address needs to be added to the router. That is what the script does.
Code: Select all
####Adds a IP address entry based on dhcp server src-address
/ip dhcp-server
:local interface;
:do { :set interface "$[ get $leaseServerName interface ]" } on-error={}
## uncomment to log interface selection
#:log info ("DHCP Server interface : $interface ");
:local gateway;
:do { :set gateway "$[ get $leaseServerName src-address ]" } on-error={}
## uncomment to log gateway selection
#:log info ("DHCP Server Gateway : $gateway ");
/ip dhcp-server network
:local cidr;
:do { :set cidr [:put "$leaseActIP/32"]}
/ip dhcp-server lease
:local hostname;
:do { :set hostname "$[ get [ find where server=$leaseServerName && address=$leaseActIP && mac-address=$leaseActMAC ] host-name ]" } on-error={}
## set comment and log info to display
:local forLease "$hostname / $leaseActIP / $leaseActMAC / $interface"
/ip address;
:log info ("Add IP address at DHCP Release : $forLease ");
:if ($leaseBound = "1") do={
add address=$gateway network=$leaseActIP interface=$interface comment=$forLease;
} else={
:log info ("Remove IP address at DHCP Release : $forLease ");
:foreach n in [find] do={
:if ([get $n network] = $leaseActIP) do={
remove $n;
}
}
}
####
Another important note: I have seen issues using long lease times, the script doesn't always add the IP address back in upon renewal if the lease is set too long.
I tested times of 10s, 10m, 10h, 1d, 2d, 3d. At 3 day lease times is were I saw the problem above. When that happens it takes the client offline. So far shortening the lease time fixes it.
So does static entries, but who wants to maintain all of those. Why don't we go back to static routing........