1 | 1 |
new file mode 100755 |
... | ... |
@@ -0,0 +1,87 @@ |
1 |
+#!/bin/sh |
|
2 |
+set -euC |
|
3 |
+ |
|
4 |
+# nfsn-dns-update DOMAIN NAME TYPE FILTER_REGEX [DATA]... |
|
5 |
+ |
|
6 |
+# nfsn-dns-update "example.com" "git" "A" '' \ |
|
7 |
+# "$(curl -s "https://api.ipify.org")" |
|
8 |
+ |
|
9 |
+# Arguments. |
|
10 |
+ |
|
11 |
+domain="$1" ; shift |
|
12 |
+name="$1" ; shift |
|
13 |
+type="$1" ; shift |
|
14 |
+filter_regex="$1" ; shift |
|
15 |
+ |
|
16 |
+host="${name:+"$name."}$domain" |
|
17 |
+ |
|
18 |
+data_new_list="" |
|
19 |
+while [ "$#" -gt "0" ] |
|
20 |
+do |
|
21 |
+ data="$1" ; shift |
|
22 |
+ data_new_list="$( |
|
23 |
+ printf "%s${data_new_list:+"\n"}%s\n" \ |
|
24 |
+ "${data_new_list:-}" \ |
|
25 |
+ "$data" |
|
26 |
+ )" |
|
27 |
+done |
|
28 |
+ |
|
29 |
+# Old data. |
|
30 |
+ |
|
31 |
+data_old_response="$( |
|
32 |
+ nfsn-send "POST" "/dns/$domain/listRRs" \ |
|
33 |
+ "name" "$name" \ |
|
34 |
+ "type" "$type" |
|
35 |
+)" |
|
36 |
+data_old_list="$( |
|
37 |
+ printf "%s\n" "$data_old_response" \ |
|
38 |
+ | jq -r ' |
|
39 |
+ .[] | |
|
40 |
+ if ."aux" |
|
41 |
+ then |
|
42 |
+ "\(."aux") " |
|
43 |
+ else |
|
44 |
+ "" |
|
45 |
+ end |
|
46 |
+ + |
|
47 |
+ "\(."data"?)" |
|
48 |
+ ' \ |
|
49 |
+ | grep "$filter_regex" \ |
|
50 |
+ || true |
|
51 |
+)" |
|
52 |
+ |
|
53 |
+# Update. |
|
54 |
+ |
|
55 |
+if [ -n "$data_old_list" ] |
|
56 |
+then |
|
57 |
+ printf "%s\n" "$data_old_list" \ |
|
58 |
+ | while IFS= read -r data |
|
59 |
+ do |
|
60 |
+ if ! printf "%s\n" "$data_new_list" | grep -qFx "$data" |
|
61 |
+ then |
|
62 |
+ printf "Removing data: %s\n" "$data" |
|
63 |
+ nfsn-send "POST" "/dns/$domain/removeRR" \ |
|
64 |
+ "name" "$name" \ |
|
65 |
+ "type" "$type" \ |
|
66 |
+ "data" "$data" |
|
67 |
+ fi |
|
68 |
+ done |
|
69 |
+fi |
|
70 |
+ |
|
71 |
+if [ -n "$data_new_list" ] |
|
72 |
+then |
|
73 |
+ printf "%s\n" "$data_new_list" \ |
|
74 |
+ | while IFS= read -r data |
|
75 |
+ do |
|
76 |
+ if ! printf "%s\n" "$data_old_list" | grep -qFx "$data" |
|
77 |
+ then |
|
78 |
+ printf "Adding data: %s\n" "$data" |
|
79 |
+ nfsn-send "POST" "/dns/$domain/addRR" \ |
|
80 |
+ "name" "$name" \ |
|
81 |
+ "type" "$type" \ |
|
82 |
+ "data" "$data" |
|
83 |
+ else |
|
84 |
+ printf "Data already present: %s\n" "$data" |
|
85 |
+ fi |
|
86 |
+ done |
|
87 |
+fi |