Files
dotfiles/config/waybar/scripts/vpn-check.sh
T
2026-09-23 00:44:36 +05:00

69 lines
2.3 KiB
Bash
Executable File

#!/usr/bin/env bash
# Check for VPN connections using nmcli device show
# Initialize variables
vpn_status=""
vpn_tooltip=""
vpn_class="disconnected"
vpn_count=0
# Get all VPN devices and their details
while IFS= read -r device; do
if [ -n "$device" ]; then
# Get device type and connection
device_info=$(nmcli device show "$device" 2>/dev/null)
connection=$(echo "$device_info" | grep "GENERAL.CONNECTION:" | awk -F: '{print $2}' | xargs)
device_type=$(echo "$device_info" | grep "GENERAL.TYPE:" | awk -F: '{print $2}' | xargs)
ip_addr=$(echo "$device_info" | grep "IP4.ADDRESS" | head -1 | awk -F: '{print $2}' | xargs)
if [ -n "$connection" ] && [ "$connection" != "--" ]; then
((vpn_count++))
# Determine VPN type
if [[ "$device" == wg* ]]; then
vpn_type="WG"
vpn_icon="🔒"
elif [[ "$device" == tun* ]]; then
vpn_type="OVPN"
vpn_icon="🔒"
else
vpn_type="VPN"
vpn_icon="🔒"
fi
# Build status text
if [ -z "$vpn_status" ]; then
vpn_status="$vpn_icon $vpn_type"
vpn_tooltip="$vpn_type: $connection"
else
vpn_status="$vpn_status | $vpn_icon $vpn_type"
vpn_tooltip="$vpn_tooltip\n$vpn_type: $connection"
fi
# Add IP if available
if [ -n "$ip_addr" ]; then
vpn_tooltip="$vpn_tooltip\nIP: $ip_addr"
fi
# Set class based on first VPN type found
if [ "$vpn_class" = "disconnected" ]; then
if [[ "$device" == wg* ]]; then
vpn_class="connected-wg"
elif [[ "$device" == tun* ]]; then
vpn_class="connected-ovpn"
else
vpn_class="connected"
fi
fi
fi
fi
done < <(nmcli -t -f DEVICE device status | grep -E '^(tun|wg)' | cut -d: -f1)
# If no VPN found, show empty
if [ $vpn_count -eq 0 ]; then
echo "{\"text\":\"\", \"tooltip\":\"No VPN connected\", \"class\":\"disconnected\"}"
else
echo "{\"text\":\"$vpn_status\", \"tooltip\":\"$vpn_tooltip\", \"class\":\"$vpn_class\"}"
fi