1+ """Example for the usage of the hole module."""
2+ import asyncio
3+ import json
4+ from datetime import datetime
5+
6+ import aiohttp
7+
8+ from hole import Hole
9+
10+ PASSWORD = "your_password_here"
11+ HOST = "your_host_here"
12+ PROTOCOL = "https"
13+ PORT = 443
14+ VERIFY_TLS = False
15+
16+ async def main ():
17+ """Get the data from a Pi-hole instance."""
18+ async with aiohttp .ClientSession () as session :
19+ async with Hole (
20+ host = HOST ,
21+ session = session ,
22+ password = PASSWORD ,
23+ protocol = PROTOCOL ,
24+ port = PORT ,
25+ verify_tls = VERIFY_TLS
26+ ) as pihole :
27+ await pihole .get_data ()
28+
29+ print ("\n === Version Information ===" )
30+ print (f"Core Version: { pihole .core_current } (Latest: { pihole .core_latest } , Update Available: { pihole .core_update } )" )
31+ print (f"Web Version: { pihole .web_current } (Latest: { pihole .web_latest } , Update Available: { pihole .web_update } )" )
32+ print (f"FTL Version: { pihole .ftl_current } (Latest: { pihole .ftl_latest } , Update Available: { pihole .ftl_update } )" )
33+
34+ print ("\n === Basic Statistics ===" )
35+ print (f"Status: { pihole .status } " )
36+ print (f"Domains being blocked: { pihole .domains_being_blocked } " )
37+ print (f"Total queries today: { pihole .dns_queries_today } " )
38+ print (f"Queries blocked today: { pihole .ads_blocked_today } " )
39+ print (f"Percentage blocked: { pihole .ads_percentage_today } %" )
40+
41+ print ("\n === Client Statistics ===" )
42+ print (f"Total clients ever seen: { pihole .clients_ever_seen } " )
43+ print (f"Active clients: { pihole .unique_clients } " )
44+
45+ print ("\n === Query Statistics ===" )
46+ print (f"Queries forwarded: { pihole .queries_forwarded } " )
47+ print (f"Queries cached: { pihole .queries_cached } " )
48+ print (f"Unique domains: { pihole .unique_domains } " )
49+
50+ print ("\n === Top Permitted Domains ===" )
51+ for domain in pihole .top_queries :
52+ print (f"{ domain ['domain' ]} : { domain ['count' ]} queries" )
53+
54+ print ("\n === Top Blocked Domains (Ads) ===" )
55+ for domain in pihole .top_ads :
56+ print (f"{ domain ['domain' ]} : { domain ['count' ]} queries" )
57+
58+ print ("\n === Forward Destinations ===" )
59+ for upstream in pihole .forward_destinations :
60+ print (f"Name: { upstream ['name' ]} , IP: { upstream ['ip' ]} , Count: { upstream ['count' ]} " )
61+
62+ print ("\n === Reply Types ===" )
63+ for reply_type , count in pihole .reply_types .items ():
64+ print (f"{ reply_type } : { count } " )
65+
66+ print ("\n === Raw Data ===" )
67+ print (json .dumps ({
68+ "data" : pihole .data ,
69+ "blocked_domains" : pihole .blocked_domains ,
70+ "permitted_domains" : pihole .permitted_domains ,
71+ "clients" : pihole .clients ,
72+ "upstreams" : pihole .upstreams ,
73+ "blocking_status" : pihole .blocking_status ,
74+ "versions" : pihole .versions
75+ }, indent = 4 , sort_keys = True ))
76+
77+ async def toggle_blocking ():
78+ """Example of enabling and disabling Pi-hole blocking."""
79+ async with aiohttp .ClientSession () as session :
80+ async with Hole (
81+ host = HOST ,
82+ session = session ,
83+ password = PASSWORD ,
84+ protocol = PROTOCOL ,
85+ port = PORT ,
86+ verify_tls = VERIFY_TLS
87+ ) as pihole :
88+ await pihole .get_data ()
89+ initial_status = pihole .status
90+ print (f"\n Initial Pi-hole status: { initial_status } " )
91+
92+ print ("\n Disabling Pi-hole blocking for 60 seconds..." )
93+ disable_result = await pihole .disable (duration = 60 )
94+
95+ await pihole .get_data ()
96+ if pihole .status != "disabled" :
97+ print (f"ERROR: Failed to disable Pi-hole! Status is still: { pihole .status } " )
98+ return
99+ print (f"Successfully disabled Pi-hole. Status: { pihole .status } " )
100+ print (f"Disable operation response: { disable_result } " )
101+
102+ print ("\n Waiting 5 seconds..." )
103+ await asyncio .sleep (5 )
104+
105+ print ("\n Enabling Pi-hole blocking..." )
106+ enable_result = await pihole .enable ()
107+
108+ await pihole .get_data ()
109+ if pihole .status != "enabled" :
110+ print (f"ERROR: Failed to enable Pi-hole! Status is still: { pihole .status } " )
111+ return
112+ print (f"Successfully enabled Pi-hole. Status: { pihole .status } " )
113+ print (f"Enable operation response: { enable_result } " )
114+
115+ if pihole .status == initial_status :
116+ print ("\n Toggle test completed successfully! Pi-hole returned to initial state." )
117+ else :
118+ print (f"\n WARNING: Final status ({ pihole .status } ) differs from initial status ({ initial_status } )" )
119+
120+ if __name__ == "__main__" :
121+ print (f"=== Pi-hole Statistics as of { datetime .now ()} ===" )
122+ asyncio .run (main ())
123+ asyncio .run (toggle_blocking ())
0 commit comments