Skip to content

Instantly share code, notes, and snippets.

@jhildenbiddle
Last active February 8, 2021 17:50
Show Gist options
  • Save jhildenbiddle/f910555cc32114e7b4c30f862a00ec60 to your computer and use it in GitHub Desktop.
Save jhildenbiddle/f910555cc32114e7b4c30f862a00ec60 to your computer and use it in GitHub Desktop.
Toggle Private Internet Access VPN
#!/bin/bash
# Toggle Private Internet Access VPN
# https://gist.github.com/jhildenbiddle/f910555cc32114e7b4c30f862a00ec60
#
# Requires installation of PIA app
# https://www.privateinternetaccess.com/pages/download
# Set path to piactl (use `which piactl` to determine)
piactl="/usr/local/bin/piactl"
# Set connect/disconnecttimeout (seconds)
timeout=10
#-------------------------------------------------------------------------------
notify () {
echo $1
if [ -f `which osascript` ]; then
osascript -e "display notification \"$1\" with title \"PIA Toggle\""
fi
}
if [ ! -f "$piactl" ]; then
notify "Please update path to piactl in piatoggle.sh"
exit 1
fi
status=`$piactl get connectionstate`
timer1=$(date -u +%s)
if [[ $status == "Disconnect"* ]]; then
$piactl connect
else
$piactl disconnect
fi
status=`$piactl get connectionstate`
echo "$status"
while [[ $status != *"onnected" ]]; do
progress=`$piactl get connectionstate`
timer2=$(date -u +%s)
if [[ $progress != $status ]]; then
echo "$progress"
fi
status=$progress
elapsed=$(($timer2-$timer1))
if (( $elapsed < $timeout )); then
sleep 1
else
notify "Timeout"
exit 1
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment