-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvpn.fish
98 lines (84 loc) · 3.17 KB
/
vpn.fish
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
function vpn -a cmd name_or_index -d 'Handles existing macOS VPN (PPTP and L2TP) services'
set -l service_name
set -l LIST 'list'
set -l START 'start'
set -l STOP 'stop'
set -l SWITCH 'switch'
# set -l STATUS 'status'
set -l available_commands $LIST $START $STOP $SWITCH # $STATUS
set -l types_regex 'PPTP|L2TP'
set -l quoted_regex '\".*\"'
set -l CONNECTED 'Connected'
set -l DISCONNECTED 'Disconnected'
set -l brackets_regex "\($CONNECTED|$DISCONNECTED\)"
set -l prompt 'set_color blue; echo -n "Specify index or service name: "; set_color normal;'
if [ -z "$cmd" ]
set cmd $LIST
else if not contains $cmd $available_commands
set_color red
echo "$_: invalid command, use $available_commands instead"
set_color normal
return 1
end
# get list of VPN names and statuses (DRY is crying, but it's better just like that)
set -l names (scutil --nc list | grep -E $types_regex | grep -o -E $quoted_regex | sed -e "s/\"//g")
set -l statuses (scutil --nc list | grep -E $types_regex | grep -o -E $brackets_regex | sed -e "s/[\(\)]//g")
if [ -z "$name_or_index" -o $cmd = $LIST ]
# show VPN list
for i in (seq (count $names))
echo -s $i '. ' $names[$i] ' - ' $statuses[$i]
end
if [ $cmd = $LIST ]
return 0
end
# ask for input if not just list
read -p $prompt name_or_index
end
if [ $name_or_index -eq $name_or_index ] # small trick to check if it is a number (int only)
if [ $name_or_index -gt (count $names) -a $name_or_index -le 0 ]
set_color red
echo '$_: invalid service index'
set_color normal
return 1
end
set service_name $names[$name_or_index]
else
if not contains $name_or_index $names
set_color red
echo '$_: invalid service name'
set_color normal
return 1
end
set service_name $name_or_index
end
# # `list` command is informative enough to have separate `status` command
# if [ $cmd = $STATUS ]
# echo "$service_name is" (scutil --nc status "$service_name" | head -n 1 | tr "[:upper:]" "[:lower:]")
# return 0
# end
while true
set -l service_status (scutil --nc status "$service_name" | head -n 1)
set_color green
if [ $service_status = $CONNECTED ]
if [ $cmd = $START ]
echo "$service_name: $CONNECTED"
break
end
if [ $cmd = $SWITCH ]
set cmd $STOP
end
else if [ $service_status = $DISCONNECTED ]
if [ $cmd = $STOP ]
echo "$service_name: $DISCONNECTED"
break
end
if [ $cmd = $SWITCH ]
set cmd $START
end
end
set_color normal
sleep 0.2
# it's ok to have it inside loop, because sometimes better to kick it twice or even more
scutil --nc $cmd $service_name # `stop` command not really working in Yosemite (see https://discussions.apple.com/message/27696050)
end
end