Created
March 18, 2025 16:42
Continuously fetch DNS query stats from Pihole 6
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python3 | |
# very small script to continuously dump queries from a local network Pihole installation | |
# public domain, 2025 by Florian 'floe' Echtler <floe@butterbrot.org> | |
import requests,sys,datetime,time | |
# use password to get a session token | |
url = "http://pi.hole/api/auth" | |
payload = {"password": sys.argv[1] } | |
response = requests.request("POST", url, json=payload, verify=False) | |
session = response.json()["session"] | |
sid = session["sid"] | |
csrf = session["csrf"] | |
# start time is current timestamp | |
start = str(datetime.datetime.now().timestamp()) | |
# query payload and session token headers | |
payload = {} | |
headers = { | |
"X-FTL-SID": sid, | |
"X-FTL-CSRF": csrf | |
} | |
# main loop | |
while True: | |
# wait 1 second between queries | |
time.sleep(1) | |
# get all results since the most recent timestamp | |
url = "http://pi.hole/api/queries?from="+start | |
response = requests.request("GET", url, headers=headers, data=payload, verify=False) | |
# continue if no results available | |
queries = response.json().get("queries") | |
if not "queries" or len(queries) == 0: | |
continue | |
# last result plus small delta is the new timestamp | |
start = str(queries[0]["time"]+0.001) | |
# loop through all results | |
for q in queries: | |
print(q["time"],q["domain"],q["status"]) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment