Skip to content

Instantly share code, notes, and snippets.

@labocho
Last active October 18, 2017 08:20
Show Gist options
  • Select an option

  • Save labocho/968afeb5d8ac3f1dc9c9994b2090c024 to your computer and use it in GitHub Desktop.

Select an option

Save labocho/968afeb5d8ac3f1dc9c9994b2090c024 to your computer and use it in GitHub Desktop.
Ruby script to connect/disconnect VPN with service and (optional) configuration name
#!/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