Skip to content

Instantly share code, notes, and snippets.

@nansenat16
Last active November 7, 2023 13:08
Show Gist options
  • Save nansenat16/bbedb728a486b4a7fb76ae031995a317 to your computer and use it in GitHub Desktop.
Save nansenat16/bbedb728a486b4a7fb76ae031995a317 to your computer and use it in GitHub Desktop.
RouterOS godaddy-ddns-update-script
#GoDaddy DDNS Update Script
#Test with RouterOS 6.45.2 (Minimum version RouterOS 6.44)
#Date:2019/08/02
#The read,write,policy,test that all of these policies need to be set in script for the global variable
#ref1 https://www.instructables.com/id/Quick-and-Dirty-Dynamic-DNS-Using-GoDaddy/
#ref2 http://www.minitw.com/post/2018/05/09/routeros-ddns-namecheap
#Easy Test
# /tool fetch url="https://api.godaddy.com/v1/domains/[domain]/records/A/[hostname]" http-method=put http-data="[{\"data\": \"10.10.10.10\"}]" http-header-field="content-type: application/json,Authorization: sso-key [API_KEY]:[API_SECRET]"
#Create Schedule running
# /system scheduler add name=update-ddns interval=1m on-event=godaddy-ddns
#kye token format is [API_KEY]:[API_SECRET]
:local ddnkey "Authorization: sso-key [API_KEY]:[API_SECRET]"
:local ddnsdomain "[domain]"
:local ddnshostname "[hostname]"
:local ddnsinterface "pppoe-adsl"
#Use global variable to check if need update
:global ddnslastipADSL1
:global ddnsipADSL1 [/ip address get [/ip address find actual-interface=$ddnsinterface] address ]
:if ([:typeof [:global ddnslastipADSL1]] = nil ) do={ :global ddnslastipADSL1 0.0.0.0/0 } else={ :set ddnslastipADSL1 $ddnslastipADSL1 }
:if ([:typeof [:global ddnsipADSL1]] = nil ) do={
:log info ("DDNS: No ip address present on " . $ddnsinterface . ", please check.")
} else={
:if ($ddnsipADSL1 != $ddnslastipADSL1) do={
:local ipFormat [:pick $ddnsipADSL1 0 [:find $ddnsipADSL1 "/"]];
:log info "DDNS-Update use $ddnsinterface IP : $ipFormat"
:local ddnsurl "https://api.godaddy.com/v1/domains/$ddnsdomain/records/A/$ddnshostname"
:local ddnsipjson "[{\"data\": \"$ipFormat\"}]"
:local result [/tool fetch url=$ddnsurl http-method=put http-data=$ddnsipjson http-header-field="content-type: application/json,$ddnkey" as-value output=user]
:if ($result->"status" = "finished") do={
:log info ($ddnshostname . "." . $ddnsdomain " Update IP " . $ipFormat . " Success")
:global ddnslastipADSL1 $ddnsipADSL1
} else={
:log info ("DDNS Update Error".$result->status)
}
} else={
:log info "DDNS: IP No Change"
}
}
@BratSinot
Copy link

For IPv6 we can use something like that:

#kye token format is [API_KEY]:[API_SECRET]
:local ddnkey "Authorization: sso-key [API_KEY]:[API_SECRET]"
:local ddnsdomain "[domain]"
:local ddnshostname "@"
:local ddnsinterfacev4 "pppoe-adsl"
:local ddnsinterfacev6 "bridge"

#Use global variable to check if need update
:global ddnsLastIPv4
:global ddnsIPv4 [/ip address get [/ip address find actual-interface=$ddnsinterfacev4] address ]
:if ([:typeof [:global ddnsLastIPv4]] = nil ) do={ :global ddnsLastIPv4 0.0.0.0/0 } else={ :set ddnsLastIPv4 $ddnsLastIPv4 }

:global ddnsLastIPv6
:global ddnsIPv6 [/ipv6 address get [/ipv6 address find actual-interface=$ddnsinterfacev6 !link-local] address ]
:if ([:typeof [:global ddnsLastIPv6]] = nil ) do={ :global ddnsLastIPv6 ::0/0 } else={ :set ddnsLastIPv6 $ddnsLastIPv6 }

:if ([:typeof [:global ddnsIPv4]] = nil ) do={
  :log info ("DDNS: No IPv4 address present on " . $ddnsinterfacev4 . ", please check.")
} else={
  :if ($ddnsIPv4 != $ddnsLastIPv4) do={
    :local ipFormat [:pick $ddnsIPv4 0 [:find $ddnsIPv4 "/"]];
	:log info "DDNS-Update use $ddnsinterfacev4 IPv4 : $ipFormat"
    :local ddnsurl "https://api.godaddy.com/v1/domains/$ddnsdomain/records/A/$ddnshostname" 
    :local ddnsipjson "[{\"data\": \"$ipFormat\"}]"
    :local result [/tool fetch url=$ddnsurl http-method=put http-data=$ddnsipjson http-header-field="content-type: application/json,$ddnkey" as-value output=user]
    :if ($result->"status" = "finished") do={
        :log info ($ddnshostname . "." . $ddnsdomain "  Update IPv4 " . $ipFormat . " Success")
        :global ddnsLastIPv4 $ddnsIPv4
    } else={
        :log info ("DDNS IPv4 Update Error".$result->status)
    }
  } else={
    :log info "DDNS: IPv4 No Change"
  }
}

:if ([:typeof [:global ddnsIPv6]] = nil ) do={
  :log info ("DDNS: No IPv6 address present on " . $ddnsinterfacev6 . ", please check.")
} else={
  :if ($ddnsIPv6 != $ddnsLastIPv6) do={
    :local ipFormat [:pick $ddnsIPv6 0 [:find $ddnsIPv6 "/"]];
	:log info "DDNS-Update use $ddnsinterfacev6 IPv6 : $ipFormat"
    :local ddnsurl "https://api.godaddy.com/v1/domains/$ddnsdomain/records/AAAA/$ddnshostname" 
    :local ddnsipjson "[{\"data\": \"$ipFormat\"}]"
    :local result [/tool fetch url=$ddnsurl http-method=put http-data=$ddnsipjson http-header-field="content-type: application/json,$ddnkey" as-value output=user]
    :if ($result->"status" = "finished") do={
        :log info ($ddnshostname . "." . $ddnsdomain "  Update IPv6 " . $ipFormat . " Success")
        :global ddnsLastIPv6 $ddnsIPv6
    } else={
        :log info ("DDNS IPv6 Update Error".$result->status)
    }
  } else={
    :log info "DDNS: IPv6 No Change"
  }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment