~krystianch/update_hexonet

f947e5b0ff525e405bb7fb7bf67fb85e7c0cf392 — Krystian Chachuła 10 months ago
Initial commit
1 files changed, 62 insertions(+), 0 deletions(-)

A update_hexonet.sh
A  => update_hexonet.sh +62 -0
@@ 1,62 @@
#.Distributed under the terms of the GNU General Public License (GPL) version 3.0
#
# script for sending updates to hexonet.net
#.2024 Krystian Chachuła <krystian@krystianch.com>

call() {
	command="$1"
	curl -s \
		-X POST \
		-H 'Content-Type: application/x-www-form-urlencoded' \
		--data-urlencode 's_entity=54cd' \
		--data-urlencode "s_command=$command" \
		--data "s_login=$URL_USER&s_pw=$URL_PASS" \
		https://api.ispapi.net/api/call.cgi
}

[ -z "$domain" ]   && write_log 14 "Service section not configured correctly! Missing 'domain'"
[ -z "$username" ] && write_log 14 "Service section not configured correctly! Missing 'username'"
[ -z "$password" ] && write_log 14 "Service section not configured correctly! Missing 'password'"

domain="${domain%.}".
zone="${domain#*.}"
name="${domain%%.*}"
record="$name 3600 IN A $__IP"

update_cmd="COMMAND = UPDATEDNSZONE
ADDRR0 = $name 3600 IN A $__IP
DNSZONE = $zone
EXTENDED = 1
INCSERIAL = 1
RESOLVETTLCONFLICTS = 1"

query_cmd="COMMAND = QueryDNSZoneRRList
DNSZONE = $zone"
existing=$(call "$query_cmd" \
	| awk "/PROPERTY\[RR\]\[[0-9]+\]=${domain}/ {print \$2,\$3,\$4,\$5}")
if [ -n "$existing" ] && [ "$name $existing" != "$record" ]; then
	write_log 7 "Record exists but IP is wrong, will be replaced"
	update_cmd="$update_cmd
DELRR0 = $name $existing"
elif [ -z "$existing" ]; then
	write_log 7 "Record does not exist"
else
	write_log 7 "Record exists"
	return 0
fi

write_log 7 "$update_cmd"

res=$(call "$update_cmd")
code=$(echo "$res" \
	| awk -F= '/CODE=[0-9]+/ { print $2 }')
if [ -z "$code" ]; then
	write_log 7 "Unexpected response: $res"
	return 1
elif [ "$code" != 200 ]; then
	write_log 7 "Error response: $res"
	return 1
fi

write_log 7 "Success"
return 0