minor bug fix
[distro-setup] / lan-dyn-dns-update
1 #!/bin/bash
2
3 # usage: I run this script on a timer to correct the dns on wrt for
4 # clients where dns is updated dynamically by openvpn, in the case that
5 # openvpn crashes or the computer running openvpn crashes.
6
7 set -eE -o pipefail
8 trap 'echo "$0:$LINENO:error: \"$BASH_COMMAND\" returned $?" >&2' ERR
9
10 x="$(readlink -f "$BASH_SOURCE")"; cd ${x%/*} # directory of this file
11
12 while read -r ip host; do
13 found=false
14 is_connected="grep -q "^CLIENT_LIST,$host," /run/openvpn-server/status-server.log"
15 if $is_connected; then continue; fi
16 if ! grep -q "^CLIENT_LIST,$host," /run/openvpn-server/status-server.log; then
17 cd $(mktemp -d); dir=$PWD
18 ssh wrt tar -C /etc -c hosts | tar -x
19 if grep -qFx "$ip $host" hosts; then continue; fi
20 # openvpn udpates its status file every 60 seconds by default
21 if (( $(stat -c%Y hosts) > $(( $(date +%s) + 60 )) )); then
22 sleep 60
23 if $is_connected; then continue; fi
24 fi
25 ssh root@wrt.b8.nz cedit ovpn-$host <<<"$ip $host" || [[ $? == 1 ]]
26 fi
27 done <lan-dns