Skip to content

Instantly share code, notes, and snippets.

@alyssais
Created May 8, 2018 17:12
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save alyssais/fea9ff6bcf7955b7f7a5e64656d611aa to your computer and use it in GitHub Desktop.
Save alyssais/fea9ff6bcf7955b7f7a5e64656d611aa to your computer and use it in GitHub Desktop.
A tiny command line interface to Viscosity
#!/usr/bin/env bash
run_list() {
osascript <<OSA
tell application "Viscosity"
repeat with theConnection in connections
set theName to name of theConnection
set theState to state of theConnection
log theName & ": " & theState
end repeat
end tell
OSA
}
run_connect() {
if [ "$#" -eq 0 ]; then
osascript -e 'tell application "Viscosity" to connectall'
else
while connection="$1"; shift; do
osascript -e "tell application \"Viscosity\" to connect \"$connection\""
done
fi
}
run_disconnect() {
if [ "$#" -eq 0 ]; then
osascript -e 'tell application "Viscosity" to disconnectall'
else
while connection="$1"; shift; do
osascript -e "tell application \"Viscosity\" to disconnect \"$connection\""
done
fi
}
run_help() {
echo "Usage:"
echo " $(basename "$0") [list] # List available VPNs"
echo " $(basename "$0") connect [VPN [...]] # Connect to specified VPNs (or all)"
echo " $(basename "$0") disconnect [VPN [...]] # Disconnect from specified VPNs (or all)"
}
run_version() {
echo "vpn 1.0.0"
}
main() {
local command="$1"
shift
case "$command" in
list | "" ) run_list ;;
connect ) run_connect ;;
disconnect ) run_disconnect ;;
-h | --help ) run_help ;;
-v | --version ) run_version ;;
*)
echo "$(basename "$0"): unknown command: $command" 1>&2
exit 1
;;
esac
}
main $@
@beauraines
Copy link

This is awesome - thank you!

I had to make one tiny tweak on GNU bash, version 5.1.16(1)-release (x86_64-apple-darwin21.1.0)

It was trying to start ALL of viscosity's connections, so I added $@ to the connect/disconnect commands.

  connect ) run_connect $@;;
  disconnect ) run_disconnect $@;;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment