Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
update-resolv-conf for openSUSE, using SUSE's netconfig with dirty tricks. No need for resolvconf.
#!/bin/bash
#
# 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 <jdthood@yahoo.co.uk>
# and Chris Hanson
# Licensed under the GNU GPL. See /usr/share/common-licenses/GPL.
#
# Adjusted for openSUSE and its 'netconfig' component
# using dirty tricks by multiple1902, kodmasin and sebix.
# Supports DNS Servers and Searchnames.
# Preserves pre-existing DNS servers and search names.
#
# 05/2006 chlauber@bnc.ch
# 01/2012 multiple1902@gmail.com
# 01/2014 boris@kodmasin.net
# 04/2021 sebix@sebix.at
#
# 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'
[ -z "$DEBUG" ] || set -x
if [ -x "/usr/bin/logger" ]; then
logger="/usr/bin/logger -t 'OpenVPN:update-resolve-conf'"
else
logger=":"
fi
set -eu -o pipefail
function print_error {
read line file <<<$(caller)
$logger -s "An error occurred in line $line of file $file:"
sed "${line}q;d" "$file" | xargs $logger -s --
}
trap print_error ERR
NETCONFIG="/sbin/netconfig"
if [ ! -x "$NETCONFIG" ] ; then
$logger -s "[OpenVPN:update-resolve-conf] missing binary $NETCONFIG. Aborting.";
exit 1;
fi
if [ -z "$script_type" ]; then
$logger -s "[OpenVPN:update-resolve-conf] variable script_type is empty. Aborting.";
exit 1;
fi
if [ -z "$dev" ]; then
$logger -s "[OpenVPN:update-resolve-conf] variable dev is empty. Aborting.";
exit 1;
fi
case $script_type in
up)
IF_DNS_NAMESERVERS=''
IF_DNS_SEARCHNAMES=''
for optionname in ${!foreign_option_*} ; do
option="${!optionname}"
part1=$(echo "$option" | cut -d " " -f 1)
$logger "[OpenVPN:update-resolve-conf] received option $option.";
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"
elif [ "$part2" == "DOMAIN" ] ; then
IF_DNS_SEARCHNAMES="$IF_DNS_SEARCHNAMES $part3"
fi
fi
done
$logger "[OpenVPN:update-resolve-conf] setting now DNSSERVERS='$IF_DNS_NAMESERVERS'"
$logger "[OpenVPN:update-resolve-conf] setting now DNSSEARCH='$IF_DNS_SEARCHNAMES'"
$NETCONFIG modify -i "$dev" -s vpn << EOF
DNSSERVERS='$IF_DNS_NAMESERVERS'
DNSSEARCH='$IF_DNS_SEARCHNAMES'
EOF
;;
down)
$NETCONFIG remove -i "$dev" -s vpn
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment