iTranslated by AI
Connecting to VPN via CLI (for Mac)
Introduction
No matter the situation, people never want to leave their terminal.
VPN connections are no exception.
So, let's make it possible to connect to a VPN from the terminal.

The gif above shows the following operations:
- Connect to VPN with vpnc (vpn connect)
- Check status with vpns (vpn status)
- Stop the VPN connection with vpnd (vpn disconnect)
Mac's Default Command: scutil
macOS has a command called scutil.
scutil --help (English translation)
usage: scutil
Interactive access to the dynamic store.
scutil --prefs [preference-file]
[raw] Interactive access to saved settings.
scutil [-W] -r nodename
scutil [-W] -r address
scutil [-W] -r local-address remote-address
Check reachability of node, address, or address pair ( -W for monitoring ).
scutil -w dynamic-store-key [ -t timeout ]
-w Wait for the presence of a dynamic store key.
-t Time to wait for the key.
scutil --get pref
scutil --set pref [newval]
scutil --get filename path key
pref Display or set the specified setting. Valid settings include:
ComputerName, LocalHostName, HostName
newval The new setting value to be set. If not specified, the new value is read from standard input.
scutil --dns
Display DNS settings.
scutil --proxy
Display proxy settings.
scutil --nwi
Display network information.
scutil --nc
Display VPN network configuration information. Refer to --nc help for the full list of commands.
scutil --renew [interface-name]
Re-evaluate network settings for the interface.
scutil --allow-new-interfaces [off|on]
Manage creation of new interfaces when the screen is locked.
scutil --error err#
Display the descriptive message for the specified error code.
Aside:
scutil: sc stands for System Configuration.
pref: short for preference.
VPN management can be handled using scutil --nc [command].
scutil --nc help (English translation)
Usage: scutil --nc [command]
list
List network connection services available in the current set.
status <service>
Indicate whether the specified service is connected and display extended status information for the service.
show <service>
Display configuration information for the specified service.
statistics <service>
Provide statistics on bytes, packets, and errors for the specified service.
select <service>
Make the specified service active in the current set. This allows the service to be started.
start <service> [--user user] [--password password] [--secret secret]
Start the specified service. Can take optional arguments for user, password, and secret.
stop <service>
Stop the specified service.
suspend <service>
Suspend the specified service (PPP, Modem On Hold).
resume <service>
Resume the specified service (PPP, Modem On Hold).
ondemand [-W] [hostname]
ondemand -- --refresh
Display on-demand VPN information.
trigger <hostname> [background] [port]
Trigger on-demand VPN for the specified hostname, optionally specifying a port and background flag.
enablevpn <service or VPN type> [path]
Enable the specified VPN application type. Takes either a service or a VPN type. Pass a path to set the ApplicationURL.
disablevpn <service or VPN type>
Disable the specified VPN application type. Takes either a service or a VPN type.
help
Display available commands for --nc.
You might think, "If I know the service name, I'm all set!" However, even if you check with the networksetup -listallnetworkservices command, IKEv2 will not be displayed.
# IKEv2 is not displayed 😢
$ networksetup -listallnetworkservices
An asterisk (*) denotes that a network service is disabled.
AX88179A
Thunderbolt Bridge
Wi-Fi
What is IKEv2?
IKEv2 (Internet Key Exchange version 2)
- A VPN (Virtual Private Network) protocol.
- Features:
- High security and stability.
- Fast connection.
- Optimized for mobile devices.
In short, IKEv2 cannot be managed using Mac's default scutil command.
Therefore, I would like to introduce an OSS tool that can also manage IKEv2.
VPNStatus
VPNStatus is a tool for managing VPNs on macOS.
You can start, stop, and check the status of your VPN using the vpnutil command.
$ vpnutil --help
vpnutil [start|stop|list|status] [VPN Name]
Example:
Start a VPN named 'MyVPN':
vpnutil start MyVPN
Stop a VPN named 'MyVPN':
vpnutil stop MyVPN
List all available VPNs and their status:
vpnutil list
Get the status of a VPN named 'MyVPN':
vpnutil status MyVPN
Installing VPNStatus
If you are using Homebrew, you can install it with the following commands:
brew tap timac/vpnstatus
brew install timac/vpnstatus/vpnutil
brew install --cask timac/vpnstatus/vpnstatus
Recommended Settings for VPNStatus
I use the following settings to make the vpnutil command easier to use. It allows you to select the VPN you want to connect to using fzf.
# vpnutil ( for Mac )
abbr -S -qq vpn='vpnutil'
alias vpns='check_vpn_status'
alias vpnc='vpn_connect_with_fzf'
alias vpnd='vpn_disconnect_if_connected'
# vpnutil ( for Mac )
check_vpn_status() {
# Extract the output of vpnutil list as json.
vpn_data=$(vpnutil list)
# Extract connected vpn.
connected_vpns=$(echo "$vpn_data" | jq -r '.VPNs[] | select(.status == "Connected") | "\(.name) (\(.status))"')
if [[ -z "$connected_vpns" ]]; then
echo "No Connected"
else
echo "Connected VPN:"
echo "$connected_vpns"
fi
}
vpn_connect_with_fzf() {
# Extract the output of vpnutil list as json.
vpn_data=$(vpnutil list)
# Get the name and status of the VPN and select it with fzf.
selected_vpn=$(echo "$vpn_data" | jq -r '.VPNs[] | "\(.name) (\(.status))"' | fzf --prompt="choose a vpn: ")
# If there is no selected VPN, exit
if [[ -z "$selected_vpn" ]]; then
echo "VPN selection canceled."
return
fi
# Extract the vpn name
vpn_name=$(echo "$selected_vpn" | sed 's/ (.*)//')
# Connection place
echo "connection: $vpn_name"
vpnutil start "$vpn_name"
}
vpn_disconnect_if_connected() {
# Extract the output of vpnutil list as json.
vpn_data=$(vpnutil list)
# Extract connected VPN
connected_vpns=$(echo "$vpn_data" | jq -r '.VPNs[] | select(.status == "Connected") | .name')
if [[ -z "$connected_vpns" ]]; then
echo "No vpn connected."
else
echo "Disconnect the following VPN connections:"
echo "$connected_vpns"
# Turn off each connected VPN.
for vpn in $connected_vpns; do
echo "cutting: $vpn"
vpnutil stop "$vpn"
done
echo "Disconnected all vpn connections."
fi
}
Once you apply the settings with the source command, they will be ready to use.
source ~/.zshrc
Installing fzf
To use the settings above, you need to install fzf.
brew install fzf
Reference: https://formulae.brew.sh/formula/fzf
Conclusion
Since I gave a presentation at Meguro LT titled "CUI is great," I shared some tips related to CUI.
Tomorrow (12/19), the next article for the Meguro LT Advent Calendar is scheduled to be about Biome by K1mu21! Look forward to it! 🎅
Discussion