Skip to content

Instantly share code, notes, and snippets.

@vena
Created July 8, 2019 21:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vena/62a52ad38274c84107566dc845a6e396 to your computer and use it in GitHub Desktop.
Save vena/62a52ad38274c84107566dc845a6e396 to your computer and use it in GitHub Desktop.
Mikrotik scripts
{
# dhcp-client lease script
# Updates Namecheap DDNS
do {
:if ($bound=1) do={
:local ddnsURI "https://dynamicdns.park-your-domain.com/update"
:local ddnsHostname "myhostname"
:local ddnsDomain "mydomain.com"
:local ddnsPassword "REPLACE WITH NAMECHEAP DDNS PASSWORD"
# Delay to allow for new route to be established
:delay 3s
:log info ("DDNS: Updating with new IP " . $"lease-address")
:local ddnsCommand ($ddnsURI . "\3F" . "host=" . $ddnsHostname . "&domain=" . $ddnsDomain . "&password=" . $ddnsPassword)
/tool fetch url=$ddnsCommand mode=https keep-result=no
}
} on-error={
:log error "DHCP Client script failed."
}
}
{
# dhcp-server lease script
# Add local devices to static DNS as hostname.domain
# REQUIRES search domain to be set in ip / dhcp-server / network
:local pref "Local DNS:"
:do {
:if ($leaseBound=1) do={
:local clientHost $"lease-hostname"
:if ([:len $clientHost]=0) do={
:log info "$pref No hostname in DHCP request (MAC: $leaseActMAC / IP: $leaseActIP)";
:error
}
:local dnsDomain [/ip dhcp-server network get [find where $leaseActIP in address] domain]
:if ([:len $dnsDomain]=0) do={
:log error "$pref No local domain for network assigned to DHCP server $leaseServerName";
:error
}
:local FQDN "$clientHost.$dnsDomain"
:log info "$pref Adding static lease for $FQDN @ $leaseActIP"
/ip dns static
remove [find where name="$FQDN"]
add name="$FQDN" address=$leaseActIP ttl=600s;
}
:if ($leaseBound=0) do={
:do {
/ip dns static remove [find where address=$leaseActIP]
} on-error={
:log error "$pref Failed to remove FQDN"
}
}
} on-error={
:log error "$pref Lease script failed..."
}
}
{
# Run periodically to resolve pool.ntp.org to IPs
:local ntpServer "us.pool.ntp.org"
:local primary [resolve $ntpServer]
:local secondary [resolve $ntpServer]
:log info "NTP Pool update: $primary / $secondary"
/system ntp client set primary-ntp="$primary" secondary-ntp="$secondary";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment