-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathalterDnsServices.sh
More file actions
executable file
·86 lines (71 loc) · 1.43 KB
/
alterDnsServices.sh
File metadata and controls
executable file
·86 lines (71 loc) · 1.43 KB
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
#!/bin/bash
WORK="work"
HOME="home"
SCRIPT="alterDnsServices.sh"
function usage()
{
echo "Usage: $SCRIPT [work | home]"
}
function getWiFiDnsServices()
{
local wifi=$(networksetup listallnetworkservices | grep Wi-Fi);
if [ ${#wifi} -le 0 ];
then
echo "The Wi-Fi network is not in this computer."
return 3;
fi
local wifiDnsServices=$(networksetup getdnsservers Wi-Fi);
local dnsAddress="127.127.127.127";
if [ ${#wifiDnsServices} -gt ${#dnsAddress} ];
then
wifiDnsServices=" ";
fi
echo "$wifi $wifiDnsServices";
return 0;
}
function setDnsServicesForWork()
{
local dnsInfo=$(getWiFiDnsServices);
if [ $? -ne 0 ];
then
echo "error code: $?";
return 4;
fi
dnsInfo=($dnsInfo);
if [ ${#dnsInfo[*]} -eq 2 ];
then
networksetup setdnsservers Wi-Fi empty;
fi
getWiFiDnsServices;
}
function setDnsServicesForHome()
{
local dnsInfo=$(getWiFiDnsServices);
if [ $? -ne 0 ];
then
echo "errorcode: $?";
return 4;
fi
if [ ${#dnsInfo[*]} -eq 1 ] || [ ! ${dnsInfo[1]} = '8.8.8.8' ];
then
networksetup setdnsservers Wi-Fi 8.8.8.8;
fi
getWiFiDnsServices;
}
if [ $# -ne 1 ]
then
usage;
exit 1;
fi
case "$1" in
$WORK)
setDnsServicesForWork;
;;
$HOME)
setDnsServicesForHome;
;;
*)
usage;
exit 2;
;;
esac