Skip to content

Instantly share code, notes, and snippets.

@yfyf
Created March 25, 2014 18:06
Show Gist options
  • Save yfyf/9767622 to your computer and use it in GitHub Desktop.
Save yfyf/9767622 to your computer and use it in GitHub Desktop.
Update resolv.conf using openresolv with (DNS) settings pushed by OpenVPN
#!/bin/bash
#
# Requires openresolv.
#
# This file goes into
#
# /usr/share/openvpn/update-resolv-conf
#
# And this goes into your OpenVPN conf:
#
# script-security 2
# up /usr/share/openvpn/update-resolv-conf
# down /usr/share/openvpn/update-resolv-conf
#
# Parses DHCP options from openvpn to update resolv.conf
#
# Originally from: https://wiki.archlinux.org/index.php/OpenVPN#DNS
#
# Used snippets of resolvconf script by Thomas Hood <[email protected]>
# and Chris Hanson
# Licensed under the GNU GPL. See /usr/share/common-licenses/GPL.
# 07/2013 [email protected] Fixed intet name
# 05/2006 [email protected]
#
# 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'
# foreign_option_4='dhcp-option DOMAIN-SEARCH bnc.local'
set -e
## You might need to set the path manually here, e.g. on Debian
# RESOLVCONF = /sbin/resolvconf
RESOLVCONF=$(which resolvconf)
[ -x $RESOLVCONF ] || exit 0
case $script_type in
up)
for optionname in ${!foreign_option_*} ; do
option="${!optionname}"
echo $option
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
R=""
for DS in $IF_DNS_SEARCH ; do
R="${R}search $DS
"
done
for NS in $IF_DNS_NAMESERVERS ; do
R="${R}nameserver $NS
"
done
#echo -n "$R" | $RESOLVCONF -p -a "${dev}"
echo -n "$R" | $RESOLVCONF -a "${dev}.inet"
;;
down)
$RESOLVCONF -d "${dev}.inet"
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment