Skip to content

Instantly share code, notes, and snippets.

@elico
Last active June 1, 2024 18:13
Show Gist options
  • Save elico/28f403bb8f7209cf6520b83eb73413e9 to your computer and use it in GitHub Desktop.
Save elico/28f403bb8f7209cf6520b83eb73413e9 to your computer and use it in GitHub Desktop.
Mikrotik RouterOS Lease script example

Sources

Script Variables/Parameters

This parameter is available in v6.39rc33+ These are available variables that are accessible for the event script:

bound - 1 - lease is added/changed; 0 - lease is removed

server-address - server address

lease-address - lease address provided by a server

interface - name of the interface on which the client is configured

gateway-address - gateway address provided by a server

vendor-specific - stores value of option 43 received from DHCP server

lease-options - an array of received options

{
:do {
:log info ("dhcp-client for interface: " . $interface . " script Started");
:local routingTable "WAN1";
:local routingTable "WAN2";
:local routingTable "LTE1";
:local routingTable "LTE2";
:set routingTable "WAN1";
:local routingTableCount [/routing/table/print count-only where name="$routingTable"];
:if ($routingTableCount = 0) do={
/routing/table/add name="$routingTable" fib;
}
:local count [/ip route print count-only where comment="$routingTable" and routing-table=$routingTable]
:if ($bound=1) do={
:if ($count = 0) do={
/ip route add gateway=$"gateway-address" comment="$routingTable" routing-table=$routingTable;
} else={
:if ($count = 1) do={
:local test [/ip route find where comment="$routingTable" and routing-table=$routingTable]
:if ([/ip route get $test gateway] != $"gateway-address") do={
/ip route set $test gateway=$"gateway-address";
}
} else={
:log info "Multiple routes found for $routingTable";
}
}
} else={
/ip route remove [find comment="$routingTable"];
}
:log info ("dhcp-client for interface: " . $interface . " script Finished Succesfully");
} on-error={
:log info ("dhcp-client for interface: " . $interface . " script Finished with error");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment