Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Auto reconnects to an IKEv2/IPSEC VPN service from your Mac
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