@@ -63,6 +63,7 @@ lazy-connect - Shell function to fuzzy search an IPSec VPN by name
63
63
-u - Update lazy-connect
64
64
-r - Refresh vpn list in ~/.config/lazy-connect
65
65
-h - Show this help
66
+ -d - Disconnect a connected VPN
66
67
EOF
67
68
}
68
69
@@ -105,7 +106,7 @@ function _lazy_connect() {
105
106
esac
106
107
fi
107
108
108
- osascript << EOF
109
+ result= $( osascript << EOF
109
110
on connectVpn(vpnName, password)
110
111
tell application "System Events"
111
112
tell process "SystemUIServer"
@@ -115,7 +116,7 @@ function _lazy_connect() {
115
116
click menu item vpnName of menu 1 of vpnMenu
116
117
delay 1
117
118
keystroke password
118
- keystroke return
119
+ return "true"
119
120
on error errorStr
120
121
if errorStr does not contain "Can’t get menu item" and errorStr does not contain vpnName then
121
122
display dialog errorStr
@@ -127,6 +128,34 @@ function _lazy_connect() {
127
128
128
129
connectVpn("$vpn_name ", "$password ")
129
130
EOF
131
+ )
132
+ [[ $result -eq " true" ]] && echo $vpn_name | sed ' s/Connect/Disconnect/g' >> " $_lazy_connect_config_dir /connected_vpns"
133
+ }
134
+
135
+ function _lazy_disconnect() {
136
+ vpn_name=$1
137
+
138
+ osascript << EOF
139
+ on disconnectVpn(vpnName)
140
+ tell application "System Events"
141
+ tell process "SystemUIServer"
142
+ set vpnMenu to (menu bar item 1 of menu bar 1 where description is "VPN")
143
+ tell vpnMenu to click
144
+ try
145
+ click menu item vpnName of menu 1 of vpnMenu
146
+ on error errorStr
147
+ if errorStr does not contain "Can’t get menu item" and errorStr does not contain vpnName then
148
+ display dialog errorStr
149
+ end if
150
+ end try
151
+ end tell
152
+ end tell
153
+ end disconnectVpn
154
+
155
+ disconnectVpn("$vpn_name ")
156
+ sed "/Connect ${vpn_name} /d" $_lazy_connect_config_dir /vpns
157
+ sed "/Disconnect ${vpn_name} /d" $_lazy_connect_config_dir /connected_vpns | sort -u
158
+ EOF
130
159
}
131
160
132
161
function _lazy_connect_update() {
@@ -139,7 +168,7 @@ function lazy-connect() {
139
168
local OPTIND
140
169
mkdir -p $_lazy_connect_config_dir
141
170
142
- while getopts " iruh " opt; do
171
+ while getopts " iruhd " opt; do
143
172
case $opt in
144
173
h)
145
174
_lazy_connect_usage
@@ -154,6 +183,11 @@ function lazy-connect() {
154
183
_lazy_connect_vpn_refresh
155
184
return 0
156
185
;;
186
+ d)
187
+ vpn_name=$( cat $_lazy_connect_config_dir /connected_vpns | fzf --height=10 --ansi --reverse)
188
+ _lazy_disconnect " $vpn_name "
189
+ return 0
190
+ ;;
157
191
u)
158
192
_lazy_connect_update
159
193
return 0
0 commit comments