Skip to content

Instantly share code, notes, and snippets.

@pklaus

pklaus/client.sh Secret

Created January 5, 2011 08:40
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pklaus/5084036e9a4b4206e074 to your computer and use it in GitHub Desktop.
Save pklaus/5084036e9a4b4206e074 to your computer and use it in GitHub Desktop.
Setting DNS Servers as pushed by an OpenVPN Server. Uses resolvconf.
#!/bin/sh
# script for OpenVPN to set dns servers pushed by the server using resolvconf on the client.
# <http://openvpn.net/archive/openvpn-users/2006-10/msg00218.html>
# up down script for client.ovpn configuration files (to be set using:)
# up ./client.sh
# down ./client.sh
UPORDOWN=$1
DEV=$2
case $UPORDOWN in
up)
set |
sed -n "s/^foreign_option_.* DNS \(.*\)'/nameserver \1/; T next; p;
:next; s/^foreign_option_.* DOMAIN \(.*\)'/domain \1/; T; p;" |
resolvconf -a $DEV && resolvconf -u ;;
down)
resolvconf -d $DEV && resolvconf -u ;;
*) echo das war wohl nichts ;;
esac
@RobKehl
Copy link

RobKehl commented Oct 5, 2013

Thanks for the script, it helped out some time ago.

Nowadays it on the one hand does not work with recent versions of OpenVPN anymore, it is superseeded by /etc/openvpn/update-resolv-conf which ships with openvpn. From version 2.3.2-5, it plays very nicely with resolvconf, even up to that it plays fairly nice.

On the other hand, the script "client.sh" at least has the design flaw that it depends on "foreign_option_.* DNS " appearing at the beginning of a line of the output of 'set', which is expensive and subject to break: Just reg a function that uses "cat<<EOF" and outputs a string that matches and your resolv.conf will be severely broken in cases.

So, you might consider removing this or pointing out the limitations, don't you feel so?

With kind regards,

Rob

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