Best way to toggle WireGuard on and off?

I'm still learning that stuff. But it's just a matter of time...

Note to self: Combining wgon and wgoff scripts into wgtoggle script that turns it on if it's off and turns it off if it's on:

#!/bin/sh

# Get the current WireGuard interface status
wg_status=$(ip addr show wg0 | grep -q UP)

# Check if WireGuard is currently running
if [ "$wg_status" -eq 0 ]; then
  # Interface is UP, so disable WireGuard
  uci -q set network.wg0.disabled='1'
  echo "Disabling WireGuard..."
else
  # Interface is DOWN, so enable WireGuard
  uci -q del network.wg0.disabled
  echo "Enabling WireGuard..."
fi

# Commit changes to network configuration
uci -q commit network

# Restart network service to apply changes
service network restart

echo "Done!"

Save in /usr/local as wgtoggle
Make the script executable using: chmod +x wgtoggle

Run the script with ./wgtoggle to toggle WireGuard based on its current state.

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.