diff options
Diffstat (limited to 'data.d/vpn-wireguard/hooks/post-up')
-rwxr-xr-x | data.d/vpn-wireguard/hooks/post-up | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/data.d/vpn-wireguard/hooks/post-up b/data.d/vpn-wireguard/hooks/post-up new file mode 100755 index 0000000..edbcd50 --- /dev/null +++ b/data.d/vpn-wireguard/hooks/post-up @@ -0,0 +1,47 @@ +#!/usr/bin/env bash + +readonly COLOR_RESET="\033[0m" +readonly COLOR_OK="\033[32;1m" +readonly COLOR_NOK="\033[31;1m" +readonly BUFFER="$(mktemp)" + +main() { + printf "Verifying connectability...\n" + + # Ping all known hosts, as it seems that the wireguard interface comes up when + # only after it gets used on the machine itself. + while read -r addr; + do + check "$addr" & + done < <(awk -F= '/vpn-wireguard.ipv(4|6)=/ { print $NF }' /etc/bashtard/hosts.d/*) + + wait + + sort -- "$BUFFER" >&2 +} + +check() { + local addr="$1" + + if ping -c 1 -q -w 1 "$addr" > /dev/null + then + log OK "$addr" + else + log NOK "$addr" + fi +} + +log() { + local state="$1" + local addr="$2" + local color="$COLOR_NOK" + + if [[ $state == "OK" ]] + then + color="$COLOR_OK" + fi + + printf "%b%3s%b: %s\n" "$color" "$state" "$COLOR_RESET" "$addr" >> "$BUFFER" +} + +main "$@" |