summaryrefslogtreecommitdiff
path: root/bin/changedns
blob: 4760a678e0f8941cf3254a82168c6749b88609fe (plain)
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#!/bin/bash

SN="`basename $0`"	# base portion of our filename
AE="-a"			# switch to add an entry
RE="-r"			# swithc to remove an entry
EL="/tmp/resolv.conf"	# expected location of the system nameserver config file
ARGS="$@"		# friendly variable name for all args

usage(){
	echo "usage:
$SN -a {ip} [{ip}...]
$SN -r {ip} [{ip}...]"
}

#if are there at least two arguments?
if [ "$#" -gt 1 ]
then

	#remove any old resolv.conf file
	rm .resolve.conf .resolve.old .resolve.new 2> /dev/null
			
	#cast current nameserver file into OLD
	cat "$EL" > .resolve.old

	#build IP list
	for ARG in $ARGS
	do
		if [ "$ARG" != "$1" ]
		then
			IP_LIST=`echo "$IP_LIST $ARG"`
		fi
	done

	#select case on first switch
	case "$1" in

		#case '-a'
		"$AE"	)

			#for each IP in IP_LIST
			for IP in $IP_LIST
			do
				#add nameserver entery to .newfile
				echo "nameserver $IP" >> .resolve.conf

				#cat OLD into grep excluding IP lines,
				#storing results into NEW
				cat .resolve.old | grep -v "$IP" > .resolve.new

				#set OLD to NEW
				cat .resolve.new > .resolve.old

			done

			#cat OLD onto end of .newfile
			cat .resolve.new >> .resolve.conf

			#clean up work files
			rm .resolve.new .resolve.old 

			#move old conf file to old conf file.bak
			mv "$EL" "$EL.bak"

			#move .newfile to resolve.conf file
			mv .resolve.conf "$EL"

			echo "Added $IP_LIST to $EL"
		;;

		#case '-r'
		"$RE"	)

			#for each IP in IP_LIST
			for IP in $IP_LIST
			do
				#cat OLD into grep excluding IP lines,
				#storing results into NEW
				cat .resolve.old | grep -v "$IP" > .resolve.new

				#set OLD to NEW
				cat .resolve.new > .resolve.old

			done

			#move old conf file to old conf file.bak
			mv "$EL" "$EL.bak"

			#move .newfile to resolve.conf file
			mv .resolve.new "$EL"

			echo "Removed $IP_LIST from $EL"

		;;

		#case else
		*	)

			usage
		;;

	
	#end switch
	esac

#else
else

	usage

#end
fi