hostblockingscript.sh 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. #!/bin/bash
  2. # Credits to "Ben" (Administrator) from CupOfLinux.
  3. # check for root rights
  4. if [ ! $(whoami) == root ]; then
  5. echo "Please run this script with root-rights!"
  6. echo "su -c \"sh hostblockingscript.sh\" root"
  7. echo "sudo ./hostblockingscript.sh"
  8. exit 1;
  9. fi
  10. # backup "/etc/hosts"
  11. if [ ! -f /etc/hosts.BAK ]; then
  12. cp /etc/hosts /etc/hosts.BAK
  13. fi
  14. # counting blocked domains
  15. Blockeddomainsold=$(cat /etc/hosts | grep "0.0.0.0" | wc -l)
  16. # temporary files to safe blocked domains
  17. Selfdestructinghosts=$(mktemp)
  18. Selfdestructinghosts2=$(mktemp)
  19. # User-output
  20. echo ""
  21. echo "[ HOSTBLOCKINGSCRIPT ]"
  22. echo "https://notabug.org/jayvii/Scripts/src/master/Adblocking"
  23. echo ""
  24. echo "##########"
  25. # download lists
  26. echo "Getting (outdated) DisconnectMe-list..."
  27. wget -q -O - https://raw.githubusercontent.com/disconnectme/disconnect/b27abbf033c6f80f157fe9d98cb767c87065fbf4/firefox/content/disconnect.safariextension/opera/chrome/scripts/data.js >> $Selfdestructinghosts
  28. echo "Getting Adblock Easy-list..."
  29. wget -q -O - https://easylist-downloads.adblockplus.org/easylist.txt >> $Selfdestructinghosts
  30. echo "Getting Adblock EasyPrivacy-list..."
  31. wget -q -O - https://easylist-downloads.adblockplus.org/easyprivacy.txt >> $Selfdestructinghosts
  32. echo "Getting Adblock AntiAdblock-list..."
  33. wget -q -O - https://easylist-downloads.adblockplus.org/antiadblockfilters.txt >> $Selfdestructinghosts
  34. echo "Getting Adblock MalewareDomain-list..."
  35. wget -q -O - https://easylist-downloads.adblockplus.org/malwaredomains_full.txt >> $Selfdestructinghosts
  36. echo "Getting Adblock FanboyAnnoyance-list..."
  37. wget -q -O - https://easylist-downloads.adblockplus.org/fanboy-annoyance.txt >> $Selfdestructinghosts
  38. echo "Getting FanboySocial-list..."
  39. wget -q -O - https://easylist-downloads.adblockplus.org/fanboy-social.txt >> $Selfdestructinghosts
  40. echo "Getting Winhelp2002 Hosts-list..."
  41. wget -q -O - http://winhelp2002.mvps.org/hosts.txt >> $Selfdestructinghosts
  42. echo "Getting HostsFile AdServer-list..."
  43. wget -q -O - http://hosts-file.net/ad_servers.asp >> $Selfdestructinghosts
  44. echo "Getting SomeoneWhoCares Hosts-file..."
  45. wget -q -O - http://someonewhocares.org/hosts/hosts >> $Selfdestructinghosts
  46. echo "Getting NoTrack Trackers-list..."
  47. wget -q -O - https://raw.githubusercontent.com/quidsup/notrack/master/trackers.txt >> $Selfdestructinghosts
  48. echo "Getting NoTrack MaliciousSites-list..."
  49. wget -q -O - https://raw.githubusercontent.com/quidsup/notrack/master/malicious-sites.txt >> $Selfdestructinghosts
  50. echo "Getting MalewareDomainList Hosts-list..."
  51. wget -q -O - http://www.malwaredomainlist.com/hostslist/hosts.txt >> $Selfdestructinghosts
  52. echo "Getting Spam404 Hosts-list..."
  53. wget -q -O - https://raw.githubusercontent.com/Dawsey21/Lists/master/adblock-list.txt >> $Selfdestructinghosts
  54. # https://raw.githubusercontent.com/disconnectme/disconnect-tracking-protection/master/services.json
  55. # https://s3.amazonaws.com/lists.disconnect.me/entitylist.json
  56. sed -e 's/\r//' -e '/^0.0.0.0/!d' -e '/localhost/d' -e 's/0.0.0.0/0.0.0.0/' -e 's/ \+/\t/' -e 's/#.*$//' -e 's/[ \t]*$//' < $Selfdestructinghosts | sort -u > $Selfdestructinghosts2
  57. # create a master hosts file
  58. echo -e "\n#Hostslist created "$(date) | cat /etc/hosts.BAK - $Selfdestructinghosts2 > /etc/hosts.NEW
  59. # cleaning
  60. rm -rf $Selfdestructinghosts $Selfdestructinghosts2
  61. # replace current /etc/hosts with our new one.
  62. cp /etc/hosts.NEW /etc/hosts
  63. # counting blocked domains
  64. Blockeddomainsnew=$(cat /etc/hosts | grep "0.0.0.0" | wc -l)
  65. # final output for user
  66. if [ "$1" == "libnotify" ]; then
  67. notify-send "HostBlockingScript" "Done! Replaced $Blockeddomainsold with $Blockeddomainsnew blocks." -t 0
  68. else
  69. echo "##########"
  70. echo ""
  71. echo "Done!"
  72. echo "Previously blocked domains: $Blockeddomainsold"
  73. echo "Currently blocked domains: $Blockeddomainsnew"
  74. fi