Skip to content

Instantly share code, notes, and snippets.

@thomas-p-wilson
Created March 14, 2020 01:01
Show Gist options
  • Save thomas-p-wilson/08796d8b392dba130cc2600e627baa5a to your computer and use it in GitHub Desktop.
Save thomas-p-wilson/08796d8b392dba130cc2600e627baa5a to your computer and use it in GitHub Desktop.
Patch OpenVPN client config to support DNS in Ubuntu/Debian
#!/bin/bash
#
# A utility for patching the user's client connection profile to support DNS
# resolution in OpenVPN clients which do not otherwise support it.
#
if ! pidof systemd 2>&1 >/dev/null; then
echo "This script currently supports only SystemD users"
return 1 2>/dev/null
exit 1
fi
if ! type wget 2>&1 >/dev/null && ! type curl 2>&1 >/dev/null ; then
echo "This script requires either wget or curl"
return 1 2>/dev/null
exit 1
fi
if ! type sed 2>&1 >/dev/null ; then
echo "This script requires sed"
return 1 2>/dev/null
exit 1
fi
if [ ! -f "$1" ]; then
echo "First argument must be the path to an OpenVPN client configuration"
return 1 2>/dev/null
exit 1
fi
# Download the script for updating resolved
if [ ! -f "/etc/openvpn/scripts/update-systemd-resolved" ]; then
if type wget 2>&1 >/dev/null ; then
sudo wget https://raw.githubusercontent.com/jonathanio/update-systemd-resolved/master/update-systemd-resolved -P /etc/openvpn/scripts
elif type curl 2>&1 >/dev/null ; then
curl -O /etc/openvpn/scripts/update-systemd-resolved https://raw.githubusercontent.com/jonathanio/update-systemd-resolved/master/update-systemd-resolved
fi
sudo chmod +x /etc/openvpn/scripts/update-systemd-resolved
fi
# Update the OpenVPN client connection configuration
sed -i -e '/^#\?\(\s*script-security\s\).*/{s//\12/;:a;n;ba;q}' -e '$ascript-security 2' "$1"
sed -i -e '/^#\?\(\s*up\s\).*/{s//\1\/etc\/openvpn\/scripts\/update-systemd-resolved/;:a;n;ba;q}' -e '$aup /etc/openvpn/scripts/update-systemd-resolved' "$1"
sed -i -e '/^#\?\(\s*down\s\).*/{s//\1\/etc\/openvpn\/scripts\/update-systemd-resolved/;:a;n;ba;q}' -e '$adown /etc/openvpn/scripts/update-systemd-resolved' "$1"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment