Skip to content

Instantly share code, notes, and snippets.

@dyens
Created November 9, 2023 18:29
Show Gist options
  • Save dyens/12d4fc505348a52f56beb66844f0b89c to your computer and use it in GitHub Desktop.
Save dyens/12d4fc505348a52f56beb66844f0b89c to your computer and use it in GitHub Desktop.
update-resolv-conf
#!/bin/bash
#
# Adjusted for CentOS7/RHEL/Fedora
#
# Note: ideally networKManager would handle this via dnsmasq, but it doesn't
# appear to handle this dynamically as of yet. So we'll just clobber it.
# We run the risk that NeworkManager will replace it later..
#
# Parses DHCP options from openvpn to update resolv.conf
# To use set as 'up' and 'down' script in your openvpn *.conf:
# up /etc/openvpn/update-resolv-conf
# down /etc/openvpn/update-resolv-conf
#
# Used snippets of resolvconf script by Thomas Hood and Chris Hanson.
# Licensed under the GNU GPL. See /usr/share/common-licenses/GPL.
#
# Example envs set from openvpn:
#
# foreign_option_1='dhcp-option DNS 193.43.27.132'
# foreign_option_2='dhcp-option DNS 193.43.27.133'
# foreign_option_3='dhcp-option DOMAIN be.bnc.ch'
#
split_into_parts()
{
part1="$1"
part2="$2"
part3="$3"
}
case "$script_type" in
up)
NMSRVRS=""
SRCHS=""
for optionvarname in ${!foreign_option_*} ; do
option="${!optionvarname}"
echo "Found Option: $option"
split_into_parts $option
if [ "$part1" = "dhcp-option" ] ; then
if [ "$part2" = "DNS" ] ; then
NMSRVRS="${NMSRVRS:+$NMSRVRS }$part3"
elif [ "$part2" = "DOMAIN" ] ; then
SRCHS="${SRCHS:+$SRCHS }$part3"
fi
fi
done
R=""
[ "$SRCHS" ] && R="search $SRCHS
"
for NS in $NMSRVRS ; do
R="${R}nameserver $NS
"
done
cp /etc/resolv.conf "/etc/resolv.conf.pre:$dev"
cat > /etc/resolv.conf <<END
# generated by /etc/openvpn/update-resolv-conf
$R
END
;;
down)
mv "/etc/resolv.conf.pre:$dev" /etc/resolv.conf
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment