e10cdb7a4749f295ad1806c3a62b59ed9faf74de
[distro-setup] / dynamic-ip-update
1 #!/bin/bash
2 source ~/.bashrc
3
4 main() {
5
6 fqdn=$(hostname -f)
7 domaintmp=${fqdn#*.}
8 hostnametmp=${fqdn%%.*}
9 # i for internet
10 fqdn=${hostnametmp}i.${domaintmp}
11
12 up4=false
13
14 if ! read -r _ _ gateway _ < <(ip -4 route get 85.119.83.50 2>/dev/null); then
15 # if our internet is down, just give up, no need to have an error
16 if [[ ! $INVOCATION_ID ]]; then
17 echo $0: failed to get route, giving up
18 fi
19 exit 0
20 fi
21
22 case $gateway in
23 10.2.0.1)
24 dynhost=i.b8.nz
25 ;;
26 *)
27 return 0
28 ;;
29 esac
30
31 if timeout -s 9 5 ssh-keyscan -p 2220 -t rsa $gateway 2>/dev/null | grep -qFx "[$gateway]:2220 ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCH+/h1dGEfKEusBblndU2e6QT4wLCm5+yqr/sqh/0X9YfjR7BfWWm8nNmuP55cYc+Wuf5ljB1H1acXEcsl1y8e0j3agHfF0V74FE1N1zz5nn2Ep8NHnmqgEhza38ZxMPh+4p3X7zklEKU7+3SzybKBi8sg0wLzlS2LM0JaUN80zR2sK11Kye3dURUXPk78u5wodOkgcEYRwSYaDMJlUzWP+poRXIDJwFaMQnwmxbl/c84yOyaU0x/d6hFwoRscWecihX+vvBNeSyxR4xr2HDOyUWwJkctyAgt2p7w3tfkXOKcCRzTAjGVIMQLTvo0sG/yJbcyHoEFdFybCsgDvfyYn"; then
32 # we are on a home network
33 cur4="$(dig +short $dynhost @iankelling.org | tail -1)"
34 if ip4=$(curl -s4 https://iankelling.org/cgi/pubip); then
35 if [[ $cur4 && $ip4 && $cur4 != $ip4 ]]; then
36 up4=true # update ipv4
37 fi
38 fi
39 fi
40
41 # may not be set yet so allow fail
42 cur6="$(host -4 -t aaaa $fqdn iankelling.org | sed -rn 's/.*has IPv6 address (.*)/\1/p;T;q')" ||:
43
44 up6=false
45
46 out6=$(curl -s6 https://iankelling.org/cgi/pubip) ||: # failure allowed if we have no ipv6
47
48 if [[ $out6 ]]; then
49 dev=$(ip -o a show to $out6 | awk '{print $2}')
50 # we use slaac with privacy extension, so get our less private more permanent address
51 mac=$(cat /sys/class/net/$dev/address)
52
53 IFS=: read -a f <<<$mac; set -- ${f[@]}
54 ip6=${out6%:*:*:*:*}:$(printf %x $((0x$1 + 2)))$2:$3'ff:fe'$4:$5$6
55 # in case we aren't using slaac
56 if ! ip a | grep "^ *inet6 $ip6/" &>/dev/null; then
57 ip6=$out6
58 fi
59 fi
60
61 if [[ $cur6 != $ip6 ]]; then
62 up6=true
63 fi
64
65 if ! $up4 && ! $up6; then
66 return 0
67 fi
68
69 # note, a simpler way to do this would be to ssh and use
70 # "${SSH_CLIENT%% *}
71 # to update bind if needed.
72
73 f=$(mktemp)
74 cat >>$f <<EOF
75 server iankelling.org
76 zone b8.nz
77 EOF
78
79 if $up4; then
80 cat >>$f <<EOF
81 update delete $dynhost. A
82 update add $dynhost. 300 A $ip4
83 EOF
84 fi
85
86 if $up6; then
87 if [[ $ip6 ]]; then
88 cat >>$f <<EOF
89 update delete $fqdn. AAAA
90 update add $fqdn. 60 AAAA $ip6
91 EOF
92 else
93 cat >>$f <<EOF
94 update delete $fqdn. AAAA
95 EOF
96 fi
97 fi
98
99 cat >>$f <<EOF
100 show
101 send
102 answer
103 quit
104 EOF
105
106 nsupdate -k /p/c/machine_specific/vps/filesystem/etc/bind/Kb8.nz.*.private <$f
107 sed -i 's/^server .*/server bk.b8.nz/' $f
108 nsupdate -k /p/c/machine_specific/vps/filesystem/etc/bind/Kb8.nz.*.private <$f
109
110
111 }
112
113 if [[ $INVOCATION_ID ]]; then
114 # this is to prevent systemd from filling up the journal
115 for (( runcount=0; runcount < 100; runcount++ )); do
116 main
117 sleep 30
118 done
119 else
120 main
121 fi
122
123
124
125
126
127 exit 0
128
129
130 # # # persistent initial setup for this:
131 # # # create files in /a/c/machine_specific/vps/filesystem/etc/bind
132 # # # note, conflink also does some group ownership stuff.
133 # mkc /p/c/machine_specific/vps/filesystem/etc/bind
134 # sudo dnssec-keygen -a HMAC-SHA512 -b 512 -n HOST b8.nz
135 # user=$(id -un)
136 # sudo chown $user:$user *
137
138
139 # f=key.b8.nz
140 # cat >$f <<EOF
141 # key b8.nz. {
142 # algorithm HMAC-SHA512;
143 # secret "$(awk '$1 == "Key:" {print $2}' Kb8.nz.*.private)";
144 # };
145 # EOF
146
147 # chmod 640 [kK]*
148
149 # # push here?
150 # #myunison -ob li
151 # #ssh li conflink
152 # ssh li.b8.nz systemctl reload bind9
153
154
155 # # b8.nz has address 65.96.178.16
156 # # b8.nz has IPv6 address 2601:197:600:6efb:82fa:5bff:fe1c:6ecf