Skip to content

Commit 4ce44a4

Browse files
author
Dinesh Kumar
committed
Disconnect a vpn
1 parent 42b09e3 commit 4ce44a4

File tree

1 file changed

+37
-2
lines changed

1 file changed

+37
-2
lines changed

lazy-connect.sh

+37-2
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ lazy-connect - Shell function to fuzzy search an IPSec VPN by name
6363
-u - Update lazy-connect
6464
-r - Refresh vpn list in ~/.config/lazy-connect
6565
-h - Show this help
66+
-d - Disconnect a connected VPN
6667
EOF
6768
}
6869

@@ -105,7 +106,7 @@ function _lazy_connect() {
105106
esac
106107
fi
107108

108-
osascript <<EOF
109+
result=$(osascript <<EOF
109110
on connectVpn(vpnName, password)
110111
tell application "System Events"
111112
tell process "SystemUIServer"
@@ -116,6 +117,7 @@ function _lazy_connect() {
116117
delay 1
117118
keystroke password
118119
keystroke return
120+
return "true"
119121
on error errorStr
120122
if errorStr does not contain "Can’t get menu item" and errorStr does not contain vpnName then
121123
display dialog errorStr
@@ -127,6 +129,34 @@ function _lazy_connect() {
127129
128130
connectVpn("$vpn_name", "$password")
129131
EOF
132+
)
133+
[[ $result -eq "true" ]] && echo $vpn_name | sed 's/Connect/Disconnect/g' >> "$_lazy_connect_config_dir/connected_vpns"
134+
}
135+
136+
function _lazy_disconnect() {
137+
vpn_name=$1
138+
139+
osascript <<EOF
140+
on disconnectVpn(vpnName)
141+
tell application "System Events"
142+
tell process "SystemUIServer"
143+
set vpnMenu to (menu bar item 1 of menu bar 1 where description is "VPN")
144+
tell vpnMenu to click
145+
try
146+
click menu item vpnName of menu 1 of vpnMenu
147+
on error errorStr
148+
if errorStr does not contain "Can’t get menu item" and errorStr does not contain vpnName then
149+
display dialog errorStr
150+
end if
151+
end try
152+
end tell
153+
end tell
154+
end disconnectVpn
155+
156+
disconnectVpn("$vpn_name")
157+
sed "/Connect ${vpn_name}/d" $_lazy_connect_config_dir/vpns
158+
sed "/Disconnect ${vpn_name}/d" $_lazy_connect_config_dir/connected_vpns | sort -u
159+
EOF
130160
}
131161

132162
function _lazy_connect_update() {
@@ -139,7 +169,7 @@ function lazy-connect() {
139169
local OPTIND
140170
mkdir -p $_lazy_connect_config_dir
141171

142-
while getopts "iruh" opt; do
172+
while getopts "iruhd" opt; do
143173
case $opt in
144174
h)
145175
_lazy_connect_usage
@@ -154,6 +184,11 @@ function lazy-connect() {
154184
_lazy_connect_vpn_refresh
155185
return 0
156186
;;
187+
d)
188+
vpn_name=$(cat $_lazy_connect_config_dir/connected_vpns | fzf --height=10 --ansi --reverse)
189+
_lazy_disconnect "$vpn_name"
190+
return 0
191+
;;
157192
u)
158193
_lazy_connect_update
159194
return 0

0 commit comments

Comments
 (0)