Automate Domain DNS Management with a Bash Script
Managing DNS records can be a time-consuming task, especially when handling multiple subdomains. This Bash script simplifies the process using the Porkbun API, automating DNS record queries, subdomain creation, and deletion. While this example specifically works for Porkbun.com, the same concept can be adapted to any domain provider that offers an API, enabling seamless DNS automation tailored to your provider’s specifications.
Why Automate DNS Management?
Manual DNS management can be error-prone and tedious. Automating these tasks ensures consistency, saves time, and reduces administrative overhead.
Key Features of the Script
- DNS Record Queries: Fetches existing DNS records for a domain.
- Subdomain Creation: Adds random subdomains with specified IP addresses.
- Subdomain Deletion: Deletes subdomains based on specific criteria.
- Secure API Integration: Interacts securely with the Porkbun API.
How It Works
1. Query DNS Records
The script retrieves DNS records for a specified domain, ensuring visibility into existing subdomains and their IPs.
Command Example:
./domain-api-setup.sh example.com
What It Does:
- Lists subdomains with different IP addresses.
- Displays subdomain IDs, names, and associated IP addresses.
2. Add Subdomains
Automatically generates and adds subdomains with random names to the specified domain.
Process:
- Generates a random subdomain name.
- Associates it with the target IP.
- Sets a time-to-live (TTL) of 600 seconds.
Output Example:
Subdomain created successfully!
Subdomain: randomname.example.com -> 34.27.202.113
3. Delete Subdomains
Removes subdomains with IP addresses different from the main domain’s IP.
How It Works:
- Retrieves DNS records.
- Deletes records with IPs that do not match the primary domain’s IP.
Success Message:
Subdomain with ID 12345 deleted successfully.
Getting Started
1. Download and Make the Script Executable
chmod +x domain-api-setup.sh
2. Run the Script
./domain-api-setup.sh mydomain.com
3. Follow the Output
The script provides real-time feedback, displaying DNS queries, subdomain additions, and deletions.
Benefits of Using This Script
- Time-Saving: Automates repetitive DNS management tasks.
- Error-Reduction: Minimizes manual configuration mistakes.
- Scalable: Easily handles multiple subdomains and domains.
- Secure Integration: Works securely with the Porkbun API.
Conclusion
Automating DNS management with this Bash script ensures efficient and error-free domain management. It streamlines tasks like querying DNS records, adding subdomains, and removing outdated entries. Enhance your domain management today with this powerful, automated solution.
Note: Ensure your API keys are kept secure and rotate them periodically for added security.
#!/bin/bash
# =========================================
# Domain API Setup Script
# =========================================
#
# Version: 1.0.0
# Script written by Warith AL Maawali
#
# Discord channel: https://discord.gg/KEFErEx
# Twitter: http://twitter.com/warith2020
# Linkedin: http://www.linkedin.com/in/warith1977
# Website: https://www.digi77.com
# (c) 2024
#
# Description:
# This script automates the management of DNS records for a specified domain using the Porkbun API.
# It includes functionalities to query existing DNS records, add new subdomains with random names,
# and delete subdomains with specific criteria.
#
# This software is dual-licensed:
#
# Personal, non-commercial use: Apache License 2.0
# Commercial, corporate, or organizational use: Separate commercial license required.
# Contact me for licensing inquiries.
#
# Usage: ./domain-api-setup.sh [domain]
#
# Usage Examples:
# Run this script to manage DNS records for the default domain:
# ./domain-api-setup.sh
# Run this script to manage DNS records for a specified domain:
# ./domain-api-setup.sh example.com
# =========================================
# Global variables for API authentication and domain configuration
API_KEY="pk1_3xxxxxxxxxxxxxxxx"
SECRET_API_KEY="sk1_xxxxxxxxxxxxxxxxxxxxxxxxxx"
DOMAIN="xxx.com" # Default domain name
TYPE="A" # DNS record type
TARGET_IP="34.27.202.113" # Target IP address for subdomains
TTL="600" # Time to live for DNS records
NUM_SUBDOMAINS=4 # Number of subdomains to add
# Check if the user provided a domain as an argument and override the default if so
if [ ! -z "$1" ]; then
DOMAIN="$1"
fi
# Function to query DNS records for the specified domain
query_dns_records() {
echo "Fetching DNS records for: $DOMAIN, Type: $TYPE"
echo "------------------------------------------------"
# Query Porkbun API for DNS records by domain and type
RESPONSE=$(curl -s -X POST https://api.porkbun.com/api/json/v3/dns/retrieve/"$DOMAIN" \
-H "Content-Type: application/json" \
-d '{
"apikey": "'"$API_KEY"'",
"secretapikey": "'"$SECRET_API_KEY"'"
}')
# Check if the response was successful
STATUS=$(echo "$RESPONSE" | jq -r '.status')
if [[ "$STATUS" != "SUCCESS" ]]; then
echo "Error retrieving DNS records: $RESPONSE"
exit 1
fi
# Get the main domain IP address
MAIN_IP=$(dig +short "$DOMAIN" | head -n 1)
# List subdomains with their IDs and IP addresses, excluding those with the main domain IP and ensuring valid IP addresses
echo "Subdomains with different and valid IP Addresses:"
echo "-------------------------------------------------"
echo "$RESPONSE" | jq -r --arg MAIN_IP "$MAIN_IP" '.records[] | select(.name != "'"$DOMAIN"'") | select(.content != $MAIN_IP) | select(.content | test("^[0-9]+\\.[0-9]+\\.[0-9]+\\.[0-9]+$")) | "\(.id) - \(.name) - \(.content)"'
}
# Function to add a specified number of subdomains with random names
add_subdomain() {
for ((i = 0; i < NUM_SUBDOMAINS; i++)); do
# Generate a random subdomain string
RANDOM_SUBDOMAIN="$(openssl rand -hex 8)"
# Create the subdomain via Porkbun API
RESPONSE=$(curl -s -X POST https://api.porkbun.com/api/json/v3/dns/create/"$DOMAIN" \
-H "Content-Type: application/json" \
-d '{
"apikey": "'"$API_KEY"'",
"secretapikey": "'"$SECRET_API_KEY"'",
"name": "'"$RANDOM_SUBDOMAIN"'",
"type": "'"$TYPE"'",
"content": "'"$TARGET_IP"'",
"ttl": "'"$TTL"'"
}')
# Check if the subdomain was created successfully
STATUS=$(echo "$RESPONSE" | jq -r '.status')
if [[ "$STATUS" == "SUCCESS" ]]; then
echo "Subdomain created successfully!"
echo "Subdomain: $RANDOM_SUBDOMAIN.$DOMAIN -> $TARGET_IP"
else
echo "Error creating subdomain: $RESPONSE"
fi
done
}
# Function to delete subdomains with different and valid IP addresses
delete_subdomains() {
echo "Deleting subdomains with different and valid IP Addresses:"
echo "----------------------------------------------------------"
# Query Porkbun API for DNS records by domain and type
RESPONSE=$(curl -s -X POST https://api.porkbun.com/api/json/v3/dns/retrieve/"$DOMAIN" \
-H "Content-Type: application/json" \
-d '{
"apikey": "'"$API_KEY"'",
"secretapikey": "'"$SECRET_API_KEY"'"
}')
# Check if the response was successful
STATUS=$(echo "$RESPONSE" | jq -r '.status')
if [[ "$STATUS" != "SUCCESS" ]]; then
echo "Error retrieving DNS records: $RESPONSE"
exit 1
fi
# Get the main domain IP address
MAIN_IP=$(dig +short "$DOMAIN" | head -n 1)
# Delete subdomains with different and valid IP addresses
echo "$RESPONSE" | jq -r --arg MAIN_IP "$MAIN_IP" '.records[] | select(.name != "'"$DOMAIN"'") | select(.content != $MAIN_IP) | select(.content | test("^[0-9]+\\.[0-9]+\\.[0-9]+\\.[0-9]+$")) | .id' | while read -r ID; do
DELETE_RESPONSE=$(curl -s -X POST https://api.porkbun.com/api/json/v3/dns/delete/"$DOMAIN"/"$ID" \
-H "Content-Type: application/json" \
-d '{
"apikey": "'"$API_KEY"'",
"secretapikey": "'"$SECRET_API_KEY"'"
}')
DELETE_STATUS=$(echo "$DELETE_RESPONSE" | jq -r '.status')
if [[ "$DELETE_STATUS" == "SUCCESS" ]]; then
echo "Subdomain with ID $ID deleted successfully."
else
echo "Error deleting subdomain with ID $ID: $DELETE_RESPONSE"
fi
done
}
# Call the functions in sequence to manage DNS records
query_dns_records # Initial query to list current DNS records
add_subdomain # Add new subdomains
query_dns_records # Query again to verify new subdomains
delete_subdomains # Delete specific subdomains
query_dns_records # Final query to confirm deletions
exit 0 # Exit the script successfully
ShellScript