3

When I execute this command to list my network services:

networksetup -listallnetworkservices

I only get Wifi, why my VPN configuration is not listed here (it's in the Network Preferences list) ? And how can I get it listed in the terminal ?

My goal is to export it via a terminal, edit the configuration manually in a text editor and re-import it.

EDIT

I have set "MyVPN" like below and it's connecting via this Network Configuration window. enter image description here

But when trying to list it or run it with:

networksetup -connectpppoeservice "MyVPN"

Or with:

scutil --nc start "MyVPN"
No service

Then it doesn't work.

1 Answer 1

5

Use networksetup or scutil

You can use it to at least establish/ disconnect connections to the VPN.

Use this command to connect the VPN configured with the name "myVPN":

networksetup -connectpppoeservice "myVPN"

Disconnect from the VPN

networksetup -disconnectpppoeservice "myVPN"

When you want to check the connection status

networksetup -showpppoestatus "myVPN"

The "system configuration utility" or scutil command provides access to network configuration, too.

To connect to your VPN use this command:

scutil --nc start "myVPN"

Run the following command to disconnect from the VPN:

scutil --nc stop "myVPN"

If you want to check the connection status, use:

 scutil --nc status "myVPN"

Let's make it even better

Github Code

#!/bin/bash
#
# Provides some basic utilities for VPN connections.

readonly cmd="${1}"
readonly vpn="${2}"


err() {
  echo "$*" >&2
}

validateCmd() {
  if [[ -z "${cmd}" ]]; then 
    err "Command must be provided: [connect, disconnect, status, list]"
    exit 1
  fi
}

validateVpnName() {
  if [[ -z "${vpn}" ]]; then 
    err "VPN name must be provided as second argument"
    exit 1
  fi
}

isConnected() {
  networksetup -showpppoestatus "${vpn}" | grep -qv "^connected$"
}

enterPassword() {
  sleep 1
  osascript -e "tell application \"System Events\" to keystroke \"${1}\""
  osascript -e "tell application \"System Events\" to keystroke return"
}

connect() {
  if ! isConnected; then
    err "Already connected to '${vpn}'"
    exit 1
  fi

  local readonly password="$(security find-generic-password -s "${vpn}" -w)"
  if [[ -z "${password}" ]]; then
    err "Unable to find VPN password in keychain"
    exit 1
  fi

  scutil --nc start "${vpn}"
  enterPassword "${password}"
}

disconnect() {
  scutil --nc stop "${vpn}"
}

status() {
  scutil --nc status "${vpn}"
}

list() {
  scutil --nc list
}

main() {
  validateCmd

  case "${cmd}" in
    connect)
      validateVpnName
      connect "${vpn}"
      ;;
    disconnect)
      validateVpnName
      disconnect "${vpn}"
      ;;
    status)
      validateVpnName
      status "${vpn}"
      ;;
    list)
      list
      ;;
    *)
      err "Unexpected cmd '${cmd}', must be one of [connect, disconnect, status, list]"
      exit 1
      ;;
  esac
}

set -e 
main "${@}"
set +e

Open the script in an editor and add this code. Let's say it's called vpnConnection. Add #!/bin/bash so that it can start without further ado

echo `'#!/bin/bash'` > ~/vpnConnection.sh && chmod +x ~/vpnConnection.sh

./vpnConnection.sh list show all VPN connections

./vpnConnection.sh connect "myVPN" Connect to the VPN "myVPN" and automatically enter the password

./vpnConnection.sh disconnect "myVPN" disconnect the VPN "myVPN"

./vpnConnection.sh status "myVPN" view the connection status for VPN "myVPN"

2
  • 1
    Thanks very much this reply! However, I still can connect to my VPN using these commands, I have updated my post with further details.
    – DevonDahon
    Apr 21, 2021 at 19:38
  • It seems to be due to IKEv2 protocol according to this post.
    – DevonDahon
    Apr 21, 2021 at 19:59

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .