Advertisement
corrosiontears

Script Adblock para Linux Hosts [MoAB Agressivio]

Aug 18th, 2014
752
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 3.06 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. # MoaAB Experimental:
  37. wget -nv -O - https://dl.dropboxusercontent.com/u/39421859/hosts  >> $temphosts1
  38.  
  39. # ZeusTracker Experimental:
  40. wget -nv -O - "https://zeustracker.abuse.ch/blocklist.php?download=baddomains" >> $temphosts1
  41.  
  42. # HostsFile Experimental:
  43. wget -nv -O - http://www.hostsfile.org/Downloads/hosts.txt >> $temphosts1
  44.  
  45. # Do some work on the file:
  46. # 1. Remove MS-DOS carriage returns
  47. # 2. Delete all lines that don't begin with 127.0.0.1
  48. # 3. Delete any lines containing the word localhost because we'll obtain that from the original hosts file
  49. # 4. Replace 127.0.0.1 with 0.0.0.0 because then we don't have to wait for the resolver to fail
  50. # 5. Scrunch extraneous spaces separating address from name into a single tab
  51. # 6. Delete any comments on lines
  52. # 7. Clean up leftover trailing blanks
  53. # Pass all this through sort with the unique flag to remove duplicates and save the result
  54. echo "Parsing, cleaning, de-duplicating, sorting..."
  55. 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
  56.  
  57. # Combine system hosts with adblocks
  58. echo Merging with original system hosts...
  59. echo -e "\n# Ad blocking hosts generated "$(date) | cat ~/hosts-system - $temphosts2 > ~/hosts-block
  60.  
  61. # Create a new hosts file [Edited]
  62. echo Creating new hosts file...
  63. cp -r ~/hosts-block /etc/hosts
  64.  
  65. # Clean up temp files and remind user to copy new file
  66. echo "Cleaning up..."
  67. rm $temphosts1 $temphosts2
  68. echo "Done."
  69.  
  70. echo
  71. echo "New file created and added to /etc/hosts"
  72. echo "Now enjoy your system without adds ;)"
  73.  
  74. echo
  75. echo "You can always restore your original hosts file with this command:"
  76. echo " sudo cp ~/hosts-system /etc/hosts"
  77. echo "so don't delete that file! (It's saved read-only for your protection.)"
  78. echo
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement