Last active
October 3, 2019 13:56
-
-
Save jtyr/01d1f0bee0a83a44d28f53a663a2a45a to your computer and use it in GitHub Desktop.
Script which helps to configure resolv.conf based on information pushed by the OpenVPN server.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
### | |
# | |
# Description | |
# ----------- | |
# | |
# Script which helps to configure resolv.conf based on information pushed by | |
# the OpenVPN server. | |
# | |
# Usage | |
# ----- | |
# | |
# Put this file into /etc/openvpn/resolv_conf.sh and configure the client to | |
# use it by adding the following into the config file: | |
# | |
# script-security 2 | |
# up /etc/openvpn/resolv_conf.sh | |
# down /etc/openvpn/resolv_conf.sh | |
# | |
# Requirements | |
# ------------ | |
# | |
# Bash v4.0+ | |
# | |
### | |
PATH='/bin:/usr/bin:/usr/local/bin:/sbin:/usr/sbin:/usr/local/sbin' | |
function up() { | |
echo 'Running resolv.conf UP script...' >&2 | |
domain='' | |
search='' | |
nameserver='' | |
for opt in ${!foreign_option_*} ; do | |
opt="${!opt}" | |
if [[ ${opt:0:12} == 'dhcp-option ' ]]; then | |
opt="${opt:12}" | |
key="${opt%% *}" | |
val="${opt#* }" | |
if [[ $key == 'DNS' ]]; then | |
nameserver+="nameserver $val\n" | |
elif [[ $key == 'DOMAIN' ]]; then | |
if [[ -z $search ]]; then | |
search='search' | |
fi | |
search+=" $val" | |
elif [[ $key == 'ADAPTER_DOMAIN_SUFFIX' ]]; then | |
domain="domain $val\n" | |
fi | |
fi | |
done | |
if [ -n "$domain$nameserver$search" ]; then | |
if [ -e /etc/resolv.conf ]; then | |
cp /etc/resolv.conf{,.ovpnsave} | |
fi | |
echo -e "$domain$nameserver$search" > /etc/resolv.conf | |
fi | |
} | |
function down() { | |
echo 'Running resolv.conf DOWN script...' >&2 | |
if [ -e /etc/resolv.conf.ovpnsave ]; then | |
cp /etc/resolv.conf{.ovpnsave,} | |
rm -f /etc/resolv.conf.ovpnsave | |
fi | |
} | |
function main() { | |
if [[ $script_type == 'up' ]]; then | |
up | |
elif [[ $script_type == 'down' ]]; then | |
down | |
fi | |
} | |
main |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment