-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from FarrowStrange/hetzner-api-dyndns-2007250936
[hetzner-api-dyndns-2007250936] complete rework
- Loading branch information
Showing
2 changed files
with
123 additions
and
33 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
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 |