macOS: Switching between OpenVPN and NextDNS automatically

Since macOS doesn’t support OpenVPN out of the box, the easiest solution is to use Tunnelblick as your client. Alternatively, there’s CLI builds for OpenVPN available on Homebrew as well.

Regardless of your client of choice though, OpenVPN and NextDNS don’t play well together without a little help. A generic setup that supports both is as follows:

#!/usr/bin/env bash

# Disconnect from NextDNS before connecting to OpenVPN
networksetup -disconnectpppoeservice NextDNS
#!/usr/bin/env bash

# Reconnect to NextDNS after disconnecting from OpenVPN
networksetup -connectpppoeservice NextDNS

With this setup, whenever you connect to a VPN using Tunnelblick, NextDNS will automatically be turned off. When you disconnect, NextDNS will be turned on again, allowing you to easily switch between the two.