From c7526fb606308704cf1eb176bf77e34a520afa09 Mon Sep 17 00:00:00 2001 From: James Price Date: Tue, 3 Jan 2023 10:55:06 +1100 Subject: [PATCH] Added mandatory API Token command line arguments for latest version of API (which now has mandatory authentication) See: https://pi-hole.net/blog/2022/12/21/pi-hole-ftl-v5-20-and-web-v5-18-released/#page-content and: https://github.com/pi-hole/AdminLTE/pull/2411 --- README.md | 14 +++++++++++--- main.py | 8 +++++++- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 1057b20..438c2d7 100644 --- a/README.md +++ b/README.md @@ -30,7 +30,15 @@ python ./inky-pihole/main.py ``` ## Options -All command line optins are optional and the default values when left out are detailed below. + +* `-t`, `--token` + Pihole API Token (_Required_) + e.g.: `--token "4e0a518a7e2cc8e8b0520b91c8f0156709167ce9265298add8fa45d1d933f99e"` + Navigate to the settings admin page to retrieve your API token: + http://pihole/admin/settings.php?tab=api + and click the `Show API Token` button to view your API key. + +All other command line options are optional, and the default values when left out are detailed below. * `-a` , `--apihosts` A comma separated list of your Pi-Hole host names. e.g.: `--apihosts "192.168.1.10, 192.168.1.11"` @@ -80,11 +88,11 @@ crontab -e Add the following line: ``` -*/30 * * * * python /home/pi/inky-pihole/main.py +*/30 * * * * python /home/pi/inky-pihole/main.py --token "4e0a518a7e2cc8e8b0520b91c8f0156709167ce9265298add8fa45d1d933f99e" ``` Or, add some command line options: ``` -*/30 * * * * python /home/pi/inky-pihole/main.py --rotate --lcars --simple --apihosts "10.0.0.10,10.0.0.11" --tz "Australia/Melbourne" +*/30 * * * * python /home/pi/inky-pihole/main.py --rotate --lcars --simple --apihosts "10.0.0.10,10.0.0.11" --tz "Australia/Melbourne" --token "4e0a518a7e2cc8e8b0520b91c8f0156709167ce9265298add8fa45d1d933f99e" ``` 30 minutes should be a non-obtrusive refresh time for most people. The display can flash quite a lot during updates, so refreshing more regularly is only recommended if you need closer monitoring. diff --git a/main.py b/main.py index bd19014..989d5aa 100644 --- a/main.py +++ b/main.py @@ -19,6 +19,7 @@ parser.add_argument('-s', '--simple', action='store_true', help='Simple Statistics Display') parser.add_argument('--lcars', action='store_true', help='Use LCARS style display') parser.add_argument('--tz', help='Timezone (default: UTC)') +parser.add_argument('-t', '--token', required=True, help='API authentication token. See: http://pihole/admin/settings.php?tab=api [Show API Token] button') # Helpers parser.add_argument('--timezones', action='store_true', help='Show all available TimeZone values') @@ -30,6 +31,10 @@ print(tz) exit() +if (args.token is None) + print('API token is mandatory!') + exit(); + # Initialize Inky pHAT Display display = auto() @@ -53,7 +58,8 @@ # Get Pi-Hole API data def get_data(host, combine=False): try: - f = urlopen('http://' + host + '/admin/api.php') # open API connection + url = 'http://' + host + '/admin/api.php?summaryRaw&auth=' + args.token + f = urlopen(url) # open API connection json_data_string = f.read() # Read data f.close() # close connection