This project generates a script for Mikrotik routers
that updates a pair of Cloudflare DNS records
when the router's public IP address has changed.
The two records are for a domain and all it's subdomains:
for example sub.domain.com
and *.sub.domain.com
.
I use this to direct traffic to a Traefik instance,
with subdomains routed to different services behind the Traefik proxy.
Mikrotik routers include a
Dynamic DNS feature
that works for many purposes,
and it's possible to use CNAME
DNS records that point to the Mikrotik
dynamic DNS domain for your router.
But you might prefer a script like this that allows your
router to update the records at Cloudflare directly.
Both gen-script
and gen-script-ipinfo
do samething but with different approaches:
gen-script
will use local interfaces addressgen-script-ipinfo
will use return of [ipinfo.io][https://ipinfo.io]
gen-script
will query the Cloudflare API and generate a
script that you can add to your Mikrotik router.
To run gen-script
successfully you will need to set the following environment
variables:
DOMAIN
- the domain records at Cloudflare that you want to update. For example,sub.domain.com
.TOKEN
- an API token that you've created in the Cloudflare dashboard. See below.ZONE_ID
- the Zone ID, found on the Cloudflare Overview page for the top-level domain containing the domain records.$INTERFACE_V4
- interface from Mikrotik to get Public IPv4$INTERFACE_V6
- interface from Mirkotik to get Public IPv6 - This one is optional.
I recommend using direnv to set these variables when you change to this directory.
The TOKEN
value is created in the Cloudflare dashboard.
Follow these steps:
- Click the profile icon in the top right of the dashboard, and choose 'My Profile'.
- Click on 'API Tokens', then 'Create Token'.
- Click 'Start with a template', then choose the 'Edit zone DNS' template.
- Under 'Zone Resources', choose your top level domain name from the pull-down list on the right.
- Click 'Continue to summary'.
- Click 'Create Token'.
- Copy the token shown on the following screen
and set the
TOKEN
environment variable to its value.
Once you have set the environment variables listed above
you can run gen-script
.
It will use the Cloudflare API to find the DNS record ids for the
domain and wildcard subdomain of DOMAIN
within the zone
identified by ZONE_ID
.
The value of TOKEN
will be used to authenticate the API requests.
The values from INTERFACE_V4
and INTERFACE_V6
will be used to get
interface address.
gen-script
will output the script to be loaded on to your
router by following these steps:
- Log in to the Mikrotik web user interface on your router, or use WinBox if you are familiar with it.
- Choose 'System' > 'Scripts'.
- Click 'Add New'.
- Set an appropriate name (e.g.
mcd
). - Under 'Policy', select only 'read', 'write', 'policy', and 'test'.
- Paste the output from
gen-script
in to the 'Source' field. - Click 'Apply'.
You will probably want to configure the router to run the script every few minutes:
- Choose 'System' > 'Scheduler'.
- Click 'Add New'.
- Set an appropriate name (e.g.
mcd
). - Set an interval, such as
00:15:00
for every 15 minutes. - Select only the 'read', 'write', 'policy', and 'test' policies, as above.
- Under 'On Event' enter the name you gave to the script when you created it (step 4 above).
- Click 'Apply'.
The script should now run every 15 minutes, updating the Cloudflare DNS records when the router's public IP address has changed.