Skip to content

Instantly share code, notes, and snippets.

@oguzhancoskun
Created November 4, 2019 12:07
Show Gist options
  • Save oguzhancoskun/ea8d009a6e44df5256099d49ca6353da to your computer and use it in GitHub Desktop.
Save oguzhancoskun/ea8d009a6e44df5256099d49ca6353da to your computer and use it in GitHub Desktop.
usage: viscocity.sh connect [vpnname]
#!/usr/bin/env bash
#Oguzhan Coskun <[email protected]>
display_usage() {
echo "connect <connection name> Open the given connection"
echo "connect all Open all the connections"
echo "disconnect <connection name> Close the given connection"
echo "disconnect all Close all the connections"
echo "ls List all the configured connections"
echo "connections Display ongoing connections"
exit 2
}
while [[ $# > 0 ]]; do
key="$1"
case $key in
-q|--quiet)
QUIET="yes"
;;
-h|--help)
display_usage
QUIET="yes"
;;
*)
REMAINS="$REMAINS \"$1\""
;;
esac
shift
done
eval set -- $REMAINS
cmd="$1"
arg="$2"
case $cmd in
connections)
if [ -z "$QUIET" ]; then
echo "Connection name"
echo "---------------"
fi
osascript -e 'tell application "Viscosity"
set connames to name of connections where state is equal to "Connected"
return connames
end tell
' | tr , '\n' | sed 's/^ *//;s/ *$//' | sort
;;
ls|list)
if [ -z "$QUIET" ]; then
echo "Connection name Status"
echo "--------------------------------------------"
fi
osascript -e 'tell application "Viscosity"
set result to {}
repeat with con in connections
set result to result & ((get name of con) & "|" & (get state of con))
end repeat
return result
end tell
' | tr , '\n' | sed 's/^ *//;s/ *$//' | column -t -s \\"|\\" | sort
;;
connect|connect-all|start|start-all)
if [ "$arg" == "all" ]; then
osascript -e 'tell application "Viscosity" to connectall'
elif [ "$arg" == "" ]; then
echo "$0: missing argument connection name"
exit 1
else
item_uuid=$(cat ~/.op/saml_auth_config_uuid)
domain=$(cat ~/.op/saml_auth_config_domain)
email=$(cat ~/.op/saml_auth_config_email)
secret=$(cat ~/.op/saml_auth_config_secret)
op_session_key=$(op signin ${domain} ${email} ${secret} --output=raw)
SAML_TOTP=$(echo ${op_session_key} | op get totp $item_uuid)
echo "${SAML_TOTP}" |pbcopy
osascript -e "tell application \"Viscosity\" to connect \"$arg\""
fi
if [ -z "$QUIET" ]; then
echo "Connecting $arg"
fi
;;
disconnect|connect-all|stop|stop-all)
if [ "$arg" == "all" ]; then
osascript -e 'tell application "Viscosity" to disconnectall'
elif [ "$arg" == "" ]; then
echo "$0: missing argument connection name"
exit 1
else
osascript -e "tell application \"Viscosity\" to disconnect \"$arg\""
fi
if [ -z "$QUIET" ]; then
echo "Disconnecting $arg"
fi
;;
*)
display_usage
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment