Skip to content

Instantly share code, notes, and snippets.

@schappim
Created December 10, 2024 04:00
Show Gist options
  • Save schappim/273474d489e3221065a3c06003810b14 to your computer and use it in GitHub Desktop.
Save schappim/273474d489e3221065a3c06003810b14 to your computer and use it in GitHub Desktop.
Pi-hole Blocklist Management Script
#!/bin/bash
DEFAULT_PIHOLE_IP="localhost"
show_help() {
echo "Usage: $0 [-i ip_address] [-t token] [-a domain|-r domain|-l]"
echo " -i: IP address of Pi-hole server (default: localhost)"
echo " -t: API token (required)"
echo " -a: Add domain to blocklist"
echo " -r: Remove domain from blocklist"
echo " -l: List current blocklist"
echo " -h: Show this help message"
exit 1
}
while getopts "i:t:a:r:lh" opt; do
case $opt in
i) PIHOLE_IP="$OPTARG";;
t) API_TOKEN="$OPTARG";;
a) ACTION="add" && DOMAIN="$OPTARG";;
r) ACTION="remove" && DOMAIN="$OPTARG";;
l) ACTION="list";;
h) show_help;;
?) show_help;;
esac
done
if [ -z "$API_TOKEN" ]; then
echo "Error: API token is required"
show_help
fi
PIHOLE_IP=${PIHOLE_IP:-$DEFAULT_PIHOLE_IP}
case $ACTION in
"add")
response=$(curl -s "http://${PIHOLE_IP}/admin/api.php?list=black&add=${DOMAIN}&auth=${API_TOKEN}")
echo "Adding ${DOMAIN} to blocklist: $response"
;;
"remove")
response=$(curl -s "http://${PIHOLE_IP}/admin/api.php?list=black&sub=${DOMAIN}&auth=${API_TOKEN}")
echo "Removing ${DOMAIN} from blocklist: $response"
;;
"list")
response=$(curl -s "http://${PIHOLE_IP}/admin/api.php?list=black&auth=${API_TOKEN}")
echo "Current Blocklist:"
echo "$response" | jq -r '.[]'
;;
*)
echo "Error: No action specified"
show_help
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment