Updates a DNS A record on your Plesk server over SSH when your WAN IP changes.
Requirements · Install · Configuration · How it works · Tested with · Changelog
If your Plesk server hosts the DNS for your domain anyway, you don't need a dynamic DNS service for the machine at home: a subdomain on your own server will do. I wrote this script to keep such a subdomain pointed at my home connection.
It runs from cron on a machine behind the changing IP, asks OpenDNS for the current WAN IP and, when it changed, writes it into the Plesk database over SSH, tells Plesk to apply it, and sends you a mail.
On the machine that runs the script:
- bash
- the OpenSSH client (
ssh,ssh-agent,ssh-add) dig(indnsutilsorbind-tools, depending on the distribution)wget- a
mailxthat can send through SMTP with-S smtp=..., for example s-nail or heirloom-mailx
On the Plesk server:
- an SSH login with a key, for a user that may run
mysqlon the Plesk database and/opt/psa/admin/bin/dnsmng. The script runs both without a password prompt, so put the database credentials in that user's~/.my.cnf.
-
In Plesk, create a subdomain for the machine at home, for example
home.example.com. It needs no hosting, only its A record. -
Optional: lower the TTL in the subdomain's SOA record, so that a new IP reaches resolvers sooner.
-
Clone this repository on the machine at home:
$ git clone https://github.com/derblub/dyndns ~/dyndns -
Copy the example configuration and fill in your values (see Configuration). The script refuses to run without it.
$ mkdir -p ~/.config/dyndns ~/.local/state $ cp ~/dyndns/config.example ~/.config/dyndns/config $ chmod 600 ~/.config/dyndns/config
-
Run it once by hand and check the output, the log and the record in Plesk:
$ ~/dyndns/dyndns.sh -
Add it to your crontab (
crontab -e), for example every five minutes:*/5 * * * * $HOME/dyndns/dyndns.sh >> $HOME/.local/state/dyndns.cron.log 2>&1Call the script itself or
bash dyndns.sh, notsh dyndns.sh: it needs bash. Every run prints the messages ofssh-agentandssh-add; the redirect keeps cron from mailing them to you every five minutes. Errors still reach you by mail and inLOG_FILE.
The script reads ~/.config/dyndns/config. To use another file, set DYNDNS_CONFIG, for example DYNDNS_CONFIG=/etc/dyndns.conf dyndns.sh. The file is sourced by bash, so it uses shell syntax.
| Setting | What it is |
|---|---|
DNS_RESOLVER |
The dig query that returns your WAN IP. Keep myip.opendns.com @resolver1.opendns.com unless you have another service that answers the same way. |
DNS_TABLE |
The table with the DNS records in the Plesk database: psa.dns_recs. |
REMOTEUSER, REMOTEHOST |
The SSH login on the Plesk server. |
IDENTITY |
The SSH key for that login. It must not have a passphrase when the script runs from cron. |
DOMAIN |
The record to update, without the trailing dot, for example home.example.com. |
MAILTO |
Who gets the notification mails. Several addresses separated by spaces, or a bash array. |
SMTPSERVER, SMTPPORT, SMTPFROM, SMTPUSER, SMTPPASSWORD |
The SMTP account that sends the mails. The script uses STARTTLS. |
LOG_FILE |
Where changes and errors are logged. Leave it empty to only print them. |
LAST_WAN_IP |
The file that keeps the last IP the script saved. |
- The script reads the configuration, starts an
ssh-agentand adds the key fromIDENTITY. - It checks whether the machine is online (a
wget --spiderrequest togoogle.at). If not, it waits 30 seconds and goes on anyway. - It asks
DNS_RESOLVERfor the WAN IP and looks up the current A record ofDOMAIN. If both match the IP inLAST_WAN_IP, it stops without further output. - If it got no WAN IP, it tries once more, then logs the error, sends a mail and stops. It also stops with an error when the answer is anything but a single IPv4 address, because the address goes into an SQL statement and a command line on the server.
- Over SSH, it reads the A record of
DOMAINfromDNS_TABLE. If it gets nothing back (no connection, or no such record), it logs a connection error, sends a mail and stops. - If the record in the database differs from the WAN IP, it updates the record, runs
dnsmng --updateforDOMAINso that Plesk applies it, saves the IP inLAST_WAN_IP, logs it and sends a mail. If the database reports no changed row, it logs and mails that error.
However the run ends, the script stops the ssh-agent it started.
The exit code is 1 when nothing changed, as well as on errors, so don't use it to detect failures; the log and the mails are the reliable signal.
- Plesk 10
- Plesk 11.5
Both in 2015. For any other version, check first that psa.dns_recs has the columns host, type, val and displayVal and that Plesk still uses them, and that /opt/psa/admin/bin/dnsmng exists. Without dnsmng, you have to make your name server reload the zone yourself.
A report from a Plesk version not listed above is the most useful contribution. See CONTRIBUTING.md for how to test changes, and the Code of Conduct. Security issues go through SECURITY.md, which also lists every host the script talks to.
GPL-3.0 © Daniel Kurdoghlian, Pushing Pixels
Built and maintained by Daniel Kurdoghlian at Pushing Pixels in Vienna.
I build infrastructure that deploys the same way every time, and the pipelines around it.