Well I’m stumped. All of the commands below work in the terminal and return the same result, but as soon as I try to add any of them into my python file it causes it to fail. I can’t see what I’m missing so I’m dumping everything here and will try to come back to it later with fresh eyes.
These commands all work, both as root and dietpi user, and with all ip address versions:
Each of these commands works and returns the same result when I run them dierctly in the terminal:
curl -f -s "http://127.0.0.1/admin/api.php?summaryRaw&auth=69xx"
curl -f -s "http://192.168.1.100/admin/api.php?summaryRaw&auth=69xx" | jq .dns_queries_today
echo ">stats>quit" | nc 127.0.0.1 4711
echo ">stats>quit" | nc 127.0.0.1 4711 | grep dns_queries_today | cut -d\ -f2
echo -e ">stats>quit" | nc 127.0.0.1 4711 | grep dns_queries_today | cut -d\ -f2
In addition I also ran the same things in python and got the same results, again mixing/matching everything above:
python3
import json
import requests
api_url = 'http://127.0.0.1/admin/api.php?summaryRaw&auth=69xx
r = requests. Get(api_url)
data = json.loads(r.text)
Queries = data['dns_queries_today']
print(Queries)
python3
import subprocess
cmd = "curl -f -s \"http://127.0.0.1/admin/api.php?summaryRaw" + "&auth=$(grep -oPi \"(?<=WEBPASSWORD\=).+\" /etc/pihole/setupVars.conf)\" | jq .dns_queries_today"
Queries = subprocess.check_output(cmd, shell = True ).decode("utf-8", errors="ignore").strip()
print(Queries)
Here’re my python tests just to make sure I’m getting the syntax correct. Again I tried every combination I could think of using single quotes/double quotes/backslashes to escape quotes where necessary.
list of relevant things imported:
import subprocess
import os
import json
import requests
Examples:
cmd = "curl -f -s 'http://127.0.0.1/admin/api.php?summaryRaw&auth=94xx' | jq .dns_queries_today"
cmd = "curl -f -s \"http://127.0.0.1/admin/api.php?summaryRaw" + "&auth=$(grep -oPi \"(?<=WEBPASSWORD\=).+\" /etc/pihole/setupVars.conf)\" | jq .dns_queries_today"
cmd = "os.system('curl -f -s \"http://127.0.0.1/admin/api.php?summaryRaw&auth=94xx" | jq .dns_queries_today')"
cmd = "echo -e \">stats>quit\" | nc 127.0.0.1 4711 | grep dns_queries_today | cut -d\ -f2"
Queries = subprocess.check_output(cmd, shell = True ).decode("utf-8", errors="ignore").strip()
try:
api_url = 'http://127.0.0.1/admin/api.php?summaryRaw&auth=94xx'
r = requests.get(api_url) or r = requests.get(api_url, headers={"Content-Type":"text"}) or r = requests.get(api_url, headers={"Content-Type":"application/json"})
data = json.loads(r.text)
Queries = data['dns_queries_today']
except KeyError:
time.sleep(1)
continue
This is my working file with my tests on row 195-209:
bakebit_nanohat_oled.txt (11.2 KB)