Last active
September 15, 2024 18:13
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/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 |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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:
Usage:
manage_vpn.sh).chmod +x manage_vpn.sh./manage_vpn.shRequirements:
scutilcommand for VPN management)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.