Skip to content

Commit

Permalink
Merge pull request #2 from FarrowStrange/hetzner-api-dyndns-2007250936
Browse files Browse the repository at this point in the history
[hetzner-api-dyndns-2007250936] complete rework
  • Loading branch information
FarrowStrange authored Jul 26, 2020
2 parents 403b087 + a90e67a commit 6b8285a
Show file tree
Hide file tree
Showing 2 changed files with 123 additions and 33 deletions.
55 changes: 44 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

A small script to dynamically update DNS records using the Hetzner DNS-API. Feel free to propose changes.

**Hetzner DNS API Doc**
**Hetzner DNS API Doc:**

https://dns.hetzner.com/api-docs/

Expand All @@ -11,15 +11,15 @@ https://dns.hetzner.com/api-docs/
First, a new access token must be created in the DNS Console. This should be copied immediately, because for security reasons it will not be possible to display the token later.

## Get all Zones
Get all zones and copy zone ID
Get all zones and copy the desired zone ID.
```
curl "https://dns.hetzner.com/api/v1/zones" -H \
'Auth-API-Token: ${apitoken}'
```

## Add Record
Use the previously obtained zone id to create a dns record.
In the output you get the record ID. This is needed in the script and should therefore be noted.
Use the previously obtained zone ID to create a dns record.
In the output you get the record ID. This is needed for the script and should therefore be noted.
```
curl -X "POST" "https://dns.hetzner.com/api/v1/records" \
-H 'Content-Type: application/json' \
Expand All @@ -34,12 +34,45 @@ curl -X "POST" "https://dns.hetzner.com/api/v1/records" \
```

# Usage
Insert the Access token and the Zone and Record ID in the script.
For security reasons, the access token is stored directly in the script. Enter your previously created token here.
```
...
auth_api_token=''
...
```

As soon as the token is deposited, the script can be called with the appropriate parameters. This allows several DynDNS records to be created in different zones. Optionally, the TTL and the record type can be specified. It is advisable to keep the TTL as low as possible, so that changed records are used as soon as possible.
```
./dyndns.sh -z <Zone ID> -r <Record ID> -n <Record Name> [-t <TTL>] [-T <Record Type>]
```

To keep your DynDNS Records up to date, you have to create a cronjob that calls the script periodically.

To keep your DynDNS Records up to date, you create a cronjob that calls the script periodically.
It is advisable to keep the TTL as low as possible, so that changed records are used as soon as possible.
**Example:** Check DynDNS every 5 Minutes
```
api_auth_token=${apitoken}
api_zone=${zoneID}
api_record=${recordID}
```
*/5 0 * * * /usb/bin/dyndns.sh -z 98jFjsd8dh1GHasdf7a8hJG7 -r AHD82h347fGAF1 -n dyn
```

# Help
Type `-h` to display help page.
```
./dyndns -h
```
```
exec: ./dyndns.sh -z <Zone ID> -r <Record ID> -n <Record Name>
parameters:
-z - Zone ID
-r - Record ID
-n - Record name
optional parameters:
-t - TTL (Default: 60)
-T - Record type (Default: A)
help:
-h - Show Help
example:
.exec: ./dyndns.sh -z 98jFjsd8dh1GHasdf7a8hJG7 -r AHD82h347fGAF1 -n dyn
```
101 changes: 79 additions & 22 deletions dyndns.sh
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,35 +1,92 @@
#!/bin/bash
# DynDNS Script for Hetzner API by FarrowStrange
# V0.1
# DynDNS Script for Hetzner DNS API by FarrowStrange
# v1.0

api_auth_token=
api_zone=
api_record=
auth_api_token=''
record_ttl='60'
record_type='A'

pub_addr=`curl -s ifconfig.me`
api_dyn_addr=`curl -s "https://dns.hetzner.com/api/v1/records/${api_record}" -H 'Auth-API-Token: '${api_auth_token} | cut -d ',' -f 4 | cut -d '"' -f 4`
display_help() {
cat <<EOF
if [[ $pub_addr == $api_dyn_addr ]]; then
echo "DNS record is up to date - nothing to to."
exec: ./dyndns.sh -z <Zone ID> -r <Record ID> -n <Record Name>
else
echo "DNS record is no longer valid - updating record"
parameters:
-z - Zone ID
-r - Record ID
-n - Record name
optional parameters:
-t - TTL (Default: 60)
-T - Record type (Default: A)
help:
-h - Show Help
example:
.exec: ./dyndns.sh -z 98jFjsd8dh1GHasdf7a8hJG7 -r AHD82h347fGAF1 -n dyn
EOF
exit 1
}

while getopts ":z:r:n:t:T:h:" opt; do
case "$opt" in
z ) zone_id="${OPTARG}";;
r ) record_id="${OPTARG}";;
n ) record_name="${OPTARG}";;
t ) record_ttl="${OPTARG}";;
T ) record_type="${OPTARG}";;
h ) display_help;;
\? ) echo "Invalid option: -$OPTARG" >&2; exit 1;;
: ) echo "Missing option argument for -$OPTARG" >&2; exit 1;;
* ) echo "Unimplemented option: -$OPTARG" >&2; exit 1;;
esac
done

if [[ "${zone_id}" = "" ]]; then
echo "Missing option for zone ID: -z <Zone ID>"
echo "Use -h to display help."
exit 1
elif [[ "${record_id}" = "" ]]; then
echo "Mission option for record ID: -r <Record ID>"
echo "Use -h to display help."
exit 1
elif [[ "${record_name}" = "" ]]; then
echo "Mission option for record name: -n <Record Name>"
echo "Use -h to display help."
exit 1
fi

if [[ "${auth_api_token}" = "" ]]; then
echo "No Auth API Token specified. Please reference at the top of the Script."
exit 1
fi


cur_pub_addr=`curl -s https://ifconfig.me`
cur_dyn_addr=`curl -s "https://dns.hetzner.com/api/v1/records/${record_id}" -H 'Auth-API-Token: '${auth_api_token} | cut -d ',' -f 4 | cut -d '"' -f 4`

if [[ $cur_pub_addr == $cur_dyn_addr ]]; then
echo "DNS record \"${record_name}\" is up to date - nothing to to."
exit 0
else
echo "DNS record \"${record_name}\" is no longer valid - updating record"

curl -s -X "PUT" "https://dns.hetzner.com/api/v1/records/${api_record}" \
curl -s -X "PUT" "https://dns.hetzner.com/api/v1/records/${record_id}" \
-H 'Content-Type: application/json' \
-H 'Auth-API-Token: '${api_auth_token} \
-H 'Auth-API-Token: '${auth_api_token} \
-d $'{
"value": "'${pub_addr}'",
"ttl": 60,
"type": "A",
"name": "gw",
"zone_id": "'${api_zone}'"
"value": "'${cur_pub_addr}'",
"ttl": '${record_ttl}',
"type": "'${record_type}'",
"name": "'${record_name}'",
"zone_id": "'${zone_id}'"
}'

if [[ $? != 0 ]]; then
echo "Unable to update record"
else
echo "DNS record updated successfully"
echo "Unable to update record: \"${record_name}\""
else
echo "DNS record \"${record_name}\" updated successfully"
fi
fi
fi

0 comments on commit 6b8285a

Please sign in to comment.