From 090483be6cf72e353e89c5a4fd118b7bdbe57baf Mon Sep 17 00:00:00 2001 From: Patrick Spek Date: Thu, 15 Feb 2024 11:28:32 +0100 Subject: Update post-up hook for wireguard --- data.d/vpn-wireguard/hooks/post-up | 50 ++++++++++++++++++++++++++++++++------ 1 file changed, 42 insertions(+), 8 deletions(-) diff --git a/data.d/vpn-wireguard/hooks/post-up b/data.d/vpn-wireguard/hooks/post-up index 948a9a8..a824a30 100755 --- a/data.d/vpn-wireguard/hooks/post-up +++ b/data.d/vpn-wireguard/hooks/post-up @@ -1,10 +1,44 @@ #!/usr/bin/env bash -# 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 - ping -c 1 -q -w 1 "$addr" & -done < <(awk -F= '/vpn-wireguard.ip/ { print $NF }' /etc/bashtard/hosts.d/*) - -wait +readonly COLOR_RESET="\033[0m" +readonly COLOR_OK="\033[32;1m" +readonly COLOR_NOK="\033[31;1m" + +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.ip/ { print $NF }' /etc/bashtard/hosts.d/*) + + wait +} + +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" >&2 +} + +main "$@" -- cgit v1.1