Skip to content

Instantly share code, notes, and snippets.

@up209d
Last active September 6, 2020 07:53
Show Gist options
  • Save up209d/aaecddad3316b8ee2680009f87ed5395 to your computer and use it in GitHub Desktop.
Save up209d/aaecddad3316b8ee2680009f87ed5395 to your computer and use it in GitHub Desktop.
Auto connecting to VPN on MacOS
#!/bin/sh
# VPN.sh
###### SETUP #######
# You need to setup and be able to connect to you VPN at least once
# Change these variables for your setup
# You can export your VPN_PWD into .bash_profile (.zprofile if you are using zsh) so it won't be exposed in the command
# In .bash_profile or .zprofile insert lines
# EXPORT AUTO_VPN=<YOUR VPN NAME>
# EXPORT AUTO_VPN_PWD=<YOUR PASSWORD>
# Run chmod +x VPN.sh
###### RUN #######
# VPN.sh --auto
# RECONNECT_AFTER=10.0 # If cancel reconnecting, recheck after 10.0 second
CHECKING_RATE=1.0 # Check connection status once in 1.0 second
IS_AUTOMATING=0 # 0 | 1
CONNECTION_STATUS="Disconnected" # Connected | Connecting | Disconnected
connect () {
echo "Using VPN Service: $AUTO_VPN"
scutil --nc start "$AUTO_VPN"
sleep 0.5
osascript -e "
set foundApplication to false
set foundWindowIndex to -1
repeat while foundApplication is equal to false
tell application \"System Events\" to set foundApplication to exists (processes where name is \"UserNotificationCenter\")
delay 0.05
end repeat
repeat while foundWindowIndex is equal to -1
tell application \"System Events\"
tell application process \"UserNotificationCenter\"
set windowCount to count windows
if (windowCount is greater than or equal to 1) then
if (value of static text 1 of window 1 contains \"VPN Connection\")
set foundWindowIndex to 1
end if
end if
end tell
end tell
delay 0.05
end repeat
log \"Found VPN Login: window \" & foundWindowIndex
tell application \"System Events\"
tell application \"UserNotificationCenter\"
activate
end tell
delay 0.05
tell application process \"UserNotificationCenter\"
tell window foundWindowIndex
set focused of text field 2 to true
keystroke \"$AUTO_VPN_PWD\"
click button 1
end tell
end tell
end tell
"
IS_AUTOMATING=0
}
polling_connection_status () {
CONNECTION_STATUS=$(scutil --nc status "$AUTO_VPN" | sed -n 1p)
while [ $CONNECTION_STATUS = "Disconnected" ] && [ $IS_AUTOMATING = 0 ]; do
CONNECTION_STATUS="Connecting"
echo "Connection is not found, now checking VPN Service: $AUTO_VPN, $CONNECTION_STATUS"
osascript -e "
tell application \"System Events\"
with timeout of 9999999999 seconds
set myMsg to \"$AUTO_VPN was diconnected\" & return & return & \"Do you want to re-connect?\"
set theResp to display dialog myMsg buttons {\"Cancel\", \"Connect\"} default button 2
end timeout
end tell
"
if [ "$?" = "0" ] && [ $IS_AUTOMATING = 0 ]; then
IS_AUTOMATING=1
connect
else
exit 0;
fi
sleep $CHECKING_RATE
done
}
if [ "$1" = "--auto" ]; then
while $true; do
polling_connection_status
sleep $CHECKING_RATE
done
else
polling_connection_status
sleep $CHECKING_RATE
exit 0
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment