-
-
Save adgedenkers/3874427 to your computer and use it in GitHub Desktop.
tell application "System Events" | |
-- get current clipboard contents as a string | |
set CurrentClipboard to the clipboard as string | |
-- set the clipboad to your password | |
set the clipboard to "Y0urVPNPa$$w0rd" | |
-- start playing with the VPN | |
tell current location of network preferences | |
-- set the name of the VPN service from your Network Settings | |
set VPNService to service "VPN Connection 01" | |
-- determine current VPN connection status | |
set isConnected to connected of current configuration of VPNService | |
-- if connected, then disconnect | |
if isConnected then | |
disconnect VPNService | |
else -- otherwise, connect to the VPN | |
connect VPNService | |
-- wait 10 seconds before pasting in the password | |
delay 10 | |
tell application id "com.apple.systemevents" | |
-- paste clipboard contents into password box | |
keystroke "v" using {command down} | |
-- press "Enter" | |
keystroke (key code 36) | |
-- wait 10 seconds to connect | |
delay 10 | |
-- determine current VPN connection status (after providing password) | |
set nowConnected to connected of current configuration of VPNService | |
-- if we're now connected ... | |
if nowConnected then | |
-- press "Enter" again to get rid of a dialog confirmation prompt, if one exists | |
keystroke (key code 36) | |
-- now, execute any other commands you want (ping a server to check its status, open mail, etc.) | |
end if | |
end tell | |
end if | |
end tell | |
-- now reset the clipboard to what it was before we started | |
set the clipboard to CurrentClipboard | |
end tell |
Excellent! Thanks!
One more thanks!!
Thank you :)
I suggest adding the following (as I got some error messages from time to time with the above script):
Just before setting the current clipboard contents as as a string, clear the clipboard:
try
set the clipboard to ""
on error err_message
display dialog err_message
end try
This no longer works in 10.11, it produces the error, execution error: System Events got an error: Can’t get current configuration of service id "18E8C59B-C186-4669-9F8F-FA67D7AA6E53" of network preferences. (-1728)
. See http://stackoverflow.com/questions/32957121/in-mac-os-x-10-11-opening-a-vpn-connection-window-with-the-command-line-gives-m for a workaround.
scutil workaround works like a charm! Also for common Cisco IPsecs no Oauth is needed
While this looks like a nifty script, I don't like the idea of storing the password in plain text. It would be great if someone could add the ability for the password to come from Keychain item. For now, I'm using this scutil solution: http://superuser.com/a/934577
@antialias I'm seeing the same issue and I've substituted every field accordingly. I've also tried @erikhansen's scutil
solution, but it always returns with No Service
. This this an issue with OS X 10.11.4?
Updated bash version:
VPN="YOUR_VPN_NAME"
IS_CONNECTED=$(test -z `scutil --nc status "$VPN" | grep Connected` && echo 0 || echo 1);
if [ $IS_CONNECTED = 1 ]; then
scutil --nc stop "$VPN"
else
scutil --nc start "$VPN"
fi
Oliver, that's neat, but 'if' can test the exit status of commands directly:
VPN="YOUR_VPN_NAME"
if scutil --nc status "$VPN" | grep -q Connected; then
scutil --nc stop "$VPN"
else
scutil --nc start "$VPN"
fi
sweet! just what I was looking for ;)
Always No service.
Seems something wrong with IKEv2 on macOS 10.12
Anyone helps?
I ran into the "Can’t get current configuration" and "No service" errors after upgrading to Sierra. Duplicating the VPN configuration in Network Preferences (then using the new entry with scutil
) fixed it for me.
@sbine What do you mean by using new entry with scutil ? I'm stuck. :-/
Won't work with IKEv2 VPN's. Heres how you can do that: http://matt.coneybeare.me/how-to-setup-an-auto-reconnect-script-for-an-ikev2-vpn-service-on-your-mac/
@spacebarrier I know it's been 6 months, but the terminal command
scutil --nc start "VPN_service_name"
will start the VPN service from a terminal command in High Sierra (obviously, replace VPN_service_name
with the actual service name in the Network preference pane).
Replacing start
with stop
will, obviously, stop the VPN.
The terminal command can be run in AppleScript by using the syntax:
do shell script "scutil --nc start \"VPN_service_name\""
where the slashes are necessary to escape the quotes.
Hello! Thank you for all these information. I've connected to VPN using Cisco AnyConnect. I tried with different possible names and server name as VPN, but I got "No service" error. Is there anyway we can list the available VPN connection names?
@tuxian0 If you're using the Cisco AnyConnect client then none of this will work. You could try scripting the Cisco client directly, or see if your VPN configuration can be redone with the native macOS VPN client, configured in System Preferences -> Network.
Either this doesn’t work in 10.15 or it doesn’t work with the NordVPN IKE client. Iot doesn’t show up in the scutil —nc list
command
@LwsBtlr , this is likely because you are using IKEv2 services. I had a similar issue trying to fix my VPN to auto reconnect. Below is the solution I eventually used. This may point you in the direction to solve your issue:
===
I simply downloaded the precompiled app here.
https://blog.timac.org/2018/0719-vpnstatus/
If you are interested in the source code that makes it work, you can find it on GitHub here: https://github.com/Timac/VPNStatus
@stevesoto: on line, 12, replace "VPN Connection 01" with the name of your VPN connection interface as it appears in the network preferences pane