Dnsmasq去广告自动生成规则Shell脚本

原创 楚盟  2018-01-31 02:21  阅读 610 views 次

这个脚本会在/etc/dnsmasq.d/目录下自动生成一个adblock.conf屏蔽广告规则列表,Dnsmasq通过读取这个adblock.conf文件来实现屏蔽广告的目的;

如何使用这个去广告规则看这篇笔记能知道具体方法:《Centos7搭建Dnsmasq自建DNS》

脚本下载地址:wget https://www.5yun.org/wp-content/code/15257.sh


#!/bin/bash
TMP_ADBLOCK=/tmp/dnsmasq_adblock_unsorted
ADBLOCK=/etc/dnsmasq.d/adblock.conf
TMP_HOST=/tmp/dnsmasq_adblock_hosts
TMP_HOST_SWP=/tmp/dnsmasq_adblock_hosts_swp
HOST=/etc/dnsmasq_addn_hosts
if [ -f ${TMP_HOST} ]
then
	rm ${TMP_HOST} -rf
elif [ -f ${TMP_HOST_SWAP} ]
then
	rm ${TMP_HOST_SWAP} -rf
fi
rm /etc/dnsmasq.d/adblock.conf
for  URL in	\
	"https://easylist-downloads.adblockplus.org/easylistchina+easylist.txt" \
	"https://easylist.to/easylist/easylist.txt"
do 
wget -4 --no-check-certificate -qO - "${URL}" | grep ^\|\|[^\*]*\^$ |sed -e 's:||:address\=\/:' -e 's:\^:/127\.0\.0\.1:' >> ${TMP_ADBLOCK}
#wget -4 --no-check-certificate -qO- "${URL}" | grep -v -e "^#" -e "^\s*$" -e "localhost" -e "^;" -e "^@" -e "^:" -e "^[a-zA-Z]" \
#| sed -E -e "s/#.*$//" -e "s/[[:space:]]*//g" -e "/^$/d" \
#-e "s/^127.0.0.1/server=\/./" -e "s/0.0.0.0/server=\/./" -e "/^[0-9].*$/d" -e "s/$/\/127.0.0.0/" \
#| tr -d "\r" >> ${TMP_ADBLOCK}
done
sort ${TMP_ADBLOCK} | uniq > ${ADBLOCK}
rm  -r ${TMP_ADBLOCK}
cat /etc/hosts > ${HOST}
for URL_H in	\
	"https://raw.githubusercontent.com/vokins/yhosts/master/hosts.txt" \
	"http://www.malwaredomainlist.com/hostslist/hosts.txt" \
	"https://raw.githubusercontent.com/StevenBlack/hosts/master/hosts"
do 
wget --no-check-certificate -qO - ${URL_H}  >> ${TMP_HOST}
done
sed -i s/0\.0\.0\.0/127\.0\.0\.1/g  ${TMP_HOST}
egrep ^127.0.0.1  ${TMP_HOST} | egrep -v *paypal* | egrep -v tv.sohu.com | sort | uniq >  ${TMP_HOST_SWP}
dos2unix	${TMP_HOST_SWP}
#合并hosts缓存
sort ${TMP_HOST_SWP} | uniq  > ${HOST}
#sed -i '/*paypal*/d' /etc/dnsmasq_addn_hosts
# 删除hosts缓存
rm -r ${TMP_HOST}
rm -r ${TMP_HOST_SWP}
systemctl restart dnsmasq
本文地址:https://www.5yun.org/15257.html
关注我们:请关注一下我们的微信公众号:扫描二维码楚盟网的公众号,公众号:chubin5yun
版权声明:本文为原创文章,版权归 楚盟 所有,欢迎分享本文,转载请保留出处!