Thursday, July 20, 2017

#1 2016-02-03 04:33:51 pm

sharkbone
Member
Registered: 2016-01-28
Posts: 11

AppleScript to Connect/Disconnect VPN Cisco IPSec no longer working.

Hello MacScripter Forums! I am a new member to these boards and this is my first post. First of all I am not a seasoned AppleScript user by any means. But I am trying to learn and this seems like the best place to do that. I have a script that turns VPN on and off depending on its current status; If on it turns off, If off it turns on. I didn't write this script, I found it (probably on this site though I can't be sure anymore.) It used to word flawlessly before upgrading to El Capitan. Now it only works if I setup and use L2TP when before I was using Cisco IPSec. When I try to use Cisco IPSec it returns this error:

error "System Events got an error: Can’t get current configuration of service id \"6508D6E2-6BE4-4698-913B-EDFECE4BE6B9\" of network preferences." number -1728 from current configuration of service id "6508D6E2-6BE4-4698-913B-EDFECE4BE6B9" of network preferences

Any and all help would be very much appreciated. Thank you.

Here is the AppleScript:

Applescript:

tell application "System Events"
   tell current location of network preferences
       set VPNservice to service "VPN (Cisco IPSec)"
       if exists VPNservice then
           set isConnected to connected of current configuration of VPNservice
           if isConnected then
               disconnect VPNservice
               set msg to "Disconnected"
           else
               connect VPNservice
               set isConnectedCheck to false
               set x to 0
               repeat 10 times
                   set x to (x + 1)
                   set isConnectedCheck to connected of current configuration of VPNservice
                   if isConnectedCheck then
                       set msg to "Connected success in " & (x * 2) & "s "
                       exit repeat
                   else
                       delay 2
                   end if
               end repeat
               if not isConnectedCheck then
                   set msg to "Connect fail in 20s"
               end if
           end if
       end if
   end tell
end tell

Model: iMac
AppleScript: 2.8.1
Browser: Safari 9.0.3
Operating System: Mac OS X (10.10)


Filed under: , Error, vpn, Cisco IPSec

Offline

 

#2 2017-01-10 01:22:16 pm

chrismo
Member
Registered: 2017-01-10
Posts: 1

Re: AppleScript to Connect/Disconnect VPN Cisco IPSec no longer working.

I'm having the same problem after upgrading to Sierra. I tried recreating the VPN connection configuration, but that didn't matter. My next guess is the Cisco VPN client perhaps is to blame, but don't know enough about that yet to try a new version of it, but that's my next thing to try when I have some time.

Offline

 

#3 2017-04-26 03:36:24 pm

misterfriendly
Member
From:: Massachusetts, USA
Registered: 2007-10-16
Posts: 108

Re: AppleScript to Connect/Disconnect VPN Cisco IPSec no longer working.

thanks to this guy: https://www.alfredforum.com/topic/8098- … l-capitan/

Here's a very basic script that toggles your VPN connection. Of course you need to have already configured the VPN in your network prefs.

Applescript:


set theVPN to "name_of_your_VPN"

set amIconnected to (paragraph 1 of (do shell script "scutil --nc show " & theVPN))
if amIconnected contains "(Connected)" then
   set theAction to "stop"
else if amIconnected contains "(Disconnected)" then
   set theAction to "start"
end if

do shell script "scutil --nc " & theAction & space & theVPN

Sometimes you just gotta use the command line.

Last edited by misterfriendly (2017-04-26 03:38:08 pm)

Offline

 

#4 2017-04-27 07:01:05 pm

sharkbone
Member
Registered: 2016-01-28
Posts: 11

Re: AppleScript to Connect/Disconnect VPN Cisco IPSec no longer working.

Thanks for responding with this. The script works quite nicely.

However, if your service is VPN (Cisco IPSec) or any other name containing spaces, those variables need to be quoted using the quoted form of syntax.  Otherwise things seem to get a little cranky. Or I suppose you could also name a vpn service explicitly without using any variables for those parts at all. Aside from that, the only other change I made was to remove the paragraph 1 of  part of the script. The script seems to run fine with or without it.

Here is the slightly modified version I'm currently using:

Applescript:

set theVPN to "VPN (Cisco IPSec)"

set amIconnected to (do shell script "scutil --nc show " & quoted form of theVPN)
if amIconnected contains "(Connected)" then
   set theAction to "stop"
else if amIconnected contains "(Disconnected)" then
   set theAction to "start"
end if

do shell script "scutil --nc " & theAction & space & quoted form of theVPN

Thank you once again for this solution.

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)