Skip to content

Instantly share code, notes, and snippets.

@brokosz
Last active September 15, 2024 18:13
Show Gist options
  • Save brokosz/4d25a43eacf6f70adbb9a12cfd456ea6 to your computer and use it in GitHub Desktop.
Save brokosz/4d25a43eacf6f70adbb9a12cfd456ea6 to your computer and use it in GitHub Desktop.
A simple Bash script for quickly connecting to or disconnecting from VPNs on macOS via the command line.
#!/bin/bash
# Check if scutil command is available
if ! command -v scutil &> /dev/null; then
echo "Error: scutil command not found."
exit 1
fi
# Get the list of VPN services
vpn_list=$(scutil --nc list | grep 'VPN' | awk -F'"' '{print $2}')
# Check if any VPNs are found
if [ -z "$vpn_list" ]; then
echo "No VPNs found."
exit 1
fi
# Display VPN options with status
echo "Available VPNs:"
index=1
while IFS= read -r vpn_name; do
status=$(scutil --nc status "$vpn_name" | grep -v 'NEStatus' | grep 'Status :' | awk '{print $3}')
status_text=$([ "$status" -eq 2 ] && echo "Connected" || echo "Disconnected")
printf "%d) %s\t(%s)\n" $index "$vpn_name" "$status_text"
((index++))
done <<< "$vpn_list"
# Prompt user to select a VPN or quit
read -p "Select a VPN number or (q)uit: " vpn_index
# Check if the user wants to quit
if [[ "$vpn_index" == "q" ]]; then
echo "Exiting."
exit 0
fi
# Validate input
if ! [[ "$vpn_index" =~ ^[0-9]+$ ]] || [ "$vpn_index" -lt 1 ] || [ "$vpn_index" -gt "$(wc -l <<< "$vpn_list")" ]; then
echo "Invalid selection."
exit 1
fi
selected_vpn=$(sed "${vpn_index}q;d" <<< "$vpn_list")
current_status=$(scutil --nc status "$selected_vpn" | grep -v 'NEStatus' | grep 'Status :' | awk '{print $3}')
# Perform action based on current status
if [ "$current_status" -eq 2 ]; then
echo "Disconnecting from $selected_vpn..."
scutil --nc stop "$selected_vpn"
target_status=0
else
echo "Connecting to $selected_vpn..."
scutil --nc start "$selected_vpn"
target_status=2
fi
# Function to get current VPN status
get_vpn_status() {
scutil --nc status "$1" | grep -v 'NEStatus' | grep 'Status :' | awk '{print $3}'
}
# Wait for status change
while true; do
new_status=$(get_vpn_status "$selected_vpn")
if [ "$new_status" -eq "$target_status" ]; then
status_text=$([ "$new_status" -eq 2 ] && echo "connected" || echo "disconnected")
echo "$selected_vpn is now $status_text."
break
fi
sleep 1
done
@brokosz
Copy link
Author

brokosz commented Sep 15, 2024

Simplified macOS VPN Management Script

This Bash script provides a straightforward command-line interface for managing VPN connections on macOS. It simplifies the process of connecting to and disconnecting from VPNs configured in your system preferences.

Features:

  • Automatically detects and lists all configured VPN connections on your Mac.
  • Displays the current connection status of each VPN.
  • Allows you to select a VPN from the list using a simple numerical input.
  • Automatically connects to disconnected VPNs or disconnects from connected VPNs based on their current status.
  • Provides real-time feedback on the connection/disconnection process.
  • Waits for and confirms the status change before exiting.

Usage:

  1. Save the script to a file (e.g., manage_vpn.sh).
  2. Make it executable: chmod +x manage_vpn.sh
  3. Run the script: ./manage_vpn.sh
  4. Follow the on-screen prompts to select and manage your VPN connection.

Requirements:

  • macOS (uses the scutil command for VPN management)
  • Bash shell

This script is ideal for users who prefer a quick, terminal-based method to toggle their VPN connections without navigating through the macOS System Preferences GUI.

Note: This script requires appropriate permissions to manage network connections. You may need to run it with sudo if you encounter permission issues.

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