Skip to content

Instantly share code, notes, and snippets.

@coneybeare
Last active December 5, 2023 13:26
Show Gist options
  • Save coneybeare/fb9481b2c1fb0d90f40b30fce4c68ac7 to your computer and use it in GitHub Desktop.
Save coneybeare/fb9481b2c1fb0d90f40b30fce4c68ac7 to your computer and use it in GitHub Desktop.
on idle
# Change these to your VPN's IP Address, and the VPN's name in your Mac's Network System Prefernces panel.
set vpnIPAddress to "111.222.333.444"
set vpnServiceName to "AlgoVPN"
set myIP to "127.0.0.1"
set shellScriptCommands to {¬
"dig +short myip.opendns.com @resolver1.opendns.com", ¬
"dig TXT +short o-o.myaddr.l.google.com @ns1.google.com | awk -F'\"' '{ print $2}'", ¬
"curl ifconfig.me", ¬
"curl -s http://checkip.dyndns.org/ | grep -o \"[[:digit:].]\\+\"", ¬
¬
""}
repeat with shellScriptCommand in shellScriptCommands
try
set myIP to do shell script shellScriptCommand
exit repeat # if successful
on error errorMessage number errorNumber
end try
end repeat
if myIP is not equal to vpnIPAddress then
tell application "System Events"
tell process "SystemUIServer"
set vpnMenu to (menu bar item 1 of menu bar 1 where description is "VPN")
tell vpnMenu to click
set connectionStatusItem to (menu item 1 of menu 1 of vpnMenu)
if title of connectionStatusItem is equal to ("Connect " & vpnServiceName) then
tell connectionStatusItem to click
end if
end tell
end tell
end if
end idle
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment