Skip to content

Instantly share code, notes, and snippets.

@nitrocode
Last active May 18, 2022 21:30
Show Gist options
  • Save nitrocode/559f1717ee5d610c3548b3e056b84e5e to your computer and use it in GitHub Desktop.
Save nitrocode/559f1717ee5d610c3548b3e056b84e5e to your computer and use it in GitHub Desktop.
Restart Cato on a schedule using an applescript and launchd
#!/usr/bin/osascript
(*
author: nitrocode
source: https://gist.github.com/nitrocode/559f1717ee5d610c3548b3e056b84e5e
Leave this commented block in case this file is ever updated or forked so it will be easier to update and audit in the future.
*)
tell application "System Events"
(* kill cato *)
set theID to (unix id of processes whose name is "CatoClient")
try
do shell script "kill -9 " & theID
end try
(* wait a few seconds *)
delay 3
(* start application *)
tell application "CatoClient" to activate
(* click the buttons *)
tell process "CatoClient" to tell window "Cato Networks" to tell splitter group 1
(* Due to buttons and text not having descriptions, we use
static text 1 and button 1 as identifiers *)
if (value of static text 1 is equal to "Disconnected") then
(* Text says disconnected so reconnect. *)
click button 1
else
(* Text says connected so disconnect and reconnect. *)
click button 1
delay 2
click button 1
end if
end tell
end tell
<!--
Customized from original source: https://gist.github.com/nateswart/8329172
author: nitrocode
source: https://gist.github.com/nitrocode/559f1717ee5d610c3548b3e056b84e5e
Leave this commented block in case this file is ever updated or forked so it will be easier to update and audit in the future.
-->
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Disabled</key>
<false/>
<key>Label</key>
<string>local.nitrocode.catovpn_restart</string>
<key>Program</key>
<string>/usr/bin/osascript</string>
<key>ProgramArguments</key>
<array>
<string>osascript</string>
<string>/usr/local/bin/catovpn_restart.scpt</string>
</array>
<key>RunAtLoad</key>
<true/>
<key>StartInterval</key>
<integer>3600</integer>
</dict>
</plist>

Restart Cato

Sometimes Cato VPN's latency can deterioriate overtime and a simple workaround is to restart the app and reconnect to the VPN.

Install

  1. Download the apple script

    wget https://gist.githubusercontent.com/nitrocode/559f1717ee5d610c3548b3e056b84e5e/raw/catovpn_restart.scpt \
      -O /usr/local/bin/catovpn_restart.scpt
    chmod +x /usr/local/bin/catovpn_restart.scpt
    • This can now be run manually using osascript /usr/local/bin/catovpn_restart.scpt
  2. Download the plist

    This defaults to running every hour (3600 seconds) which is controlled by the StartInterval and can be modified once this file is downloaded.

    wget https://gist.githubusercontent.com/nitrocode/559f1717ee5d610c3548b3e056b84e5e/raw/local.nitrocode.catovpn_restart.plist \
      -O ~/Library/LaunchAgents/local.nitrocode.catovpn_restart.plist
  3. Load the plist

    launchctl load ~/Library/LaunchAgents/local.nitrocode.catovpn_restart.plist

    It is required to add osascript to Security & Privacy > Accessibility and if testing locally, the following may want to be added too:

    • AppleScript
    • Script Editor
    • Terminal
    • iTerm

Note: This will not insert your 2fa but it will prompt you when the previous 2fa session token has expired.

Modify interval

If the interval is to be modified, the plist will have to be unloaded and reloaded.

launchctl unload ~/Library/LaunchAgents/local.nitrocode.catovpn_restart.plist
launchctl load ~/Library/LaunchAgents/local.nitrocode.catovpn_restart.plist

Uninstall

launchctl unload ~/Library/LaunchAgents/local.nitrocode.catovpn_restart.plist
rm ~/Library/LaunchAgents/local.nitrocode.catovpn_restart.plist
rm /usr/local/bin/catovpn_restart.scpt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment