#!/bin/bash# Quick toggle script to bring WireGuard connections up or down using WG-Quick.## Usage: manage-vpn.sh [interface-or-config]# interface-or-config a name from /etc/wireguard (e.g. wg0) or a full path to a .conf file.# Defaults to $WG_CONFIG, then to the only config in /etc/wireguard.CONFIG="${1:-${WG_CONFIG:-}}"# Pick the only config in /etc/wireguard if none was givenif[-z"$CONFIG"];thenmapfile-tconfigs<<(sudofind/etc/wireguard-maxdepth1-name'*.conf'-printf'%f\n'2>/dev/null|sed's/\.conf$//')if[${#configs[@]}-eq1];thenCONFIG="${configs[0]}"elseecho"Which WireGuard config? Pass a name or path, or set WG_CONFIG."[${#configs[@]}-gt1]&&echo"Found in /etc/wireguard: ${configs[*]}"exit1fifi# Interface name, for the status checkIFACE=$(basename"$CONFIG".conf)is_up(){iplinkshow"$IFACE">/dev/null2>&1;}turn_on(){echo"Turning on WireGuard interface $IFACE..."ifsudowg-quickup"$CONFIG";thenecho"WireGuard interface $IFACE is now up."elseecho"Failed to bring up the WireGuard interface $IFACE.">&2return1fi}turn_off(){echo"Turning off WireGuard interface $IFACE..."ifsudowg-quickdown"$CONFIG";thenecho"WireGuard interface $IFACE is now down."elseecho"Failed to bring down the WireGuard interface $IFACE.">&2return1fi}ifis_up;thenecho"WireGuard $IFACE is currently: UP"elseecho"WireGuard $IFACE is currently: DOWN"fiecho"1) Turn On"echo"2) Turn Off"read-rp"Please enter your choice (1 or 2): "choice
case"$choice"in1)turn_on;;2)turn_off;;*)echo"Invalid choice. Please enter 1 or 2."exit1;;esac