|
| 1 | +#! /bin/bash |
| 2 | + |
| 3 | +function _lazy_connect_init() { |
| 4 | + config_dir=~/.config/lazy-connect |
| 5 | + echo -n "Secret Key: " |
| 6 | + read -s secret_key |
| 7 | + echo "**********" |
| 8 | + mkdir -p $config_dir |
| 9 | + echo $secret_key > $config_dir/secret |
| 10 | + |
| 11 | + vpnNames=$(osascript <<EOF |
| 12 | + tell application "System Events" |
| 13 | + tell process "SystemUIServer" |
| 14 | + set vpnMenu to (menu bar item 1 of menu bar 1 where description is "VPN") |
| 15 | + tell vpnMenu to click |
| 16 | + set vpnMenuItems to (menu items of menu 1 of vpnMenu) |
| 17 | + -- Loop till first missing value(that is fisrt menu item seperator) and accumulate VPN Names |
| 18 | + set vpnNames to {} |
| 19 | + repeat with vpnMenuItem in vpnMenuItems |
| 20 | + set vpnName to name of vpnMenuItem |
| 21 | + if vpnName is equal to missing value then |
| 22 | + exit repeat |
| 23 | + end if |
| 24 | + set vpnNames to vpnNames & {vpnName} |
| 25 | + end repeat |
| 26 | + key code 53 |
| 27 | + get vpnNames |
| 28 | + end tell |
| 29 | + end tell |
| 30 | +EOF |
| 31 | +) |
| 32 | + echo $vpnNames | sed -e "s/Connect //g; s/Disconnect //g;" | tr , "\n" | xargs -I{} echo {} > $config_dir/vpns |
| 33 | + echo "VPN List:" |
| 34 | + cat $config_dir/vpns | nl |
| 35 | +} |
| 36 | + |
| 37 | +function _lazy_connect() { |
| 38 | + vpn_name=$1 |
| 39 | + secret_key=$2 |
| 40 | + password=$(oathtool --totp --base32 $secret_key) |
| 41 | + |
| 42 | + osascript <<EOF |
| 43 | + on connectVpn(vpnName, password) |
| 44 | + tell application "System Events" |
| 45 | + tell process "SystemUIServer" |
| 46 | + set vpnMenu to (menu bar item 1 of menu bar 1 where description is "VPN") |
| 47 | + tell vpnMenu to click |
| 48 | + try |
| 49 | + click menu item ("Connect " & vpnName) of menu 1 of vpnMenu |
| 50 | + delay 1 |
| 51 | + keystroke password |
| 52 | + keystroke return |
| 53 | + on error errorStr |
| 54 | + if errorStr does not contain "Can’t get menu item" and errorStr does not contain vpnName then |
| 55 | + display dialog errorStr |
| 56 | + end if |
| 57 | + end try |
| 58 | + end tell |
| 59 | + end tell |
| 60 | + end connectVpn |
| 61 | +
|
| 62 | + connectVpn("$vpn_name", "$password") |
| 63 | +EOF |
| 64 | +} |
| 65 | + |
| 66 | +function lazy-connect() { |
| 67 | + while getopts "iah" opt; do |
| 68 | + case $opt in |
| 69 | + h) |
| 70 | + echo "Usage" |
| 71 | + ;; |
| 72 | + i) |
| 73 | + _lazy_connect_init |
| 74 | + ;; |
| 75 | + a) |
| 76 | + echo "Connect all" |
| 77 | + ;; |
| 78 | + \?) |
| 79 | + echo "Invalid Option: -$OPTARG." |
| 80 | + echo "Usage" |
| 81 | + return 1 |
| 82 | + ;; |
| 83 | + :) |
| 84 | + echo "Option -$OPTARG requires an argument." |
| 85 | + echo "Usage" |
| 86 | + return 1 |
| 87 | + ;; |
| 88 | + esac |
| 89 | + done |
| 90 | +} |
0 commit comments