setup-dns.in 1.17 KB
Newer Older
1 2 3 4 5
#!/bin/sh

PREFIX=
. "$PREFIX/lib/libalpine.sh"

6
usage() {
7 8
	cat <<-__EOF__
		usage: setup-dns [-h] [-d domain name] [IPADDR...]
9

10
		Setup ${ROOT}etc/resolv.conf DNS settings
11

12 13 14
		options:
		 -h  Show this help
		 -d  specify search domain name
15

16 17
		The optional IPADDR are a list of DNS servers to use.
	__EOF__
18
	exit 1
19 20 21
}

while getopts "d:n:h" opt; do
22 23 24 25 26
	case $opt in
		d) DOMAINNAME="$OPTARG";;
		h) usage;;
		n) NAMESERVERS="$OPTARG";;
	esac
27
done
28 29
shift $(($OPTIND - 1))

30

31
conf="${ROOT}etc/resolv.conf"
32

33
if [ -f "$conf" ] ; then
34 35
	domain=$(awk '/^domain/ {print $2}' $conf)
	dns=$(awk '/^nameserver/ {print $2}' $conf)
36 37
fi

38 39 40 41 42 43 44
if [ -n "$DOMAINNAME" ];then
	domain="$DOMAINNAME"
else
	echon "DNS domain name? (e.g 'bar.com') [$domain] "
	default_read domain $domain
fi

45
if [ -n "$NAMESERVERS" ] || [ $# -gt 0 ];then
46 47 48 49 50 51 52 53 54
	dns="$NAMESERVERS"
else
	echon "DNS nameserver(s)? ["
	for i in $dns ; do
		echon "$i "
	done
	echon "] "
	default_read dns "$dns"
fi
55

56
if [ "$domain" != "" ]; then
57
	mkdir -p "${conf%/*}"
58 59
	echo "search $domain" > $conf
fi
60

61
if [ -n "$dns" ] || [ $# -gt 0 ] && [ -f "$conf" ]; then
62 63 64 65
	sed -i -e '/^nameserver/d' $conf
fi
for i in $dns $@; do
	mkdir -p "${conf%/*}"
66 67
	echo "nameserver $i" >> $conf
done