-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbloquear-sites.sh
385 lines (213 loc) · 7.36 KB
/
bloquear-sites.sh
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
#!/bin/bash
#
# Autor: Fernando Souza - https://www.youtube.com/@fernandosuporte/
# Data: 17/11/2024 as 12:48:45
# Atualização em: https://github.com/tuxslack/scripts
# Script: bloquear-sites.sh
# Versão: 0.1
# Bloqueie sites indesejados com o arquivo /etc/hosts.
#
#
# Data da atualização:
#
# Licença: GPL - https://www.gnu.org/
#
# https://plus.diolinux.com.br/t/bloqueie-sites-indesejados-com-o-arquivo-etc-hosts/68870
# https://github.com/StevenBlack/hosts
# https://hblock.molinero.dev/
# ----------------------------------------------------------------------------------------
# Cores para formatação da saída dos comandos
RED='\e[1;31m'
GREEN='\e[1;32m'
YELLOW='\e[1;33m'
NC='\e[0m' # sem cor
arquivo="/etc/hosts"
# arquivo="hosts"
# Bloqueando sites
# 0.0.0.0 diolinux.com.br
# ou
# 127.0.0.1 diolinux.com.br
clear
# ----------------------------------------------------------------------------------------
# Verificar se os programas estão instalados:
which yad 1> /dev/null 2> /dev/null || { echo "Programa yad não esta instalado." ; exit ; }
which grep 1> /dev/null 2> /dev/null || { yad --center --window-icon "$logo" --image=dialog-error --timeout="10" --no-buttons --title "Aviso" --text "Programa grep não esta instalado." --width 450 --height 100 2>/dev/null ; exit ; }
which sed 1> /dev/null 2> /dev/null || { yad --center --window-icon "$logo" --image=dialog-error --timeout="10" --no-buttons --title "Aviso" --text "Programa sed não esta instalado." --width 450 --height 100 2>/dev/null ; exit ; }
which wget 1> /dev/null 2> /dev/null || { yad --center --window-icon "$logo" --image=dialog-error --timeout="10" --no-buttons --title "Aviso" --text "Programa wget não esta instalado." --width 450 --height 100 2>/dev/null ; exit ; }
# ----------------------------------------------------------------------------------------
# Função verificar_root
verificar_root(){
if [ "$(id -u)" != "0" ]; then
# echo -e "${RED}\nVocê deve executar este script como Root! \n ${NC}"
yad \
--center \
--title='Aviso' \
--window-icon "$logo" \
--image=dialog-error \
--text='\n\nVocê deve executar este script como Root!\n\n' \
--timeout="10" \
--no-buttons \
2>/dev/null
exit
else
echo -e "\n${GREEN}Verificação de Root [OK] ${NC}\n"
sleep 2
fi
}
verificar_root
# ----------------------------------------------------------------------------------------
pesquisar(){
# clear
# grep - Pesquise por palavras dentro de arquivos de texto
# echo "Pesquisar por dominio Ex: www.google.com.br"
# read site
# Recebe valores e armazena em variáveis
site=$(yad \
--center \
--window-icon "$logo" \
--title="Bloqueie sites indesejados" \
--entry \
--entry-label="Pesquisar por dominio:" \
--entry-text="www.google.com.br" \
--completion \
--editable \
--button=Cancelar:1 \
--button=OK:0 \
--width="800" --height="100" \
2>/dev/null)
clear
# ----------------------------------------------------------------------------------------
# Para verificar se a variavel é nula
if [ -z "$site" ];then
clear
yad \
--center \
--title='Aviso' \
--window-icon "$logo" \
--image=dialog-error \
--text='\n\nVocê precisa informar um site no script '$(basename "$0")'...\n\n' \
--timeout="10" \
--no-buttons \
2>/dev/null
# echo 'Você precisa informar um site no script '$(basename "$0")'.'
exit
fi
# ----------------------------------------------------------------------------------------
clear
# echo -e "\n${GREEN}Site encontrado no arquivo $arquivo:${NC} \n"
grep -R "$site" "$arquivo" | yad --center --title="Site encontrado no arquivo $arquivo:" --window-icon="$logo" --text-info --fontname "mono 10" --button=Cancelar:1 --button=OK:0 --width="1100" --height="800" 2>/dev/null
if [ "$?" -eq "0" ];
then
remove
else
adicionar
fi
# https://www.mundoubuntu.com.br/dicas/comandos/306-grep-pesquise-por-palavras-dentro-de-arquivos-de-texto
}
# ----------------------------------------------------------------------------------------
adicionar(){
clear
# echo -e "\nDeseja bloquear o site $site ? [s/n] "
# read resposta
yad \
--center \
--title="Bloqueie sites indesejados" \
--text "Deseja bloquear o site $site?" \
--button="Não":1 --button="Sim":0
# if [[ $resposta = +(S|s) ]];then # Valida a resposta com "S" e com "s"
if [ "$?" == "0" ];
then
clear
echo "
Bloqueando o site $site...
"
echo "0.0.0.0 $site" >> "$arquivo"
else
clear
exit
fi
# https://www.vivaolinux.com.br/topico/Shell-Script/Loop-no-Sn
}
# ----------------------------------------------------------------------------------------
remove(){
# clear
echo "
Buscando $site no arquivo $arquivo...
"
grep -R "$site" "$arquivo" || exit
# echo -e "\nDeseja remove o bloqueio do site $site ? [s/n]"
# read resposta
yad \
--center \
--title="Bloqueie sites indesejados" \
--text "Deseja remove o bloqueio do site $site ?" \
--button="Não":1 --button="Sim":0
# if [[ $resposta = +(S|s) ]];then # Valida a resposta com "S" e com "s"
if [ "$?" == "0" ];
then
clear
echo "
Removendo o bloqueio do site $site...
"
sed -i '/'$site'/d' "$arquivo"
else
clear
exit
fi
# https://www.vivaolinux.com.br/topico/Sed-Awk-ER-Manipulacao-de-Textos-Strings/Apagar-linha-com-palavra
# https://www.thelinuxrain.org/articles/the-buttons-of-yad
# https://yad-guide.ingk.se/examples/examples.html
# https://github.com/v1cont/yad/issues/107
}
# ----------------------------------------------------------------------------------------
gerar(){
# clear
# ----------------------------------------------------------------------------------------
echo "
Testando conexão com à internet...
"
if ! ping -c 1 www.google.com.br -q &> /dev/null; then
yad --center --window-icon "$logo" --image=dialog-error --timeout="10" --no-buttons --title "ERRO" --text "Seu sistema não tem conexão com à internet. Verifique os cabos e o modem." --width="600" --height="100" 2>/dev/null
echo -e "${RED}[ERRO] - Seu sistema não tem conexão com à internet. Verifique os cabos e o modem.\n ${NC}"
sleep 10
exit 1
else
echo -e "${GREEN}[VERIFICADO] - Conexão com à internet funcionando normalmente. ${NC}"
sleep 2
fi
# ----------------------------------------------------------------------------------------
# echo -e "\nDeseja gerar um novo arquivo $arquivo? [s/n]"
# read resposta
yad \
--center \
--title="Bloqueie sites indesejados" \
--text "Deseja gerar um novo arquivo $arquivo?" \
--button="Não":1 --button="Sim":0
# if [[ $resposta = +(S|s) ]];then # Valida a resposta com "S" e com "s"
if [ "$?" == "0" ];
then
echo "
Gerando um novo arquivo $arquivo...
"
wget -O $arquivo -c "https://hblock.molinero.dev/hosts"
else
clear
exit
fi
# https://hblock.molinero.dev/
}
# ----------------------------------------------------------------------------------------
# Verificar se o arquivo existe
if [ -e "$arquivo" ] ; then
echo -e "\n${GREEN}Arquivo $arquivo encontrado [OK] ${NC}\n"
sleep 1
clear
pesquisar
else
echo -e "\n${RED}Arquivo $arquivo não encontrado... ${NC}\n"
sleep 1
clear
gerar
fi
# ----------------------------------------------------------------------------------------
exit 0