Advertisement
corrosiontears

Script Adblock para Linux Hosts (Light) [Atualizado]

Aug 14th, 2014
661
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 2.85 KB | None | 0 0
  1. #!/bin/bash
  2. # Init
  3. FILE="/tmp/out.$$"
  4. GREP="/bin/grep"
  5.  
  6. ## Script Original Encontrado no Fórum Kubuntu. Todos os créditos ao verdadeiro autor:
  7. ## https://www.kubuntuforums.net/showthread.php?56419-Script-to-automate-building-an-adblocking-hosts-file
  8. ## Modificado para funcionar como uma tarefa do sistema:
  9. ## http://pastebin.com/u/corrosiontears
  10.  
  11. # Make sure only root can run our script
  12. if [ "$(id -u)" != "0" ]; then
  13.    echo "This script must be run as root" 1>&2
  14.    exit 1
  15. fi
  16.  
  17. # If this is our first run, save a copy of the system's original hosts file and set to read-only for safety
  18. if [ ! -f ~/hosts-system ]
  19. then
  20.  echo "Saving copy of system's original hosts file..."
  21.  cp /etc/hosts ~/hosts-system
  22.  chmod 444 ~/hosts-system
  23. fi
  24.  
  25. # Perform work in temporary files
  26. temphosts1=$(mktemp)
  27. temphosts2=$(mktemp)
  28.  
  29. # Obtain various hosts files and merge into one
  30. echo "Downloading ad-blocking hosts files..."
  31. wget -nv -O - http://winhelp2002.mvps.org/hosts.txt >> $temphosts1
  32. wget -nv -O - http://hosts-file.net/ad_servers.asp >> $temphosts1
  33. wget -nv -O - http://someonewhocares.org/hosts/hosts >> $temphosts1
  34. wget -nv -O - "http://pgl.yoyo.org/adservers/serverlist.php?hostformat=hosts&showintro=0&mimetype=plaintext" >> $temphosts1
  35.  
  36. # ZeusTracker Experimental:
  37. wget -nv -O - "https://zeustracker.abuse.ch/blocklist.php?download=baddomains" >> $temphosts1
  38.  
  39. # Do some work on the file:
  40. # 1. Remove MS-DOS carriage returns
  41. # 2. Delete all lines that don't begin with 127.0.0.1
  42. # 3. Delete any lines containing the word localhost because we'll obtain that from the original hosts file
  43. # 4. Replace 127.0.0.1 with 0.0.0.0 because then we don't have to wait for the resolver to fail
  44. # 5. Scrunch extraneous spaces separating address from name into a single tab
  45. # 6. Delete any comments on lines
  46. # 7. Clean up leftover trailing blanks
  47. # Pass all this through sort with the unique flag to remove duplicates and save the result
  48. echo "Parsing, cleaning, de-duplicating, sorting..."
  49. 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
  50.  
  51. # Combine system hosts with adblocks
  52. echo Merging with original system hosts...
  53. echo -e "\n# Ad blocking hosts generated "$(date) | cat ~/hosts-system - $temphosts2 > ~/hosts-block
  54.  
  55. # Create a new hosts file [Edited]
  56. echo Creating new hosts file...
  57. cp -r ~/hosts-block /etc/hosts
  58.  
  59. # Clean up temp files and remind user to copy new file
  60. echo "Cleaning up..."
  61. rm $temphosts1 $temphosts2
  62. echo "Done."
  63.  
  64. echo
  65. echo "New file created and added to /etc/hosts"
  66. echo "Now enjoy your system without adds ;)"
  67.  
  68. echo
  69. echo "You can always restore your original hosts file with this command:"
  70. echo " sudo cp ~/hosts-system /etc/hosts"
  71. echo "so don't delete that file! (It's saved read-only for your protection.)"
  72. echo
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement