Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Disconnect a connected VPN #5

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 36 additions & 2 deletions lazy-connect.sh
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ lazy-connect - Shell function to fuzzy search an IPSec VPN by name
-u - Update lazy-connect
-r - Refresh vpn list in ~/.config/lazy-connect
-h - Show this help
-d - Disconnect a connected VPN
EOF
}

Expand Down Expand Up @@ -105,7 +106,7 @@ function _lazy_connect() {
esac
fi

osascript <<EOF
result=$(osascript <<EOF
on connectVpn(vpnName, password)
tell application "System Events"
tell process "SystemUIServer"
Expand All @@ -116,6 +117,7 @@ function _lazy_connect() {
delay 1
keystroke password
keystroke return
return "true"
on error errorStr
if errorStr does not contain "Can’t get menu item" and errorStr does not contain vpnName then
display dialog errorStr
Expand All @@ -127,6 +129,33 @@ function _lazy_connect() {

connectVpn("$vpn_name", "$password")
EOF
)
[[ $result -eq "true" ]] && echo $vpn_name | sed 's/Connect/Disconnect/g' >> "$_lazy_connect_config_dir/connected_vpns"
}

function _lazy_disconnect() {
vpn_name=$1

osascript <<EOF
on disconnectVpn(vpnName)
tell application "System Events"
tell process "SystemUIServer"
set vpnMenu to (menu bar item 1 of menu bar 1 where description is "VPN")
tell vpnMenu to click
try
click menu item vpnName of menu 1 of vpnMenu
on error errorStr
if errorStr does not contain "Can’t get menu item" and errorStr does not contain vpnName then
display dialog errorStr
end if
end try
end tell
end tell
end disconnectVpn

disconnectVpn("$vpn_name")
EOF
sed "/Disconnect ${vpn_name}/d" $_lazy_connect_config_dir/connected_vpns | sort -u > $_lazy_connect_config_dir/connected_vpns
}

function _lazy_connect_update() {
Expand All @@ -139,7 +168,7 @@ function lazy-connect() {
local OPTIND
mkdir -p $_lazy_connect_config_dir

while getopts "iruh" opt; do
while getopts "iruhd" opt; do
case $opt in
h)
_lazy_connect_usage
Expand All @@ -154,6 +183,11 @@ function lazy-connect() {
_lazy_connect_vpn_refresh
return 0
;;
d)
vpn_name=$(cat $_lazy_connect_config_dir/connected_vpns | fzf --height=10 --ansi --reverse)
_lazy_disconnect "$vpn_name"
return 0
;;
u)
_lazy_connect_update
return 0
Expand Down