Skip to content

Instantly share code, notes, and snippets.

@mgarces
Last active January 2, 2025 18:41
Show Gist options
  • Select an option

  • Save mgarces/b8496f8cf9279e4192dcd49117d0df30 to your computer and use it in GitHub Desktop.

Select an option

Save mgarces/b8496f8cf9279e4192dcd49117d0df30 to your computer and use it in GitHub Desktop.
NetBird profile switch
#!/bin/bash
# Define color codes
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[0;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# Function to list available profiles
list_profiles() {
local running_profile=$(get_running_profile)
echo -e "${BLUE}Available profiles:${NC}"
for profile in $(ls /etc/netbird/*.json | xargs -n 1 basename | cut -d'.' -f1 | sed 's/^config$/default/'); do
if [ "$profile" == "$running_profile" ]; then
echo -e "- ${GREEN}$profile [*]${NC}"
else
echo "- $profile"
fi
done
echo
}
# Function to determine the currently running profile
get_running_profile() {
local running_profile=$(ps aux | grep "[n]etbird" | grep "service run" | awk {'print $15'} | cut -d"." -f1 | cut -d"/" -f4)
if [ "$running_profile" == "config" ]; then
echo "default"
else
echo "$running_profile"
fi
}
# Check if a profile was provided
if [ -z "$1" ]; then
list_profiles
RUNNING_PROFILE=$(get_running_profile)
if [ -z "$RUNNING_PROFILE" ]; then
echo -e "${YELLOW}No Netbird profile is currently active.${NC}"
else
echo -e "${BLUE}Currently running profile: ${GREEN}$RUNNING_PROFILE${NC}"
fi
exit 0
fi
PROFILE=$1
# Check if the profile exists
if [ "$PROFILE" == "default" ]; then
CONFIG_FILE="/etc/netbird/config.json"
else
CONFIG_FILE="/etc/netbird/$PROFILE.json"
fi
if [ ! -f "$CONFIG_FILE" ]; then
echo -e "${RED}Error: Profile '$PROFILE' does not exist.${NC}"
exit 1
fi
# Determine the currently running profile
RUNNING_PROFILE=$(get_running_profile)
# Check if the desired profile is already running
if [ "$RUNNING_PROFILE" == "$PROFILE" ]; then
echo -e "${YELLOW}The profile '$PROFILE' is already running.${NC}"
exit 0
fi
# Start or switch profiles
if [ -z "$RUNNING_PROFILE" ]; then
echo -e "${BLUE}Starting profile '$PROFILE'...${NC}"
else
echo -e "Switching from profile '${BLUE}$RUNNING_PROFILE'${NC} to ${BLUE}'$PROFILE'${NC}"
if ! netbird down; then
echo -e "${RED}Error: Failed to bring down the Netbird service.${NC}"
exit 1
fi
if [ "$RUNNING_PROFILE" == "default" ]; then
sudo netbird service stop --service netbird
else
sudo netbird service stop --service "$RUNNING_PROFILE"
fi
fi
# Start the new profile
sleep 3
if [ "$PROFILE" == "default" ]; then
sudo netbird service start --service netbird
else
sudo netbird service start --service "$PROFILE"
fi
if netbird up; then
echo
netbird status
else
echo -e "${RED}Error: Failed to bring up the Netbird service for profile '$PROFILE'.${NC}"
fi
@mgarces
Copy link
Author

mgarces commented Dec 10, 2024

how to create a profile

netbird service install --service <profile> --config /etc/netbird/<profile>.json

This will create a <profile>.json that will be used when you manually invoke said profile; this above script makes profile switching easier and takes into account the fact that the default profile (netbird) is not named, so the config file is just called config.json.

The script also handles the fact that you can only be running one profile at the time, since netbird client will only attach itself to one socket.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment