Skip to content

Instantly share code, notes, and snippets.

@ryanhanwu
Created January 16, 2020 18:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ryanhanwu/1541611a9f656af5568ed3b519996183 to your computer and use it in GitHub Desktop.
Save ryanhanwu/1541611a9f656af5568ed3b519996183 to your computer and use it in GitHub Desktop.
Apple script for automatically create VPN
on create_vpn_service(vpn_name)
tell application "System Preferences"
reveal pane "com.apple.preference.network"
activate
tell application "System Events"
tell process "System Preferences"
tell window 1
click button 1 -- "Add Service"
tell sheet 1
-- set location type
click pop up button 1
click menu item "VPN" of menu 1 of pop up button 1
delay 1
-- set connection type
click pop up button 2
click menu item "L2TP over IPSec" of menu 1 of pop up button 2
delay 1
-- set name of the service
set value of attribute "AXValue" of text field 1 to vpn_name
click button 2 -- "Create"
end tell
end tell
end tell
end tell
end tell
end create_vpn_service
on update_vpn_settings(vpn_name, vpn_address, vpn_username, vpn_password, vpn_key)
tell application "System Preferences"
reveal pane "com.apple.preference.network"
activate
tell application "System Events"
tell process "System Preferences"
tell window 1
-- select the specified row in the service list
repeat with r in rows of table 1 of scroll area 1
if (value of attribute "AXValue" of static text 1 of r as string) contains vpn_name then
select r
end if
end repeat
-- set the address & username / account name
-- note that this is vpn specific
tell group 1
set value of text field 1 to vpn_address
set value of text field 2 to vpn_username
click button 1 -- "Authentication Settings…"
end tell
-- open up the auth panel and set the login password
tell sheet 1
set value of text field 1 to vpn_password
set value of text field 2 to vpn_key
click button 4 -- "Ok"
end tell
click button 7 -- "Apply"
tell sheet 1
click button 1
end tell
end tell
end tell
end tell
end tell
end update_vpn_settings
create_vpn_service("My VPN")
update_vpn_settings("MY VPN", "URL", "NAME", "PWD", "KEY")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment