Mutahir
Mutahir

Reputation: 219

AppleScript | Deploy PPTP VPN Settings to MAC Clients

Need an applescript (which I haven't used before) to deploy our VPN connection settings on our remote workers mac boxes. this is for PPTP VPN.

Upon googling I came across the following link :
http://discussions.apple.com/thread.jspa?messageID=10368307

But still I can't :
a) seem to name the VPN connection
b) give it a pptp vpn server address
c) give dns, default gateway and any custom routes for this vpn to work
.

How can I add those fields / enteries in ?

tell application “System Preferences”
reveal pane “Network”
activate
tell application “System Events”
tell process “System Preferences”
tell window 1
click button “Add Service”
tell sheet 1
click pop up button 1
click menu item “VPN” of menu 1 of pop up button 1
delay 1
click pop up button 2
click menu item “PPTP” of menu 1 of pop up button 2
click button “Create”
end tell
click button “Apply”
end tell
end tell
delay 1 — optional (just for visual feedback)
keystroke “w” using {command down}
end tell
end tell

The above script seems to create the connection just fine but without the details - and I am not sure about what syntax I would use to get this going.

Thanks for your input!
Regards
Thanks

Upvotes: 1

Views: 8055

Answers (3)

Andrew Odri
Andrew Odri

Reputation: 9422

Ancient answer

I tried the solution using the networksetup tool, but it didn't work for me :S I actually came up with a successful script using UI automation. It put up a post about it here: https://web.archive.org/web/20160731014105/http://blog.affirmix.com/2011/01/12/how-to-configure-a-vpn-in-mac-os-x-usingapplescript/

Copy/pasted answer

Disclaimer: This is now 12+ years old, and was writen for a Mac OS X version named after a cat. Milage may vary, use at your own risk, it probably won't work without modifications, yada yada... Hopefully it provides a foundation for a better answer :)

  1. Open Finder, navigate to Applications, then Utilities, and open AppleScript Editor.
  2. Select the code at the end of this article, and press ⌘ (Command) + C. This copies the code into the clipboard.
  3. Return to AppleScript Editor, and press ⌘ (Command) + V. This pastes the code from the clipboard into the editor.
  4. The first line of code defines the server name that will be defined on the local computer. Replace Server Name with the name of the network service that you would like to appear on the client computer.
  5. The second line of code defines the server address. Replace 127.0.0.1 with the URL or IP address of the VPN server.
  6. The third line defines the shared secret for the VPN. Replace s3cr3t with the shared secret of you VPN.
  7. Go to line 32, and remove the two dashes before “click pop up button 2”
    • For an L2TP over IPSec connection: Go to line 33 and remove the two dashes before ‘click menu item “L2TP over IPSEC” of menu 1 of pop up button 2’
    • For a PPTP connection: Go to line 34 and remove the two dashes before ‘click menu item “PPTP” of menu 1 of pop up button 2’
    • For a Cisco IPSec connection: Go to line 35 and remove the two dashes before ‘click menu item “Cisco IPSec” of menu 1 of pop up button 2’ [See Sergio’s comment below for information on configuring Cisco IPSec VPNs]
  8. Go to Script menu, and select the Run menu item
set vpnname to "Server Name"
set vpnserver to "127.0.0.1"
set vpnsecret to "s3cr3t"

tell application "System Events"
    if not (UI elements enabled) then
        tell application "System Preferences"
            activate
            set current pane to pane id "com.apple.preference.universalaccess"
            display dialog "This script requires access for assistive devices be enabled." & return & return & "To continue, click the OK button and enter an administrative password in the security dialog." with icon note
        end tell
        set UI elements enabled to true
        if UI elements enabled is false then return "User Cancelled"
        delay 1
    end if
    tell application "System Preferences"
        activate
        set current pane to pane id "com.apple.preference.network"
        display dialog "This script will now create the Stracor VPN connection." & return & return & "Please wait as the operation is in progress, and do not click anything. This should not take longer than 30 seconds. You will be notified when the process is complete." with icon caution
        set vpnusername to display dialog "Please enter your username:" default answer "" with icon note
        set vpnpassword to display dialog "Please enter your password:" default answer "" with icon note with hidden answer
    end tell
    tell application "System Events"
        tell process "System Preferences"
            tell window 1
                click button 5
                delay 1
            end tell
            tell sheet 1 of window 1
                click pop up button 1
                click menu item "VPN" of menu 1 of pop up button 1
                --click pop up button 2
                --click menu item "L2TP over IPSEC" of menu 1 of pop up button 2
                --click menu item "PPTP" of menu 1 of pop up button 2
                --click menu item "Cisco IPSec" of menu 1 of pop up button 2
                set focused of text field 1 to true
                keystroke "a" using command down
                keystroke vpnname
                click button 1
                delay 1
            end tell
            tell group 1 of window 1
                click checkbox 1
                set focused of text field 1 to true
                keystroke vpnserver
                keystroke tab
                keystroke text returned of vpnusername
                click button 2
                delay 1
            end tell
            tell sheet 1 of window 1
                set focused of text field 3 to true
                keystroke text returned of vpnpassword
                keystroke tab
                keystroke vpnsecret
                click button 2
                delay 1
            end tell
            tell window 1
                click button 1
                delay 1
            end tell
            tell sheet 1 of window 1
                click button 1
            end tell
        end tell
    end tell
    tell application "System Preferences"
        quit saving yes
    end tell
    display alert "The VPN has been configured. Click on the \"Connect\" button to access the network." & return & return & "The connection status and various options are available through the menu bar at the top of the screen." as informational
