forked from mariuskueng/turbo-octo-connect
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrVPNMeNow.rb
70 lines (55 loc) · 2.06 KB
/
rVPNMeNow.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# @author Marius Küng
# @version 0.2 (2013-11-22)
# Resources
# http://www.weichel21.de/wordpress/2013/03/31/open-a-vpn-verbindung-on-mac-using-ruby/
# http://forums.macrumors.com/showthread.php?t=689066
require 'json'
class VPNMeNow
action = ARGV[0]
action ||= ''
settingsFilePath = ARGV[1]
settingsFilePath ||= File.join(File.dirname(__FILE__),'settings.json')
if !File.exists?(settingsFilePath) then
puts settingsFilePath + " does not exist."
exit
end
@settings = JSON.parse(File.read(settingsFilePath))
def self.open()
close() # close open connection
cmd=[]
cmd << "osascript <<-EOF"
cmd << "tell application \"System Events\""
cmd << " tell current location of network preferences"
cmd << " set VPN to service \"#{@settings["SERVICE"]}\"" #% $settings["SERVICE"]
cmd << " if exists VPN then connect VPN"
# cmd << " repeat while (current configuration of VPN is not connected)"
cmd << " key code 48 using {command down}"
cmd << " delay 1"
cmd << " keystroke \"#{@settings["PASSWORD"]}\"" #% $settings["PASSWORD"]
cmd << " key code 36"
# cmd << " end repeat"
cmd << " end tell"
cmd << "end tell"
cmd << "EOF"
cmd=cmd.join("\n")
system(cmd)
end
def self.close
cmd=[]
cmd << "osascript <<-EOF"
cmd << "tell application \"System Events\""
cmd << " tell current location of network preferences"
cmd << " set VPN to service \"#{@settings["SERVICE"]}\" -- your VPN name here"
cmd << " if exists VPN then disconnect VPN"
cmd << " end tell"
cmd << "end tell"
cmd << "EOF"
cmd=cmd.join("\n")
system(cmd)
end
if action == "open"
self.open()
elsif action == "close"
self.close()
end
end