/
set-dns
executable file
·71 lines (58 loc) · 1.3 KB
/
set-dns
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#!/bin/sh
SET="0"
SERVERS=""
CONFFILE="/etc/defaults/set-dns.conf"
while getopts ":c:" cmd; do
case $cmd in
c) CONFFILE="$OPTARG" ;;
esac
done
[ -f "$CONFFILE" ] && . "$CONFFILE"
usage() {
cat <<-EOT
Usage: `basename $0` [OPTION]... [SERVER]...
Show or set the DNS servers for the PrimaryService using scutil(1).
-c <file> use <file> as the config file, instead of
/etc/defaults/set-dns.conf
-s set server list
-? display this help and exit
By default, SERVERS is set to:
$SERVERS
To change this, edit the set-dns.conf file.
EOT
}
OPTIND=1
while getopts ":c:s?" cmd; do
## echo "$cmd/$OPTIND: $OPTARG"
case $cmd in
c) ;;
s) SET="1" ;;
[?]) usage; exit ;;
esac
done
shift $((OPTIND - 1))
if [ "$*" != "" ]; then
SERVERS="$*"
fi
SERVICE=`echo "show State:/Network/Global/IPv4" | scutil | awk '/PrimaryService/ { print $3 }'`
KEY="State:/Network/Service/$SERVICE/DNS"
if [ "$SET" = "1" ]; then
if [ "`id -u`" != "0" ]; then
echo "You must be root to set DNS servers."
exit
fi
scutil <<-EOT
open
get $KEY
d.add ServerAddresses * $SERVERS
d.show
set $KEY
close
EOT
else
scutil <<-EOT
open
show $KEY
close
EOT
fi