Repository navigation
Expand file tree
/
Copy pathadhost2.sh
More file actions
executable file
·121 lines (97 loc) · 2.78 KB
/
Copy pathadhost2.sh
File metadata and controls
executable file
·121 lines (97 loc) · 2.78 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
108
109
110
111
112
113
114
115
116
117
118
#!/bin/bash
if (( EUID != 0 )); then
echo "You must be root to do this." 1>&2
exit 100
fi
flag=0
function cleanexit()
{
printf "\n caught ctl+c restoring host files"
cp -v /usr/share/adhost/backhost /etc/hosts
exit 2
}
function startup()
{
printf "#============running first time===============#\n\n "
touch /usr/share/adhost/check
echo "#------>changing hosts dir ownership to root---------#"
chown -R root:root *
echo "#---->setting 755 permision to hosts/ dir--------------#"
chmod -R 755 *
echo "#- moving --- hostlist file ---- /usr/share/adhost------#";
mkdir /usr/share/adhost 2>/dev/null
cp -v ./adhost2 /usr/share/adhost/
echo "#-backing up original host ---to /usr/share/adhost/-----#"
cp -v /etc/hosts /usr/share/adhost/originalhosts
printf "#===============================================#\n\n"
}
if pwd |sed 's/.*\///g'|grep -q -v ^hosts ;
then
echo "script must be run from hosts dir "
echo "[hint:from unzip dir ]"
exit 3
fi
if [[ $# -eq 1 ]];
then
if echo $1 |grep -q '^uninstall' ;
then
echo "-----uninstalling---adhost---------"
echo "----restoring oringinalhost--------"
cp -v /usr/share/adhost/originalhosts /etc/hosts
echo "1">./check
exit 0
else
echo "usage ===> adhost2 uninstall "
echo " adhost -h <custom host> "
exit 4
fi
fi
if [[ $# -eq 2 ]];
then
if [ "$1" = "-h" ]
then
file=$2
flag=1
echo "=====>>using custom host list $2 "
printf "\n\n"
else
echo "usage ===> adhost2 uninstall "
echo " adhost -h <custom host> "
exit 4
fi
fi
if [[ $(cat ./check ) -eq 1 ]];
then
echo 0 >./check
startup
fi
if ((flag!=1));
then
file=/usr/share/adhost/adhost2
fi
stat=0
tot=$(cat $file 2>/dev/null |wc -l 2>/dev/null)
i=1
echo '===backing up hosts===='
cp -v /etc/hosts /usr/share/adhost/backhost
echo "" >/etc/hosts
if [ -e "$file" ]
then
while IFS= read -r line
do
trap cleanexit SIGINT
echo "---------($i/$tot)---------"
stat=$(curl -s --head -w %{http_code} $line -o /dev/null)
if echo $stat |grep ^2 >/dev/null
then
echo " updateing host $line "
curl --progress-bar -# $line >>/etc/hosts
else
echo "host domain dont exists status=$stat "
fi
let 'i=i+1'
done < $file
else
echo "$file dont exists"
cat /usr/share/adhost/backhost > /etc/hosts
fi