DEV Community

Connect to a VPN from the Command Line on Mac OS

Andreas Siegel on May 03, 2020

During Corona time, I have to connect to a VPN most of the time in order to work. I use two different VPNs depending on what I'm working on. I can ...
Collapse
 
patakijv profile image
John Pataki • Edited

Thanks for this. It is a great script and teaching tool for using macos command line tools.

Short: Does this need to be updated for Monterey (macos 12) or for Apple Silicon chipsets?

Longer: I've been using this script successfully on my 2019 Macbook Pro that is running Big Sur (macos 11) (with the modification referred to in the comments for Big Sur). I briefly had a test machine for the 2021 Macbook Pro M1 that was running Monterey (macos12) and it seems like some things change so the script didn't work correctly. I already returned the machine (getting the M1 Pro as a replacement) and can't recall exactly but I think the format of the output that checks the connected state was different which was throwing the script off. Has anyone modified the script so it works on Monterey (and possibly also is backward compatible?)

Collapse
 
josephfisher profile image
Joseph Fisher • Edited

I appreciate your detailed explanation of different ways to connect to a VPN from the command line. It's great to have alternatives to the graphical interface for more efficient workflow. Additionally, I wanted to mention that if you're looking for a reliable VPN solution, Hotspot Shield VPN is worth considering. It offers secure and private browsing, protects your online activities, and provides access to blocked content. With its user-friendly interface and strong encryption, Hotspot Shield VPN ensures a safe and seamless VPN experience.

Collapse
 
gamergun profile image
GamerGun

Since Big Sur, it inserts the password into the "Account Name" field (probably because the focus is on that field by default). Any idea how to resolve that?

Collapse
 
tuksaur profile image
tuksaur

you can add tab to the enterPassword function after sleep 1, should resolve from printing password in username field.

osascript -e "tell application \"System Events\" to keystroke tab"

Collapse
 
gamergun profile image
GamerGun

Thank you, works like a charm!

Collapse
 
laureano_galarraga_9f4cdb profile image
Laureano Galarraga

Hi!
Thanks, worked perfectly, but I had to make a small modification.
In my case, when the script tried to input the password, it'll put it in the username filed, then hit return and fail.
I had to add:
osascript -e "tell application \"System Events\" to keystroke (ASCII character 9)"
before
osascript -e "tell application \"System Events\" to keystroke \"${1}\""
osascript -e "tell application \"System Events\" to keystroke return"

to send the tab key and input the password in the correct field.
Hope that helps!

Collapse
 
pchavanc profile image
pchavanc

Hi,
thank you for this post.
I have EdgeMax as my VPN/firewall & I was always able to connect through my mac VPN GUI client. I recently upgraded to Big Sur & my client no longer connect the way it used to. One of the other websites suggested connecting through the scripts & was trying your way to VPN connect.
I get the following error . Any suggestion on how this could be resolved?

L2TP: incorrect user shared secret found.

The script does not mention of a shared key.

Regards,
Prakash

Collapse
 
jamesmorr profile image
Jamesmorr

I'm looking for a VPN at the moment. There are a lot of reviews vpnwelt.com/vpn-mac/ such as this but they didn't count a user experience. Gonna try a native client. Thanks.

Collapse
 
teaglebuilt profile image
dillan teagle

what vpn clients do you use?

Collapse
 
andreassiegel profile image
Andreas Siegel

I use the native client in Mac OS which is available by default. In the past, I used Cisco AnyConnect but I don't see a need for it as the default is working just fine.

Collapse
 
redhat57 profile image
FireHawk

Hi, I'm unable to run that script in cronjob. It cannot fetch password from keychain. Please help me.

Collapse
 
grafst profile image
grafst

That is a great Idea

However, when running the script I get:

security: SecKeychainSearchCopyNext: The specified item could not be found in the keychain.
Unable to find VPN password in keychain

Collapse
 
marcoslhc profile image
Marcos Hernández

This assumes your VPN server is supported by Mac Native Client. If you use OpenVPN you are out of luck :(

Collapse
 
marcoslhc profile image
Marcos Hernández

The scripts are awesome though. Great work!

Collapse
 
doublefire_chen profile image
Chen Yan

Hi, bro. I want to change my VPN server address using command line, how can I make it? Thank you in advance.