Created
December 10, 2024 03:59
-
-
Save schappim/41bed3847626b71f1c1c33b029b0483e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Default values | |
DEFAULT_DURATION=300 # 5 minutes in seconds | |
DEFAULT_PIHOLE_IP="localhost" | |
# Help function | |
show_help() { | |
echo "Usage: $0 [-d duration] [-i ip_address] [-t token]" | |
echo " -d: Duration in seconds to disable Pi-hole (default: 300 seconds)" | |
echo " -i: IP address of Pi-hole server (default: localhost)" | |
echo " -t: API token (required)" | |
echo " -h: Show this help message" | |
exit 1 | |
} | |
# Parse command line arguments | |
while getopts "d:i:t:h" opt; do | |
case $opt in | |
d) DURATION="$OPTARG";; | |
i) PIHOLE_IP="$OPTARG";; | |
t) API_TOKEN="$OPTARG";; | |
h) show_help;; | |
?) show_help;; | |
esac | |
done | |
# Validate required parameters | |
if [ -z "$API_TOKEN" ]; then | |
echo "Error: API token is required" | |
show_help | |
fi | |
# Set defaults if not provided | |
DURATION=${DURATION:-$DEFAULT_DURATION} | |
PIHOLE_IP=${PIHOLE_IP:-$DEFAULT_PIHOLE_IP} | |
# Validate duration is a positive number | |
if ! [[ "$DURATION" =~ ^[0-9]+$ ]]; then | |
echo "Error: Duration must be a positive number" | |
exit 1 | |
fi | |
# Make API call to disable Pi-hole | |
response=$(curl -s "http://${PIHOLE_IP}/admin/api.php?disable=${DURATION}&auth=${API_TOKEN}") | |
# Check if the response contains "disabled" | |
if [[ $response == *"disabled"* ]]; then | |
echo "Pi-hole has been disabled for ${DURATION} seconds" | |
echo "It will automatically re-enable after this duration" | |
# Calculate when it will re-enable | |
if command -v date &> /dev/null; then | |
enable_time=$(date -d "+${DURATION} seconds" "+%H:%M:%S") | |
echo "Pi-hole will re-enable at: $enable_time" | |
fi | |
else | |
echo "Error: Failed to disable Pi-hole. Response: $response" | |
exit 1 | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment