Last active
May 29, 2024 20:45
-
-
Save Jeremie-Chauvel/bc52d194de3c3a8722877cbebdbb7164 to your computer and use it in GitHub Desktop.
A basic script to run on your self hosted server to update DNS on porkbun
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| # usage secretapikey=sk1_xxx apikey=pk1_xxx ./autodns.sh | |
| set -eo pipefail | |
| IFS=$'\n\t' | |
| # passed as env var, see usage | |
| readonly secretapikey=$secretapikey | |
| readonly apikey=$apikey | |
| set -u | |
| MY_DOMAIN_NAME_RECORD='domainname.com/ID' | |
| # checks | |
| if [ -z "${secretapikey}" ];then | |
| echo 'missing secretapikey' | |
| exit 1 | |
| fi | |
| if [ -z "${apikey}" ];then | |
| echo 'missing apikey' | |
| exit 1 | |
| fi | |
| SCRIPT_DIRECTORY="$(cd "$(dirname "${BASH_SOURCE[0]}")" >/dev/null 2>&1 && pwd)" | |
| myIp=$(ip addr show scope global | grep inet6 | grep --invert-match "mngtmpaddr" | egrep --only-matching "[[:alnum:]:]{8,39}") | |
| prevIp=$(<"$SCRIPT_DIRECTORY/myip.txt") | |
| if [[ "$prevIp" == "$myIp" ]];then | |
| echo "same IP: $myIp" | |
| exit 0 | |
| fi | |
| echo "new IP: $myIp, old IP: $prevIp" | |
| curl "https://porkbun.com/api/json/v3/dns/edit/$MY_DOMAIN_NAME_RECORD" --data "{\"secretapikey\":\"$secretapikey\",\"apikey\":\"$apikey\", \"type\": \"AAAA\",\"content\": \"$myIp\"}" | |
| echo "$myIp" > "$SCRIPT_DIRECTORY/myip.txt" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment