(Solved) NTP and update-resolv-conf

About writing shell scripts and making the most of your shell
Forum rules
Topics in this forum are automatically closed 6 months after creation.
Locked
User avatar
Pippin
Level 4
Level 4
Posts: 441
Joined: Wed Dec 13, 2017 11:14 am
Location: The Shire

(Solved) NTP and update-resolv-conf

Post by Pippin »

Hi,

Using update-resolv-conf script I just discovered that dhcp-option NTP is not working.
tcpdump shows NTP going to the system configured server instead of the pushed one.
update-resolv-conf

Code: Select all

#!/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 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'
#

[ -x /sbin/resolvconf ] || exit 0
[ "$script_type" ] || exit 0
[ "$dev" ] || exit 0

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 "$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
	echo -n "$R" | /sbin/resolvconf -a "${dev}.openvpn"
	;;
  down)
	/sbin/resolvconf -d "${dev}.openvpn"
	;;
esac
Can see that DNS and DOMAIN is done.
Since I know too little about scripting, someone willing to adjust it too include NTP?

Thanks.
Last edited by LockBot on Wed Dec 28, 2022 7:16 am, edited 3 times in total.
Reason: Topic automatically closed 6 months after creation. New replies are no longer allowed.
I gloomily came to the ironic conclusion that if you take a highly intelligent person and give them the best possible, elite education, then you will most likely wind up with an academic who is completely impervious to reality.
Halton Arp
User avatar
Pippin
Level 4
Level 4
Posts: 441
Joined: Wed Dec 13, 2017 11:14 am
Location: The Shire

Re: NTP and update-resolv-conf

Post by Pippin »

Solved using up/down script.

client.conf:

Code: Select all

setenv PATH /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
script-security 2
up /etc/openvpn/up.sh
down /etc/openvpn/down.sh
down-pre
up.sh

Code: Select all

#!/bin/sh -x

run() {
    $@
    if [ $? -ne 0 ]
        then
        echo "$@ Failed with exit code $?"
        return 1
    else
        return 0
    fi
}

run /etc/openvpn/update-resolv-conf &&
    run mv /etc/ntp.conf /etc/ntp-bak.conf &&
        mv /etc/ntp-ovpn.conf /etc/ntp.conf &&
            run systemctl restart ntp
down.sh

Code: Select all

#!/bin/sh -x

run() {
    $@
    if [ $? -ne 0 ]
        then
        echo "$@ Failed with exit code $?"
        return 1
    else
        return 0
    fi
}

run /etc/openvpn/update-resolv-conf &&
    run mv /etc/ntp.conf /etc/ntp-ovpn.conf &&
        mv /etc/ntp-bak.conf /etc/ntp.conf &&
            run systemctl restart ntp
I gloomily came to the ironic conclusion that if you take a highly intelligent person and give them the best possible, elite education, then you will most likely wind up with an academic who is completely impervious to reality.
Halton Arp
Locked

Return to “Scripts & Bash”