Skip to content

Instantly share code, notes, and snippets.

@iDanielLaw
Forked from adrianmo/ddns_update
Created May 26, 2020 04:31
Show Gist options
  • Save iDanielLaw/59021e6dd8e19582b738791998da2f79 to your computer and use it in GitHub Desktop.
Save iDanielLaw/59021e6dd8e19582b738791998da2f79 to your computer and use it in GitHub Desktop.
Mikrotik script to update a dynamic DNS host and IPsec policy
##############Script Settings##################
:local DDNSUser "username"
:local DDNSPass "password"
:local DDNSDomain "yourhost.ddns.net"
:local DDNSServer "https://members.dyndns.org/v3/update"
:local WANInter "pppoe-out1"
###############################################
:local IpCurrent [/ip address get [find interface=$WANInter] address];
:for i from=( [:len $IpCurrent] - 1) to=0 do={
:if ( [:pick $IpCurrent $i] = "/") do={
:local NewIP [:pick $IpCurrent 0 $i];
:if ([:resolve $DDNSDomain] != $NewIP) do={
/tool fetch mode=https user=$DDNSUser password=$DDNSPass url="$DDNSServer\3Fhostname=$DDNSDomain&myip=$NewIP" keep-result=no
:log info "DDNS Update: $DDNSDomain - $NewIP"
:log info "IPsec: Updating IPsec Policy."
/ip ipsec policy set [find comment="myIPsec"] sa-src-address=$NewIP
:log info "IPsec: IPsec Policy updated."
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment