Created
December 27, 2022 10:37
-
-
Save ysc3839/2c629d69f5fc3541a3f1900a23681f0c to your computer and use it in GitHub Desktop.
NATMap SRV record update script for ns1.com and OpenWrt. Derived from https://github.com/openwrt/packages/blob/master/net/ddns-scripts/files/usr/lib/ddns/update_ns1_com.sh
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
#!/bin/sh | |
. /usr/share/libubox/jshn.sh | |
write_log() { | |
logger -st natmap-update -p "$1" "$2" | |
} | |
RRTYPE=A | |
ZONE="example.com" | |
DOMAIN="ip.example.com" | |
IP="$1" | |
TTL=300 | |
API_KEY="example-key" | |
URL="https://api.nsone.net/v1/zones/$ZONE/$DOMAIN/$RRTYPE" | |
DATFILE="$(mktemp -u natmap-update.dat.XXXXXX)" | |
ERRFILE="$(mktemp -u natmap-update.err.XXXXXX)" | |
trap 'rm -f "$DATFILE" "$ERRFILE"' EXIT | |
# Construct JSON payload | |
# {"answers":[{"answer":["1.1.1.1"]}]} | |
json_init | |
json_add_array answers | |
json_add_object | |
json_add_array answer | |
json_add_string "" "$IP" | |
json_close_array | |
json_close_object | |
json_close_array | |
STATUS=$(curl -s -X POST "$URL" \ | |
-H "X-NSONE-Key: $API_KEY" \ | |
-H "Content-Type: application/json" \ | |
-d "$(json_dump)" \ | |
-w "%{http_code}\n" -o $DATFILE 2>$ERRFILE) | |
if [ $? -ne 0 ]; then | |
write_log 14 "Curl failed: $(cat $ERRFILE)" | |
return 1 | |
elif [ -z $STATUS ] || [ $STATUS != 200 ]; then | |
write_log 4 "Request failed: $STATUS, NS1 answered: $(cat $DATFILE)" | |
if [ $STATUS = 404 ]; then | |
write_log 4 "Status is 404, trying to create a DNS record" | |
# {"zone":zone,"domain":domain,"type":type,"answers":[{"answer":["1.1.1.1"]}]} | |
json_init | |
json_add_string "zone" "$ZONE" | |
json_add_string "domain" "$DOMAIN" | |
json_add_string "type" "$RRTYPE" | |
json_add_int "ttl" "$TTL" | |
json_add_array answers | |
json_add_object | |
json_add_array answer | |
json_add_string "" "$IP" | |
json_close_array | |
json_close_object | |
json_close_array | |
STATUS=$(curl -s -X PUT "$URL" \ | |
-H "X-NSONE-Key: $API_KEY" \ | |
-H "Content-Type: application/json" \ | |
-d "$(json_dump)" \ | |
-w "%{http_code}\n" -o $DATFILE 2>$ERRFILE) | |
if [ $? -ne 0 ]; then | |
write_log 14 "Curl failed: $(cat $ERRFILE)" | |
return 1 | |
elif [ -z $STATUS ] || [ $STATUS != 200 ]; then | |
write_log 14 "Request failed: $STATUS, NS1 answered: $(cat $DATFILE)" | |
return 1 | |
fi | |
else | |
return 1 | |
fi | |
fi | |
write_log 7 "NS1 answered: $(cat $DATFILE)" | |
return 0 |
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
#!/bin/sh | |
. /usr/share/libubox/jshn.sh | |
write_log() { | |
logger -st natmap-update -p "$1" "$2" | |
} | |
./update_natmap_ip_ns1.sh "$1" & | |
RRTYPE=SRV | |
ZONE="example.com" | |
DOMAIN="_service._proto.example.com" | |
PRIORITY=0 | |
WEIGHT=0 | |
PORT="$2" | |
TARGET="ip.example.com" | |
TTL=300 | |
API_KEY="example-key" | |
URL="https://api.nsone.net/v1/zones/$ZONE/$DOMAIN/$RRTYPE" | |
DATFILE="$(mktemp -u natmap-update.dat.XXXXXX)" | |
ERRFILE="$(mktemp -u natmap-update.err.XXXXXX)" | |
trap 'rm -f "$DATFILE" "$ERRFILE"' EXIT | |
# Construct JSON payload | |
# {"answers":[{"answer":[priority,weight,port,target]}]} | |
json_init | |
json_add_array answers | |
json_add_object | |
json_add_array answer | |
json_add_int "" "$PRIORITY" | |
json_add_int "" "$WEIGHT" | |
json_add_int "" "$PORT" | |
json_add_string "" "$TARGET" | |
json_close_array | |
json_close_object | |
json_close_array | |
STATUS=$(curl -s -X POST "$URL" \ | |
-H "X-NSONE-Key: $API_KEY" \ | |
-H "Content-Type: application/json" \ | |
-d "$(json_dump)" \ | |
-w "%{http_code}\n" -o $DATFILE 2>$ERRFILE) | |
if [ $? -ne 0 ]; then | |
write_log 14 "Curl failed: $(cat $ERRFILE)" | |
return 1 | |
elif [ -z $STATUS ] || [ $STATUS != 200 ]; then | |
write_log 4 "Request failed: $STATUS, NS1 answered: $(cat $DATFILE)" | |
if [ $STATUS = 404 ]; then | |
write_log 4 "Status is 404, trying to create a DNS record" | |
# {"zone":zone,"domain":domain,"type":type,"answers":[{"answer":[priority,weight,port,target]}]} | |
json_init | |
json_add_string "zone" "$ZONE" | |
json_add_string "domain" "$DOMAIN" | |
json_add_string "type" "$RRTYPE" | |
json_add_int "ttl" "$TTL" | |
json_add_array answers | |
json_add_object | |
json_add_array answer | |
json_add_int "" "$PRIORITY" | |
json_add_int "" "$WEIGHT" | |
json_add_int "" "$PORT" | |
json_add_string "" "$TARGET" | |
json_close_array | |
json_close_object | |
json_close_array | |
STATUS=$(curl -s -X PUT "$URL" \ | |
-H "X-NSONE-Key: $API_KEY" \ | |
-H "Content-Type: application/json" \ | |
-d "$(json_dump)" \ | |
-w "%{http_code}\n" -o $DATFILE 2>$ERRFILE) | |
if [ $? -ne 0 ]; then | |
write_log 14 "Curl failed: $(cat $ERRFILE)" | |
return 1 | |
elif [ -z $STATUS ] || [ $STATUS != 200 ]; then | |
write_log 14 "Request failed: $STATUS, NS1 answered: $(cat $DATFILE)" | |
return 1 | |
fi | |
else | |
return 1 | |
fi | |
fi | |
write_log 7 "NS1 answered: $(cat $DATFILE)" | |
return 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment