Auto update Pi-hole lists

One way of updating the lists on a Pi-hole system is to edit the /etc/cron.d/pihole file.

However there is another way. You can use the php script “/home/pi/listupdate.py” below

#!/usr/bin/env python
# run this directly on the pihole in cron:
# 00 07 * * * /usr/bin/python3 /home/pi/listupdate.py

import json
import urllib
import csv
from time import strftime
from urllib.request import urlopen
from subprocess import call

# set date
tdate = strftime("%d.%m.%Y")
# dns requests and blocked domains
ddata = urlopen('http://127.0.0.1/admin/api.php').read()
dbody = ddata.decode('utf-8')
ddata = json.loads(dbody)
queries = ddata['dns_queries_today']
blocked = ddata['ads_blocked_today']

# before update
bdata = urlopen('http://127.0.0.1/admin/api.php').read()
bbody = bdata.decode('utf-8')
bdata = json.loads(bbody)
before = bdata['domains_being_blocked']

# listupdate
call(["/usr/local/bin/pihole", "-g"])

# after update
adata = urlopen('http://127.0.0.1/admin/api.php').read()
abody = adata.decode('utf-8')
adata = json.loads(abody)
after = adata['domains_being_blocked']
domains_change = after - before

# append csv
outrow = [tdate,queries,blocked,before,after,domains_change]
with open(r'/home/pi/domainchange.csv', 'a') as f:
writer = csv.writer(f)
writer.writerow(outrow)

Don´t forget to add the script to the crontab (crontab -e).

Visitor Score
[Total: 0 Average: 0]