Skip to content

Instantly share code, notes, and snippets.

@LeePorte
Last active October 5, 2023 22:56
Show Gist options
  • Save LeePorte/d08ac8d906a6dbc57d68f51ac1dcf3ef to your computer and use it in GitHub Desktop.
Save LeePorte/d08ac8d906a6dbc57d68f51ac1dcf3ef to your computer and use it in GitHub Desktop.
Updating openvpn DNS and domain options for resolvectl on Ubuntu 22.04+
#!/bin/bash
#
# Parses DHCP options from OpenVPN and calls resolvectl
# configuration file with DNS settings, so they will be used by
# systemd-resolved.
#
# To use set as 'up' script in your openvpn *.conf:
# up /etc/openvpn/update-resolvectl
IFNAME=$1
case $script_type in
up)
for optionname in ${!foreign_option_*} ; do
option="${!optionname}"
echo $option >&2
part1=$(echo "$option" | cut -d " " -f 1)
if [ "$part1" == "dhcp-option" ] ; then
part2=$(echo "$option" | cut -d " " -f 2)
part3=$(echo "$option" | cut -d " " -f 3)
if [ "$part2" == "DNS" ] ; then
IF_DNS_NAMESERVERS="$IF_DNS_NAMESERVERS $part3"
fi
if [[ "$part2" == "DOMAIN" || "$part2" == "DOMAIN-SEARCH" ]] ; then
IF_DNS_SEARCH="$IF_DNS_SEARCH $part3"
fi
fi
done
DNS=""
for dns in $IF_DNS_NAMESERVERS; do
DNS="$DNS $dns"
done
/usr/bin/resolvectl dns ${IFNAME} ${DNS}
DOMAIN=""
for domain in $IF_DNS_SEARCH; do
DOMAIN="$DOMAIN $domain"
done
/usr/bin/resolvectl domain ${IFNAME} ${DOMAIN}
;;
down)
/usr/bin/resolvectl revert ${IFNAME}
;;
esac
@LeePorte
Copy link
Author

LeePorte commented Sep 1, 2022

@redsie Thank you for pointing out my copy pasta error. I have also added a down case to make sure of a clean exit.

@tsgsh
Copy link

tsgsh commented Oct 3, 2023

Now there's a typo :-)
easc
should be
esac

@LeePorte
Copy link
Author

LeePorte commented Oct 5, 2023

@tsgsh Thanks for pointing that one out.

@tsgsh
Copy link

tsgsh commented Oct 5, 2023

Works beautifully on Fedora 38. Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment