ubuntuusers.de

Script zum autom. Update von /etc/hosts will nicht

Status: Ungelöst | Ubuntu-Version: Xubuntu 13.04 (Raring Ringtail)
Antworten |

Thorsten_Reinbold Team-Icon

Anmeldungsdatum:
10. Juli 2006

Beiträge: 4784

Hallo,

ich verwende /etc/hosts zum blockieren von Werbung und anderem Mist. Da es etwas mühsam ist, die Datei manuell zu pflegen, habe ich die Aufgabe an ein Script delegiert.

Das klappte bis vor Kurzem auch recht gut, nun produziert das Script allerdings nur noch eine (bis auf den korrekten Header) leere hosts-Datei, die Daten werden zwar korrekt heruntergeladen, aber nicht übernommen.

Hier mal das Script:

 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
#!/bin/bash
#

if [ `whoami` != "root" ]; then
 echo "Insufficient permissions. Run as root."
 exit 0
fi

HOSTSDIR="/etc/hosts.d/"
TMPDIR="${HOSTSDIR}tmp/"
OUTPUTFILE="/etc/hosts"

if [ ! -d "${TMPDIR}" ]; then
 mkdir -p ${TMPDIR}
fi

if [ ! -f "${HOSTSDIR}hosts.local" ]; then
 echo "You need to create "${HOSTSDIR}hosts.local" containing the hosts you wish to keep!"
 echo -e "fe00::0		ip6-localnet" > "${HOSTSDIR}hosts.local"
 echo -e "ff00::0		ip6-mcastprefix" >> "${HOSTSDIR}hosts.local"
 echo -e "ff02::1		ip6-allnodes" >> "${HOSTSDIR}hosts.local"
 echo -e "ff02::2		ip6-allrouters" >> "${HOSTSDIR}hosts.local"
 echo -e "ff02::3		ip6-allhosts" >> "${HOSTSDIR}hosts.local"
fi

if [ ! -f "${HOSTSDIR}hosts.whitelist" ]; then
 echo "You need to create "${HOSTSDIR}hosts.whitelist" containing the hosts you want to discard from the lists!"
 echo -e "#" > "${HOSTSDIR}hosts.whitelist"
fi

echo Downloading hosts.hphosts...
curl --compressed -v -# -o "${TMPDIR}hosts.hphosts" "http://hosts-file.net/download/hosts.txt"
echo Downloading hosts.adservers...
curl --compressed -v -# -o "${TMPDIR}hosts.adservers" "http://hosts-file.net/ad_servers.asp"
echo Downloading hosts.partial...
curl --compressed -v -# -o "${TMPDIR}hosts.partial" "http://hosts-file.net/hphosts-partial.asp"
echo Downloading hosts.mvps...
curl --compressed -v -# -o "${TMPDIR}hosts.mvps" "http://winhelp2002.mvps.org/hosts.txt"
echo Downloading hosts.minenu...
curl --compressed -v -# -o "${TMPDIR}hosts.minenu" "http://hostsfile.mine.nu/Hosts"
echo Downloading hosts.malware...
curl --compressed -v -# -o "${TMPDIR}hosts.malware" "http://www.malwaredomainlist.com/hostslist/hosts.txt"
echo Downloading hosts.sowc...
curl --compressed -v -# -o "${TMPDIR}hosts.sowc" "http://someonewhocares.org/hosts/hosts"
echo Downloading hosts.ismeh...
curl --compressed -v -# -o "${TMPDIR}hosts.ismeh" "http://www.ismeh.com/HOSTS"
echo Downloading hosts.securemecca...
curl --compressed -v -# -o "${TMPDIR}hosts.securemecca" "http://www.securemecca.com/Downloads/hosts.txt"
echo Downloading hosts.yoyo...
curl --compressed -v -# -o "${TMPDIR}hosts.yoyo" "http://pgl.yoyo.org/as/serverlist.php?hostformat=hosts&showintro=0&mimetype=plaintext"

# hosts header
cat "${HOSTSDIR}hosts.local"  > "${OUTPUTFILE}"

cat "${TMPDIR}hosts.hphosts" \
    "${TMPDIR}hosts.adservers" \
    "${TMPDIR}hosts.partial" \
    "${TMPDIR}hosts.mvps" \
    "${TMPDIR}hosts.minenu" \
    "${TMPDIR}hosts.malware" \
    "${TMPDIR}hosts.sowc" \
    "${TMPDIR}hosts.ismeh" \
    "${TMPDIR}hosts.securemecca" \
    "${TMPDIR}hosts.yoyo" | \
sed -e 's/	/ /g' | \
grep -e '127.0.0.1 ' | \
tr -d "\r" | \
tr -s [:space:] | \
cut -d " " -f -2 | \
sort | uniq | \
grep -vf "${HOSTSDIR}hosts.whitelist" | \
sed -e 's/127.0.0.1 /0.0.0.0	/g' >> "${OUTPUTFILE}"

echo -e "Done!"

Möglicherweise sieht ja Jemand den Fehler, ich konnte soweit Keinen erkennen.

theinlein

Anmeldungsdatum:
29. Dezember 2007

Beiträge: 1279

Hi!

Nun, du sagts, die Downloads funktonieren - heißt wohl, die curls klappen alle nur die anschließende Filterungen nicht?

Da würde ich doch Schritt für Schritt eine Pipe nach der anderen mal auftrennen und in eine Datei umleiten, um zu schauen wo es schief geht.

Von hier aus der Ferne das im Kopf zu haben, ohne die Inhalter der Dateien zu kennen ... zu umständlich und ineffizient.

Die sed, tr und greps hätte ich mittel awk gemacht, da passt das alles in ein Werkzeug.

Aber es hat ja schon mal funktioniert.

Thorsten_Reinbold Team-Icon

(Themenstarter)

Anmeldungsdatum:
10. Juli 2006

Beiträge: 4784

theinlein schrieb:

Da würde ich doch Schritt für Schritt eine Pipe nach der anderen mal auftrennen und in eine Datei umleiten, um zu schauen wo es schief geht.

Das mache ich gerade. Schon mal klar ist, das es irgendwo in diesem sed, tr und grep-Block hakt. Das hat sich durch auskommentieren bestätigt.

Edit sagt: es ist die Zeile

1
grep -vf "${HOSTSDIR}hosts.whitelist" | \

Ich schau mir erst mal die Datei an. Evtl. hat sich dort ein Fehler eingeschlichen...

Thorsten_Reinbold Team-Icon

(Themenstarter)

Anmeldungsdatum:
10. Juli 2006

Beiträge: 4784

Nein, die Datei ist ok, aber es hakt definitiv an dieser einen grep-Zeile. Verstehe ich jetzt nicht ganz. Eigentlich sollen damit die URLs aus der Whitelist in der hosts entfernt werden, was bisher auch geklappt hat.

track

Avatar von track

Anmeldungsdatum:
26. Juni 2008

Beiträge: 7174

Wohnort: Wolfen (S-A)

Kannst Du mal so eine Rohdatei (die Ausgabe von dem cat) posten ? - die Ausgabe muss ja am Ende zu /etc/hosts passen.

Das müsste doch zu knacken sein ... (ich wollte nur jetzt nicht alles selber rekonstruieren)

track

Thorsten_Reinbold Team-Icon

(Themenstarter)

Anmeldungsdatum:
10. Juli 2006

Beiträge: 4784

Ich mache jetzt nur mal einen Ausschnitt, sonst wird das zu groß:

Beispiel mvps hosts (Original):

