~krystianch/update_hexonet

82191c2fbdeb0c69ae2a8179441056d680cfaaaf — Krystian Chachuła 4 months ago f947e5b master
Use some of the passed variables

CURL, CURL_SSL, DATFILE, ERRFILE, USE_CURL, use_https
1 files changed, 35 insertions(+), 15 deletions(-)

M update_hexonet.sh
M update_hexonet.sh => update_hexonet.sh +35 -15
@@ 2,29 2,50 @@
#
# script for sending updates to hexonet.net
#.2024 Krystian Chachuła <krystian@krystianch.com>
#
# TODO: make use of vars:
# CURL_PROXY, LOGFILE, PID_SLEEP, RETRY_COUNT, RETRY_SECONDS, VERBOSE,
# force_ipversion, use_ipv6, URL_PENC


call() {
	command="$1"
	curl -s \

	write_log 7 "Calling HEXONET with command:" $command
	
	$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" \
		--output $DATFILE --stderr $ERRFILE \
		https://api.ispapi.net/api/call.cgi

	if [ "$?" -ne 0 ]; then
		write_log 14 "cURL error (exit code $?): " $(cat "$ERRFILE")
	fi

	write_log 7 "HEXONET responded:" $(cat "$DATFILE")
}

[ -z "$CURL" ] && [ -z "$CURL_SSL" ] && write_log 14 "HEXONET communication require cURL with SSL support. Please install"
[ -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'"
[ -z "$URL_USER" ] && write_log 14 "Service section not configured correctly! Missing 'username'"
[ -z "$URL_PASS" ] && write_log 14 "Service section not configured correctly! Missing 'password'"
[ "$USE_CURL" -ne 1 ] && write_log 5 "The 'use_curl' setting is being forced to 1 as HEXONET communication require it."
[ "$use_https" -ne 1 ] && write_log 5 "The 'use_https' setting is being forced to 1 as HEXONET communication require it."

domain="${domain%.}".
zone="${domain#*.}"
name="${domain%%.*}"
write_log 7 "Parsed domain='$domain' zone='$zone' name='$name'"

record="$name 3600 IN A $__IP"
write_log 6 "Desired record is '$record'"

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


@@ 32,31 53,30 @@ RESOLVETTLCONFLICTS = 1"

query_cmd="COMMAND = QueryDNSZoneRRList
DNSZONE = $zone"
existing=$(call "$query_cmd" \
call "$query_cmd"
existing=$(cat "$DATFILE" \
	| 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"
	write_log 6 "Found existing record '$name $existing'. It will be removed"
	update_cmd="$update_cmd
DELRR0 = $name $existing"
elif [ -z "$existing" ]; then
	write_log 7 "Record does not exist"
	write_log 6 "Desired record does not exist. It will be added"
else
	write_log 7 "Record exists"
	write_log 6 "Desired record exists. Nothing to do"
	return 0
fi

write_log 7 "$update_cmd"

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

write_log 7 "Success"
write_log 6 "Update successful"
return 0