Last active
October 18, 2017 08:20
-
-
Save labocho/968afeb5d8ac3f1dc9c9994b2090c024 to your computer and use it in GitHub Desktop.
Ruby script to connect/disconnect VPN with service and (optional) configuration name
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env ruby | |
| # Thanks to: https://gist.github.com/kawaz/8bd7db8416f90c3cc168f12c8c75b6d3 | |
| # Usage: vpn (connect|disconnect) SERVICE_NAME [CONFIGURATION_NAME] | |
| require "json" | |
| SCRIPT = <<-JS | |
| function run(json) { | |
| var argv = JSON.parse(json); | |
| var sysev = Application("System Events"); | |
| var action = argv.action; | |
| var service = sysev.networkPreferences.services[argv.service]; | |
| if (argv.config) { | |
| var config = service.configurations[argv.config] | |
| sysev[action](config); | |
| } else { | |
| sysev[action](service); | |
| } | |
| } | |
| JS | |
| action, service, config, * = ARGV | |
| case action | |
| when "connect", "disconnect" | |
| else | |
| raise "Unknown action: #{action}" | |
| end | |
| unless service | |
| raise "Service required" | |
| end | |
| argv = { | |
| action: action, | |
| service: service, | |
| config: config, | |
| } | |
| exec "/usr/bin/osascript", "-l", "JavaScript", "-e", SCRIPT, argv.to_json |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment