Skip to content

Instantly share code, notes, and snippets.

@LeePorte
Created July 12, 2022 12:13
Show Gist options
  • Save LeePorte/26ab81a1a2811ab5eab3450182c64827 to your computer and use it in GitHub Desktop.
Save LeePorte/26ab81a1a2811ab5eab3450182c64827 to your computer and use it in GitHub Desktop.
#!/bin/bash
#
# Parses DHCP options from OpenVPN and calls systemd-resolve
# 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-systemd-resolve
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/systemd-resolve dns ${IFNAME} ${DNS}
DOMAIN=""
for domain in $IF_DNS_SEARCH; do
DOMAIN="$DOMAIN $domain"
done
/usr/bin/systemd-resolve domain ${IFNAME} ${DOMAIN}
;;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment