Files
SnarfCode/scripts/flush_pihole_cache.py

34 lines
1.5 KiB
Python

import paramiko
import sys
sys.stdout.reconfigure(encoding='utf-8')
key_path = r'C:\Users\Snarf\.ssh\id_ed25519_snarfnet'
def flush_pihole(ip, name):
print(f"\n==========================================")
print(f"Flushing Pi-hole DNS cache on {name} ({ip})...")
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(ip, username="root", key_filename=key_path, timeout=10)
stdin, stdout, stderr = ssh.exec_command("k3s kubectl get pods -n dns -l app=pihole -o jsonpath='{.items[0].metadata.name}'")
pod = stdout.read().decode().strip()
print(f"Pi-hole pod on {name}: {pod}")
if pod:
# Pi-hole v6 uses 'pihole reloaddns' or sending SIGHUP to pihole-FTL
stdin, stdout, stderr = ssh.exec_command(f"k3s kubectl exec -n dns {pod} -- pihole reloaddns")
print("reloaddns output:", stdout.read().decode().strip(), stderr.read().decode().strip())
stdin, stdout, stderr = ssh.exec_command(f"k3s kubectl exec -n dns {pod} -- pkill -HUP -f pihole-FTL")
stdout.channel.recv_exit_status()
# Test query from pod directly
stdin, stdout, stderr = ssh.exec_command(f"k3s kubectl exec -n dns {pod} -- dig @127.0.0.1 +short pihole1.snarfnet.net pihole2.snarfnet.net dns1.snarfnet.net dns2.snarfnet.net")
print("Resolved via 127.0.0.1 on pod:\n" + stdout.read().decode().strip())
ssh.close()
flush_pihole("140.44.4.11", "Aerodrome")
flush_pihole("140.44.4.41", "Spaceport")