See & Toggle Wi-Fi and VPN in one touchbar button

See & Toggle Wi-Fi and VPN Status with one button

  • If Wi-Fi is off, the button says "Wi-Fi" (in red)
  • If Wi-Fi is on, and VPN is connected, the button says the Wi-Fi SSID (in green)
  • If Wi-Fi is on, but VPN is disconnected, the button says the Wi-Fi SSID (in red)

Actions
Tap button once to toggle Wi-Fi
Long-tap button to toggle VPN

Installation & Configuration
1. Allow Scripting & Configure Shared Secret
If your BetterTouchTool is configured to use a shared secret, get it from the advanced settings dialog (see here, chapter "security"). If the shared secret field is empty, you can ignore the shared secret altogether and do not need to change this part of the scripts. If you would like to use one, or if it's already configured with other plugins, copy the shared secret to your clipboard.

Toggle Wi-Fi
The script tries to figure out your Wi-Fi network adapter's BSD name (like en0, en1, etc.) to toggle it.

VPN
It doesn't matter what kind of VPN you have, as long as you configured it directly through macOS's System Preferences (Network option) and as long as they start with "VPN " (e.g. "VPN USA", "VPN MyCompany"). The script to toggle VPN is configured in BetterTouchTool's "Other"-Tab.
This script finds all VPN connections and sorts them alphabetically. That means, that if you have 2 VPN networks, let's say "VPN 1 MyCompany" and "VPN 2 MyCompany", the alphabetically first one will be the default one it auto-connects to.

Known limitation: if your VPN is not a L2TP one, please adjust the word it searches for in the Touchbar-button and toggle vpn script. (I only have this kind, so I don't know exactly what other values could show up. If you have such a case, let me know and I'll implement it9o

Download
https://share.folivora.ai/sharedPreset/6ffe27cb-4f5c-4ee4-8512-9b7ce7124d65


Screenshots (optional)
The auto-connect dialog and the three different states of the touchbar button (My Wi-Fi SSID starts with "Sunrise_"


I've sunk an hour into researching what the right commands and keywords are for an openVPN with no luck. Then again, I don't know what I'm doing when it comes to networks. Any ideas on where to start?

Took me a little time to create this one, not sure if its perfect for every occasion, but works flawless right now.

I have my VPN button coded as below. Your VPN must be setup in Apple Network Prefs, you will use that Service Name below (Mine = NordVPN). Notice the "ipsec1" that is my VPN interface (When VPN is ACTIVE) when checking for interfaces using terminal command: ifconfig -lu


Setup as: Widget/Gesture

PREDEFINED ACTION: (AppleScript Blocking)

set activeInterfaces to do shell script "ifconfig -lu"
if activeInterfaces contains "ipsec1" then
display notification "NordVPN Service Disconnected" with title "VPN Disconnecting..." sound name "Bottle"
set service_name to "NordVPN"
if service_name is not null then
do shell script (do shell script "/usr/local/bin/vpnutil stop "" & service_name & """)
end if
else
display notification "NordVPN Service is Connected " with title "VPN Connecting..." sound name "Bottle"
if activeInterfaces does not contain "ipsec1" then
set service_name to "NordVPN"
if service_name is not null then
do shell script (do shell script "/usr/local/bin/vpnutil start "" & service_name & """)
end if
end if
end if

Advanced Configuration:
Run AppleScript Show return

set activeInterfaces to do shell script "ifconfig -lu"
if activeInterfaces contains "ipsec1" then
return "VPN ON"
else
return "VPN OFF"
end if

2 Likes

Is anyone having a solution to connect a IKEV2 VPN, or retrieve vpn status in OSX Catalina ?
scutil nor Applescript are not working anymore, cannot find a solution

@lost run "ifconfig -lu" in terminal while VPN is ACTIVE and show me the output.

lo0 bridge0 en0 en1 en2 p2p0 awdl0 llw0 utun0 utun1 utun2 utun3 utun4 utun5 utun6 utun7 ipsec0

see here :slight_smile: https://blog.timac.org/2018/0719-vpnstatus/

set activeInterfaces to do shell script "ifconfig -lu"

if activeInterfaces contains "ipsec" then

display notification "VPN Service Disconnected" with title "VPN Disconnecting..." sound name "Bottle"

set service_name to "IKE-NAME"

if service_name is not null then

do shell script (do shell script "/usr/local/bin/vpnutil stop "" & service_name & """)

end if

else

display notification "VPN Service is Connected " with title "VPN Connecting..." sound name "Bottle"

if activeInterfaces does not contain "ipsec" then

set service_name to "IKE-NAME"

if service_name is not null then

do shell script (do shell script "/usr/local/bin/vpnutil start "" & service_name & """)

end if

end if

end if

Have you tried something like this? Service name "IKE-NAME" change to the VPN name you have in your apple network pref's.

Hello, thanks and sorry for the late reply,
tried to run it as a shell script in term & BTT, get the error: zsh: parse error near `do'

trying the script return me this:
/bin/bash: -c: line 10: syntax error near unexpected token do' /bin/bash: -c: line 10: do shell script (do shell script "/usr/local/bin/vpnutil stop "" & service_name & """)'

I can start manually my interface with vpnutil start and shut it down with vpnutil stop whithout problems.

if I try to simply run vpnutil start MyVpn with BTT, I'm given a /bin/bash: vpnutil: command not found.

Sorry as I'm new with BTT. Using the new configuration interface, what I have to call ? a shell script.
Butcher to put the "Advanced configuration" AppleScript ? your script work nicely and return the state on my connection correctly. Sorry for the dumb question, I'm a bit... Lost :smiley:

Ive attached the preset for you to import. Its AppleScript, using shell scripts, not a shell script in BTT.
You will need to change/replace my coded name "VPN Network-NAME" in my code to what you have in Apple Network Settings for your VPN connection.

K2 VPN.bttpreset (27.1 KB)

Thank you much. Work as a charm, I'm looking how you have done that. Again, thanks

1 Like

I downloaded this, but I can't get it to connect/disconnect from my VPN. The icon is correct, whether I'm connected or disconnected to VPN. If I'm connected, I'll press the lock, then it says "Disconnecting" then it does nothing. Any ideas?

Can you show a screenshot of the actions AppleScript?

set activeInterfaces to do shell script "ifconfig -lu"

if activeInterfaces contains "ipsec" then

display notification "VPN Service Disconnected" with title "VPN Disconnecting..." sound name "Bottle"

set service_name to "nVPN"

if service_name is not null then

do shell script (do shell script "/usr/local/bin/vpnutil stop \"" & service_name & "\"")

end if

else

display notification "VPN Service is Connected " with title "VPN Connecting..." sound name "Bottle"

if activeInterfaces does not contain "ipsec" then

set service_name to "nVPN"

if service_name is not null then

do shell script (do shell script "/usr/local/bin/vpnutil start \"" & service_name & "\"")

end if

end if

end if

returnToBTT('please return a string like this somewhere in your script');

Screen Shot 2020-03-23 at 6.37.22 PM

This is what my VPN is named.

wonder if you do not have vpnutil installed.

Try in terminal: /usr/local/bin/vpnutil or just vpnutil

I don't. Is that a BTT command, or something on the system I need to install?

I tried searching for vpnutil but I don't seen anything. Is this a BTT extension, or something on OS X I need to install?

Here is VPNUTIL required for my script

vpnUtil.zip (9.6 KB)

1 Like

Thank you!