# This MVPS HOSTS file is a free download from:            #
# http://winhelp2002.mvps.org/hosts.htm                    #
#                                                          #
# Notes: The Operating System does not read the "#" symbol #
# You can create your own notes, after the # symbol        #
# This *must* be the first line: 127.0.0.1     localhost   #
#                                                          #
#**********************************************************#
# ---------------- Updated: July-08-2013 ----------------- #
#**********************************************************#
#                                                          #
# Disclaimer: this file is free to use for personal use    #
# only. Furthermore it is NOT permitted to copy any of the #
# contents or host on any other site without permission or #
# meeting the full criteria of the below license terms.    #
#                                                          #
# This work is licensed under the Creative Commons         #
# Attribution-NonCommercial-ShareAlike License.            #
# http://creativecommons.org/licenses/by-nc-sa/3.0/        #
#                                                          #
# Entries with comments are all searchable via Google.     #

127.0.0.1  localhost

::1  localhost #[IPv6]

# [Start of entries generated by MVPS HOSTS]
#
# [Misc A - Z]
127.0.0.1  fr.a2dfp.net
127.0.0.1  m.fr.a2dfp.net
127.0.0.1  ad.a8.net
127.0.0.1  asy.a8ww.net
127.0.0.1  abcstats.com
127.0.0.1  a.abv.bg
127.0.0.1  adserver.abv.bg
127.0.0.1  adv.abv.bg
127.0.0.1  bimg.abv.bg
127.0.0.1  ca.abv.bg
127.0.0.1  www2.a-counter.kiev.ua
127.0.0.1  track.acclaimnetwork.com
127.0.0.1  accuserveadsystem.com
127.0.0.1  www.accuserveadsystem.com
127.0.0.1  achmedia.com
127.0.0.1  csh.actiondesk.com
127.0.0.1  www.activemeter.com #[Tracking.Cookie]
127.0.0.1  ads.activepower.net
127.0.0.1  stat.active24stats.nl #[Tracking.Cookie]
127.0.0.1  cms.ad2click.nl
127.0.0.1  ad2games.com
127.0.0.1  ads.ad2games.com
127.0.0.1  content.ad20.net
127.0.0.1  core.ad20.net
127.0.0.1  banner.ad.nu
127.0.0.1  cl21.v4.adaction.se
127.0.0.1  adadvisor.net
127.0.0.1  tag1.adaptiveads.com
127.0.0.1  www.adbanner.ro
127.0.0.1  wad.adbasket.net
127.0.0.1  ad.pop1.adbn.ru
127.0.0.1  ad.top1.adbn.ru
127.0.0.1  ad.rich1.adbn.ru
127.0.0.1  james.adbutler.de #[Tracking.Cookie]
127.0.0.1  www.adbutler.de
127.0.0.1  cs.adcastplus.net
127.0.0.1  www.adchimp.com
127.0.0.1  engine.adclick.lv
127.0.0.1  show.adclick.lv
127.0.0.1  static.adclick.lv
127.0.0.1  www.adclick.lv
127.0.0.1  ad-clix.com

Ausschnitt (nach dem Script):

127.0.0.1	localhost.localdomain	localhost	hostname
::1		localhost.localdomain	localhost	hostname
0.0.0.0	fr.a2dfp.net
0.0.0.0	m.fr.a2dfp.net
0.0.0.0	ad.a8.net
0.0.0.0	asy.a8ww.net
0.0.0.0	abcstats.com
0.0.0.0	a.abv.bg
0.0.0.0	adserver.abv.bg
0.0.0.0	adv.abv.bg
0.0.0.0	bimg.abv.bg
0.0.0.0	ca.abv.bg
0.0.0.0	www2.a-counter.kiev.ua
0.0.0.0	track.acclaimnetwork.com
0.0.0.0	accuserveadsystem.com
0.0.0.0	www.accuserveadsystem.com
0.0.0.0	achmedia.com
0.0.0.0	csh.actiondesk.com
0.0.0.0	www.activemeter.com
0.0.0.0	ads.activepower.net
0.0.0.0	stat.active24stats.nl
0.0.0.0	cms.ad2click.nl
0.0.0.0	ad2games.com
0.0.0.0	ads.ad2games.com
0.0.0.0	content.ad20.net
0.0.0.0	core.ad20.net
0.0.0.0	banner.ad.nu
0.0.0.0	cl21.v4.adaction.se
0.0.0.0	adadvisor.net
0.0.0.0	tag1.adaptiveads.com
0.0.0.0	www.adbanner.ro
0.0.0.0	wad.adbasket.net
0.0.0.0	ad.pop1.adbn.ru
0.0.0.0	ad.top1.adbn.ru
0.0.0.0	ad.rich1.adbn.ru
0.0.0.0	james.adbutler.de
0.0.0.0	www.adbutler.de
0.0.0.0	cs.adcastplus.net
0.0.0.0	www.adchimp.com
0.0.0.0	engine.adclick.lv
0.0.0.0	show.adclick.lv
0.0.0.0	static.adclick.lv
0.0.0.0	www.adclick.lv
0.0.0.0	ad-clix.com

