Skip to content

Instantly share code, notes, and snippets.

@zxlin
Last active May 10, 2024 16:08
Show Gist options
  • Save zxlin/bda32b18376eb91de8be94dab569dbbe to your computer and use it in GitHub Desktop.
Save zxlin/bda32b18376eb91de8be94dab569dbbe to your computer and use it in GitHub Desktop.
Automatically Connect to GlobalProtect VPN on MacOS Wakeup
#!/bin/bash
# Toggle GlobalProtect VPN with AppleScript and automatically close the landing page in Chrome
# Adapted from Trevor Manternach https://gist.github.com/tmanternach/cbd4c213eab8569e38d6cd021b6255e5
# Tested using macOS Sonoma 14.4 and GlobalProtect version 5.2
# Use sleepwatcher `brew install sleepwatcher` and configure a bash script in `~/.wakeup` to run this script.
# Make sure to provide accessibility permissions to the sleepwatcher binary for the applescript to work.
# Update the GP_URL if your GlobalProtect's landing page has a different URL or make it more specific
function screenIsUnlocked { [ "$(/usr/libexec/PlistBuddy -c "print :IOConsoleUsers:0:CGSSessionScreenIsLocked" /dev/stdin 2>/dev/null <<< "$(ioreg -n Root -d1 -a)")" != "true" ] && return 0 || return 1; }
until screenIsUnlocked; do; sleep 1; done # Can't connect/auth GP if screen is still locked
osascript 2>/dev/null <<EOF
set GP_URL to "/ACS" -- the URL of the ACS landing page that GlobalProtect opens after you authenticate
tell application "System Events" to tell process "GlobalProtect"
delay 1
click menu bar item 1 of menu bar 2 -- Activates the GlobalProtect "window" in the menubar
set statusText to name of static text 2 of window 1
if statusText is "Not Connected" then
click button 2 of window 1 -- Clicks Connect
repeat until (name of static text 2 of window 1 is "Connected") -- wait until connected
delay 1
end repeat
tell application "Google Chrome" -- close the GlobalProtect connected tab
repeat with win in windows
close (tabs of win whose URL contains GP_URL)
end repeat
end tell
end if
end tell
EOF
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment