Advertisement
corrosiontears

Script Adblock para Linux Hosts (Original) [Antigo]

Aug 13th, 2014
590
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 2.66 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. ## Script Original Encontrado no Fórum Kubuntu. Todos os créditos ao verdadeiro autor:
  4. ## https://www.kubuntuforums.net/showthread.php?56419-Script-to-automate-building-an-adblocking-hosts-file
  5.  
  6. # If this is our first run, save a copy of the system's original hosts file and set to read-only for safety
  7. if [ ! -f ~/hosts-system ]
  8. then
  9.  echo "Saving copy of system's original hosts file..."
  10.  cp /etc/hosts ~/hosts-system
  11.  chmod 444 ~/hosts-system
  12. fi
  13.  
  14. # Perform work in temporary files
  15. temphosts1=$(mktemp)
  16. temphosts2=$(mktemp)
  17.  
  18. # Obtain various hosts files and merge into one
  19. echo "Downloading ad-blocking hosts files..."
  20. wget -nv -O - http://winhelp2002.mvps.org/hosts.txt >> $temphosts1
  21. wget -nv -O - http://hosts-file.net/ad_servers.asp >> $temphosts1
  22. wget -nv -O - http://someonewhocares.org/hosts/hosts >> $temphosts1
  23. wget -nv -O - "http://pgl.yoyo.org/adservers/serverlist.php?hostformat=hosts&showintro=0&mimetype=plaintext" >> $temphosts1
  24. #Otimizado:
  25. wget -nv -O - https://dl.dropboxusercontent.com/u/39421859/hosts >> $temphosts1
  26. wget -nv -O - http://www.malwaredomainlist.com/hostslist/hosts.txt >> $temphosts1
  27. wget -nv -O - "http://adblock.gjtech.net/?format=hostfile" >> $temphosts1
  28. wget -nv -O - http://www.hostsfile.org/Downloads/hosts.txt >> $temphosts1
  29.  
  30. # Do some work on the file:
  31. # 1. Remove MS-DOS carriage returns
  32. # 2. Delete all lines that don't begin with 127.0.0.1
  33. # 3. Delete any lines containing the word localhost because we'll obtain that from the original hosts file
  34. # 4. Replace 127.0.0.1 with 0.0.0.0 because then we don't have to wait for the resolver to fail
  35. # 5. Scrunch extraneous spaces separating address from name into a single tab
  36. # 6. Delete any comments on lines
  37. # 7. Clean up leftover trailing blanks
  38. # Pass all this through sort with the unique flag to remove duplicates and save the result
  39. echo "Parsing, cleaning, de-duplicating, sorting..."
  40. sed -e 's/\r//' -e '/^127.0.0.1/!d' -e '/localhost/d' -e 's/127.0.0.1/0.0.0.0/' -e 's/ \+/\t/' -e 's/#.*$//' -e 's/[ \t]*$//' < $temphosts1 | sort -u > $temphosts2
  41.  
  42. # Combine system hosts with adblocks
  43. echo Merging with original system hosts...
  44. echo -e "\n# Ad blocking hosts generated "$(date) | cat ~/hosts-system - $temphosts2 > ~/hosts-block
  45.  
  46. # Clean up temp files and remind user to copy new file
  47. echo "Cleaning up..."
  48. rm $temphosts1 $temphosts2
  49. echo "Done."
  50. echo
  51. echo "Copy ad-blocking hosts file with this command:"
  52. echo " sudo cp ~/hosts-block /etc/hosts"
  53. echo
  54. echo "You can always restore your original hosts file with this command:"
  55. echo " sudo cp ~/hosts-system /etc/hosts"
  56. echo "so don't delete that file! (It's saved read-only for your protection.)"
  57. echo
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement