| check-dns-resolve.sh | ||
| domainssaubillig_ip_update.py | ||
| hosting-ip-sync.sh | ||
| namecheap-ip-sync.sh | ||
| README.md | ||
IP Update Scripts
Set of scripts I use to update IP addresses for various domains. The supported registrars are Namecheap, domainssaubillig, and hosting.de. More domains from other registrars can easily be added given they provide a useful API.
Setup
First of all the Python script for domainssaubillig, if used, requires selenium and a browser as a webdriver, both Firefox and Chrome are supported. That's needed since the website uses Javascript for various actions, unfortunately, which makes it necessary to emulate what one would do within a browser.
For Arch Linux using Firefox run:
sudo pacman -S python-selenium geckodriver firefox
If you want to use Chrome instead:
sudo pacman -S python-selenium chromium
Place the scripts on your server, ideally owned by root and mark them as executable (chmod +x <script_name>).
In namecheap-ip-sync.sh replace the url of the curl requests with the ones for your domains/subdomains, and/or add more entries for other registrars with a convenient API.
Replace the path to where you placed the domainssaubillig_ip_update.py and/or the hosting-ip-sync.sh script, or remove the corresponding block completely if not needed.
Replace the DNS record IDs and corresponding domains in hosting-ip-sync.sh to match your information. Details on queries using curl can be found within the script.
Open domainssaubillig_ip_update.py and replace the username and domains you want to handle with it.
Regular Sync via cron
In order to check regularly for an IP change and trigger an update, a cron job is ideal. In this example it will run every other minute and call the namecheap-ip-sync.sh script to check if the IP has changed. The password for domainssaubillig or the API key for hosting.de should be stored here as an environment variable which is retrieved by the scripts.
To create a cron job run sudo crontab -e and add the following line:
*/2 * * * * SECRET='YOURPASSWORD' /usr/bin/namecheap-ip-sync.sh
assuming you placed the scripts in /usr/bin.
In case only Namecheap is used and the other scripts are not called from within the shell script (e.g. removed like mentioned above), the SECRET='XXX' part is not needed and should be removed.
If both, domainssaubillig and hosting.de should be used, you need to add a second secret variable for the API Key and adapt the script accordingly.
IMPORTANT: Even if placed in quotation marks, cron interprets some characters like % or # as, i.e., newline or comment. While a hash can be escaped via \#, the percentage causes more serious issues. Try and prevent using them to avoid some problems. A hint on problem can be found via journalctl -xe where CROND complains about an unexpected end of file and especially if the SECRET variable set is shown truncated.
That's it! Now every two minutes the cron job calls the shell script which checks your IP address and updates it if it has changed.
A log file is written to /var/log/ip-sync.log if you didn't change it.
Check if Hostname Resolution is Working
It might happen under cerain circumstances that the resolution of hostnames fails. If this happens, the address used in the scripts to obtain the server's IP address can't be resolved. In most cases a simple reboot solves this issue.
The script check-dns-resolve.sh is intended to do this. Setting this up as a cron jobs, it check periodically if hostnames can still be resolved and issues a reboot if this is not the case.
Similar to the one above, a cron job can be created with sudo crontab -e and appending the following line:
*/20 * * * * /usr/bin/check-dns-resolve.sh
This cron job check every 20 minutes if the resolution still works. 0 * * * * will run this only once an hour, or increase the frequency if needed.