Das klappt auch soweit. Nur die eine grep-Zeile will nicht, die soll, mit grep -vf aus der Datei /etc/hosts.d/hosts.whitelist sämtlich dort eingetragenen Adressen wieder aus der hosts löschen. www.taz.de ist z.B. normalerweise auf einer der Blocklisten, warum auch immer.

track

Avatar von track

Anmeldungsdatum:
26. Juni 2008

Beiträge: 7174

Wohnort: Wolfen (S-A)

Ok, diese Filterkette ist auch in meinen Augen mindestens eine Quelle der Verwirrung, wenn nicht eine Fehlerquelle.
Darum würde auch ich die Formatierung mit einem einzigen sed- Befehl erledigen:

cat .....  |  sed -n 's/ *#.*//; s/^ *127.0.0.1 */0.0.0.0 \t/p'

was erst alle Kommentare beseitigt, und dann die Null-IP anpasst.

Die Sache mit dem Whitelist-Filter müsste dann dahinter kommen ...

Aber: wie sieht denn z.Z. Deine Whitelist-Datei aus ? - sollte darin irgendwo so etwas wie ein ".*" enthalten sein, wird natürlich die gesamte Ausgabe unterdrückt !
(probier es spaßeshalber mal aus mit einem ... | grep -v ".*" ) - in die Richtung geht jetzt mein Verdacht.

LG,

track

Thorsten_Reinbold Team-Icon

(Themenstarter)

Anmeldungsdatum:
10. Juli 2006

Beiträge: 4784

Nein, ein ".*" ist da nirgends vorhanden. Die Datei ist nach dem Muster

www.seite1.net
seite2.com
www.seite3.org

aufgebaut.

Thorsten_Reinbold Team-Icon

(Themenstarter)

Anmeldungsdatum:
10. Juli 2006

Beiträge: 4784

Das:

cat .....  |  sed -n 's/ *#.*//; s/^ *127.0.0.1 */0.0.0.0 \t/p'

funktioniert übrigens ganz gut.

Ich hab's bei mir so eingebaut:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
cat "${TMPDIR}hosts.hphosts" \
"${TMPDIR}hosts.adservers" \
"${TMPDIR}hosts.partial" \
"${TMPDIR}hosts.mvps" \
"${TMPDIR}hosts.minenu" \
"${TMPDIR}hosts.malware" \
"${TMPDIR}hosts.sowc" \
"${TMPDIR}hosts.ismeh" \
"${TMPDIR}hosts.securemecca" \
"${TMPDIR}hosts.yoyo" | \
sed -n 's/ *#.*//; s/^ *127.0.0.1 */0.0.0.0\t/p' >> "${OUTPUTFILE}"

Allerdings bleibt da noch das ein oder andere Leerzeichen am Zeilenende stehen. Wie müsste ich das abändern, um das zu verhindern?

track

Avatar von track

Anmeldungsdatum:
26. Juni 2008

Beiträge: 7174

Wohnort: Wolfen (S-A)

Thorsten Reinbold schrieb:

Allerdings bleibt da noch das ein oder andere Leerzeichen am Zeilenende stehen. Wie müsste ich das abändern, um das zu verhindern?

Einfach wegmachen. 😉 z.B. so:

cat .....  |  sed -n 's/ *#.*//; s/^ *127.0.0.1 */0.0.0.0 \t/p; s/[ \r]*$//'

Das nimmt eventuelle Win...-Zeilenenden bei der Gelegenheit gleich auch noch mit weg. (beachte den Anker $ für das Zeilenende !)

LG,

track

Thorsten_Reinbold Team-Icon

(Themenstarter)

Anmeldungsdatum:
10. Juli 2006

Beiträge: 4784

Danke für deine Mühe. Ich hab das Ganze nun noch etwas verfeinert... im Rahmen meiner Möglichkeiten. 😀

#!/bin/bash

HOSTSDIR="/etc/hosts.d/"
TMPDIR="${HOSTSDIR}tmp/"
OUTPUTFILE="/etc/hosts"

if [ ! -d "${TMPDIR}" ]; then
 mkdir -p ${TMPDIR}
fi

if [ ! -f "${HOSTSDIR}hosts.local" ]; then
 echo "You need to create "${HOSTSDIR}hosts.local" containing the hosts you wish to keep!"
fi


echo Downloading hosts.hphosts...
curl --compressed -# -o "${TMPDIR}hosts.hphosts" "http://hosts-file.net/download/hosts.txt"
echo Downloading hosts.adservers...
curl --compressed -# -o "${TMPDIR}hosts.adservers" "http://hosts-file.net/ad_servers.txt"
echo Downloading hosts.partial...
curl --compressed -# -o "${TMPDIR}hosts.partial" "http://hosts-file.net/hphosts-partial.asp"
echo Downloading hosts.mvps...
curl --compressed -# -o "${TMPDIR}hosts.mvps" "http://winhelp2002.mvps.org/hosts.txt"
echo Downloading hosts.minenu...
curl --compressed -# -o "${TMPDIR}hosts.minenu" "http://hostsfile.mine.nu/Hosts"
echo Downloading hosts.malware...
curl --compressed -# -o "${TMPDIR}hosts.malware" "http://www.malwaredomainlist.com/hostslist/hosts.txt"
echo Downloading hosts.sowc...
curl --compressed -# -o "${TMPDIR}hosts.sowc" "http://someonewhocares.org/hosts/hosts"
echo Downloading hosts.ismeh...
curl --compressed -# -o "${TMPDIR}hosts.ismeh" "http://www.ismeh.com"
echo Downloading hosts.securemecca...
curl --compressed -# -o "${TMPDIR}hosts.securemecca" "http://www.securemecca.com/Downloads/hosts.txt"
echo Downloading hosts.yoyo...
curl --compressed -# -o "${TMPDIR}hosts.yoyo" "http://pgl.yoyo.org/as/serverlist.php?hostformat=hosts&showintro=0&mimetype=plaintext"

cat "${HOSTSDIR}hosts.local"  > "${OUTPUTFILE}"

cat "${TMPDIR}hosts.hphosts" \
"${TMPDIR}hosts.adservers" \
"${TMPDIR}hosts.partial" \
"${TMPDIR}hosts.mvps" \
"${TMPDIR}hosts.minenu" \
"${TMPDIR}hosts.malware" \
"${TMPDIR}hosts.sowc" \
"${TMPDIR}hosts.ismeh" \
"${TMPDIR}hosts.securemecca" \
"${TMPDIR}hosts.yoyo" | \
sed -n 's/ *#.*//; s/^ *127.0.0.1 */0.0.0.0\t/p; s/[ \r]*$//' >> "${OUTPUTFILE}"
grep -v 'www.taz.de' "${OUTPUTFILE}" > "${TMPDIR}hosts.tmp" && mv "${TMPDIR}hosts.tmp" "${OUTPUTFILE}"

rm -f ${TMPDIR}*

echo -e "Done!"

Mit dem markiertem grep bin ich zwar nicht ganz glücklich, allerdings wird damit der Eintrag für die TAZ wieder zuverlässig entfernt. Evtl. hast du ja noch eine elegantere Idee.

Antworten |