end tell

If you are setting up a Cisco IPSec VPN, then here is "Sergio's comment" for reference:

Andrew,

I also was trying to configure a Cisco IPSec VPN connection and these are the tweaks I made to get it to work. (10.6.6/10.6.7) I had to add the variable “vpngroup” to add the group name we use in our configuration. Hope this helps.

tell sheet 1 of window 1
    click pop up button 1
    click menu item “vpn” of menu 1 of pop up button 1
    delay 1
    repeat until exists pop up button 2
      delay 0.2
    end repeat
    click pop up button 2
    delay 0.5
    --click menu item “l2tp over ipsec” of menu 1 of pop up button 2
    --click menu item “pptp” of menu 1 of pop up button 2
    click menu item “cisco ipsec” of menu 1 of pop up button 2
    delay 1
    set focused of text field 1 to true
    keystroke “a” using command down
    keystroke vpnname
    click button 1
    delay 1
end tell

delay 1

tell group 1 of window 1
    set focused of text field 3 to true
    keystroke vpnserver
    keystroke tab
    keystroke text returned of vpnusername
    keystroke tab
    keystroke text returned of vpnpassword
    click checkbox 1
    click button 2
    delay 1
end tell

tell sheet 1 of window 1
    delay 0.2
    set focused of text field 1 to true
    keystroke vpngroup
    keystroke tab
    keystroke vpnsecret
    click button 2
    delay 1
end tell

Upvotes: 3

user225057
user225057

Reputation:

Rihatum, here's the full Applescript with all the things you want to do.

--create new VPN PPTP service on the ethernet interface
do shell script "networksetup -createnetworkservice \"VPN (PPTP)\" en0 - where en0" with administrator privileges

--set the IP, subnet, & router IP ( order = ip subnet route )
do shell script "networksetup -setmanual \"VPN (PPTP)\" 192.168.2.50 255.255.255.0 192.168.2.1" with administrator privileges

--set VPN service using DHCP  -- if using DHCP don't use manual setting above
do shell script "networksetup -setdhcp \"VPN (PPTP)\"" with administrator privileges

--set DNS
do shell script "networksetup -setdnsservers \"VPN (PPTP)\" 208.67.222.222" with administrator privileges  

--set search domain 
do shell script "networksetup -setsearchdomains \"VPN (PPTP)\" my_company_domain.com" with administrator privileges

--rename network service -- replace CISCO_VPN with your preferred name
do shell script "networksetup -renamenetworkservice \"VPN (PPTP)\" CISCO_VPN" with administrator privileges

Upvotes: 3

user225057
user225057

Reputation:

I think what you want to do is build a script using the networksetup command and use a series of "do shell script" lines in your AppleScript instead of trying to GUI script the configuration.

Fo example, in your AppleScript this command will create a new VPN PPTP connection on the ethernet interface:

do shell script "networksetup -createnetworkservice \"VPN (PPTP)\" en0 -where en0" with administrator privileges

To set the IP, subnet, & router IPs you would use this: ( -setmanual networkservice ip subnet router )

do shell script "networksetup -setmanual \"VPN (PPTP)\" 192.168.2.50 255.255.255.0 192.168.2.1" with administrator privileges

After that you can use the other options to configure the rest. For example, to add a DNS IP to your newly created VPN PPTP connection you would add this to your AppleScript:

do shell script "networksetup -setdnsservers \"VPN (PPTP)\" 208.67.222.222" with administrator privileges

Check out the man page for the networksetup command. It does a tremendous amount of things. Also, you can Google that command and you will find a lot of forums with people discussing Mac command line network configurations. The single keyword networksetup doesn't work too well because it returns a lot of Windows pages so Google for "sudo networksetup" and that will return Mac related pages.

Upvotes: 1

Related Questions