Skip to content

Instantly share code, notes, and snippets.

@aalhitennf
Created February 5, 2023 10:02
Show Gist options
  • Save aalhitennf/840b9437983d803f994d7bcc3f19dbc1 to your computer and use it in GitHub Desktop.
Save aalhitennf/840b9437983d803f994d7bcc3f19dbc1 to your computer and use it in GitHub Desktop.
Wireguard connection script
#!/bin/sh
CONFDIR="/etc/wireguard/"
readarray -t AVAILABLE_INTERFACES < <(ls $CONFDIR | awk -F '.' '{print $1}')
readarray -t ACTIVE_INTERFACES < <(wg show | grep 'interface: ' | awk -F 'interface: ' '{print $2}')
list_available_interfaces()
{
for i in "${!AVAILABLE_INTERFACES[@]}"
do
interface=${AVAILABLE_INTERFACES[$i]}
echo $interface
done
}
list_active_interfaces()
{
for i in "${!ACTIVE_INTERFACES[@]}"
do
interface=${ACTIVE_INTERFACES[$i]}
echo $interface
done
}
stop_all_active()
{
for i in "${!ACTIVE_INTERFACES[@]}"
do
interface=${ACTIVE_INTERFACES[$i]}
systemctl stop wg-quick@$interface
systemctl disable wg-quick@$interface
done
}
activate_random()
{
stop_all_active
RANDOM=$$$(date +%s)
NEW=${AVAILABLE_INTERFACES[ $RANDOM % ${#AVAILABLE_INTERFACES[@]} ]}
systemctl enable --now wg-quick@$NEW
}
print_ip()
{
wget -qO- http://ipecho.net/plain | xargs echo
}
if [ "$1" = "random" ];
then
activate_random
elif [ "$1" = "stopall" ];
then
stop_all_active
elif [ "$1" = "active" ];
then
list_active_interfaces
elif [ "$1" = "available" ];
then
list_available_interfaces
elif [ "$1" = "ip" ];
then
print_ip
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment