Add command to remove all interfaces

This commit is contained in:
Oliver Traber 2021-07-12 19:29:25 +02:00
parent 5d5a0d03d7
commit ed0d0c8ab6
Signed by: Bluemedia
GPG key ID: C7BA47275B086E2C

View file

@ -97,6 +97,7 @@ create() {
remove() {
if ip link | grep -q "wg-$1"; then
ip link del "wg-$1"
echo "Interface wg-$1 removed."
else
echo "ERROR: Interface \"wg-$1\" could't be removed, because it doesn't exist."
exit 1
@ -104,7 +105,7 @@ remove() {
}
# Call create() for every peer in the configured folder
autostart() {
createall() {
echo "Creating interfaces for peers in path \"$PEER_CONFIG_FOLDER\"."
for peer in $(ls $PEER_CONFIG_FOLDER | egrep -i '.*\.wg\.json' ); do
peer=$(echo "$peer" | sed 's/.wg.json//g')
@ -112,6 +113,15 @@ autostart() {
done
}
# Call remove() for every interface that starts with wg-*
removeall() {
echo "Removing all active interfaces..."
for peer in $(ip link | egrep -o "wg-[^:]*"); do
peer=$(echo "$peer" | sed 's/wg-//')
remove $peer
done
}
# Create an empty config file in the script path
config() {
echo "{
@ -165,8 +175,11 @@ case $ACTION in
create $2
fi
;;
autostart)
autostart
allup)
createall
;;
alldown)
removeall
;;
config)
config
@ -185,9 +198,10 @@ case $ACTION in
echo " up <peername> Create new wireguard interface for a given peer."
echo " down <peername> Remove wireguard interface for a given peer."
echo " restart <peername> Recreate the wireguard interface for a given peer."
echo " allup Create interfaces for all peers in configured folder."
echo " alldown Remove all active wireguard interfaces."
echo " config Create an empty config file in the script path."
echo " template <path> Create an empty peer config template."
echo " autostart Create interfaces for all peers in configured folder."
;;
*)
echo "Invalid action \"$ACTION\"."' Use "./wireguard.sh help" to get help.'