diff --git a/click_vpn.scpt b/click_vpn.scpt new file mode 100644 index 0000000..aa3f8f0 --- /dev/null +++ b/click_vpn.scpt @@ -0,0 +1,27 @@ +#!/usr/bin/env osascript + +on click(name) + log "selection: " & {name} + 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 name of menu 1 of vpnMenu + on error errorStr + return "false" + log "error: " & errorStr + end try + end tell + end tell + return "true" +end click + +on run argv + set arg to (item 1 of argv) + set res to click(arg) + if res equal to "false" then + -- ESCAPE KEY + tell application "System Events" to key code 53 + end if +end run diff --git a/connected_vpns.scpt b/connected_vpns.scpt new file mode 100644 index 0000000..c14fcc4 --- /dev/null +++ b/connected_vpns.scpt @@ -0,0 +1,33 @@ +#!/usr/bin/env osascript + +-- could replace refresh with this, passing regex +on getConnectedVpns() + log "search for connected vpns" + 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 + set vpnMenuItems to (menu items of menu 1 of vpnMenu) + set connectedVpns to {} + repeat with vpnMenuItem in vpnMenuItems + set vpnName to name of vpnMenuItem + if vpnName starts with "Disconnect" then + set connectedVpns to connectedVpns & {vpnName} + else if vpnName starts with "Connect" then + exit repeat + end if + end repeat + get connectedVpns + end tell + end tell +end getConnectedVpns + +on run argv + set res to getConnectedVpns() + if res equal to {} then + log "No vpn is connected." + -- ESCAPE + end if + tell application "System Events" to key code 53 + return res +end run diff --git a/lazy-connect.sh b/lazy-connect.sh index e1d8531..82fccff 100755 --- a/lazy-connect.sh +++ b/lazy-connect.sh @@ -88,6 +88,13 @@ function _lazy_connect_get_totp() { esac } +function _lazy_disconnect_all(){ + echo "Disconnecting..." + connected_vpns=$(osascript ${_lazy_connect_project_dir}/connected_vpns.scpt) + vpns=$(echo $connected_vpns | tr ',' '\n' | sed 's/^[[:space:]]//g') + echo -n $vpns | xargs -L 1 -I {} /bin/bash -c "osascript ${_lazy_connect_project_dir}/click_vpn.scpt '{}'" +} + function _lazy_connect() { vpn_name=$1 _lazy_connect_get_totp $2 @@ -105,8 +112,10 @@ function _lazy_connect() { esac fi + [[ "$MANUAL_MODE" -eq "true" ]] && echo -n "$password" | pbcopy + osascript <