Skip to content

Instantly share code, notes, and snippets.

@corysolovewicz
Last active September 6, 2019 19:02
Show Gist options
  • Save corysolovewicz/2c7388eff580bef71d79e5bfc29996f9 to your computer and use it in GitHub Desktop.
Save corysolovewicz/2c7388eff580bef71d79e5bfc29996f9 to your computer and use it in GitHub Desktop.
macOS Mojave connect to vpn via commandline
#!/bin/bash
# Call with <script> "<VPN Connection Name>"
set -e
#set -x
vpn="$1"
echo "$vpn"
function isnt_connected () {
#scutil --nc status "$vpn" | sed -n 1p | grep -qv Connected
#echo "inside isnt_connected()"
networksetup -showpppoestatus "$vpn" | sed -n 1p | grep -vqw connected
#echo "exiting isnt_connected()"
}
function poll_until_connected () {
echo "inside poll_until_connected()"
let loops=0 || true
let max_loops=200 # 200 * 0.1 is 20 seconds. Bash doesn't support floats
while isnt_connected "$vpn"; do
sleep 0.1 # can't use a variable here, bash doesn't have floats
let loops=$loops+1
[ $loops -gt $max_loops ] && break
done
[ $loops -le $max_loops ]
echo "exiting poll_until_connected()"
}
#scutil --nc start "$vpn"
echo "connecting vpn"
networksetup -connectpppoeservice "$vpn"
echo "ran vpn connection command"
if poll_until_connected "$vpn"; then
echo "Connected to $vpn!"
exit 0
else
echo "I'm too impatient!"
#scutil --nc stop "$vpn"
echo "disconnecting vpn"
networksetup -disconnectpppoeservice "$vpn"
echo "vpn disconected command finished"
exit 1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment