forked from StevenBlack/hosts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupdateHosts.sh
More file actions
107 lines (93 loc) · 2.3 KB
/
Copy pathupdateHosts.sh
File metadata and controls
107 lines (93 loc) · 2.3 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#!/usr/bin/env bash
# This script will create in first running backup of ORIGINAL/CURRENT hosts file in hosts.skel file.
# If hosts.skel file exists, then NEW copy with customized unified hosts file will be copied to proper path.
# Next DNS Cache will be refreshed.
# YOU NEED RUNNING THIS SCRIPT FILE IN COMMAND LINE PROMPT WITH ADMINISTRATOR PRIVILIGES
#
# Might have to change change execute for the script file
# chmod 755 updaeHosts.sh
#
# ./updaeHosts.sh OR sh updaeHosts.sh
flushDNS() {
# Find OS name
#if Darwin (Mac OSX)
#if Linux (Ubunut /Debian)
OS=$(uname)
# Find Mac version and cleares the DNS Cache
if [ "$OS" = "Darwin" ]
then
MAJOR_MAC_VERSION=$(sw_vers -productVersion | awk -F '.' '{print $2}')
if [ "$MAJOR_MAC_VERSION" -ge "10" ]
then
sudo killall -HUP mDNSResponder
else
sudo dscacheutil -flushcache
fi
fi
if [ "$OS" = "Linux" ]
then
# Find Unix version and cleares the DNS Cache
MAJOR_UNIX_NAME=$(lsb_release -i | awk -F ":" '{print $2}')
if [ "$MAJOR_UNIX_NAME" = "Ubuntu" ]
then
sudo /etc/rc.d/init.d/nscd restart
fi
fi
echo "* * * DNS Cache Cleared * * *"
}
updateHosts() {
echo "* * * Updating hosts * * *"
python updateHostsFile.py -a >> /dev/null
echo "* * * Copying hosts to /etc/hosts * * *"
sudo cp hosts /etc/hosts
flushDNS
}
defaultHosts()
{
cat <<EOF
# IPv4
127.0.0.1 localhost
127.0.0.1 localhost.localdomain
127.0.0.1 local
255.255.255.255 broadcasthost
# IPv6
::1 localhost
fe80::1%lo0 localhost
EOF
}
# Restore default Hosts file.
restore()
{
OUT=$(defaultHosts)
echo "* * * Restoring default hosts * * *"
echo "This is restored default hosts file: $OUT" > hosts
sudo cp hosts /etc/hosts
flushDNS
}
# Update Hosts file from various sources.
update() {
if [ -f "/etc/hosts.skel" ]
then
updateHosts
else
echo "* * * Creating a Backup of current hosts file (/etc/hosts.skel) * * *"
sudo cp /etc/hosts /etc/hosts.skel
updateHosts
fi
}
if [ "$1" = "-u" ] || [ "$1" = "--update" ]
then
update
elif [ "$1" = "-r" ] || [ "$1" = "--restore" ]
then
restore
else
echo "Use this Script to update OR restore hosts file"
echo ""
echo "# Useage #"
echo "./t.sh -u OR ./t.sh -r"
echo ""
echo "-u or --update : to update the hosts file for various sources"
echo "-r or --restore : to restore the hosts file to system default"
echo ""
fi