Last active
April 20, 2024 20:44
-
-
Save oct8l/9df19cf878869fb152fb7a12b8b263e3 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 | |
basedir=".." | |
outputdir="output/bind9" | |
path="${basedir}/cache_domains.json" | |
nameserver="ns1.yourdomain.com." | |
# Ensure jq is installed | |
if ! command -v jq &> /dev/null; then | |
echo "jq is not installed. Please install jq to continue." | |
exit 1 | |
fi | |
mkdir -p ${outputdir} | |
# Read each domain entry | |
while read -r entry; do | |
# Process each domain file associated with the entry | |
while read -r fileid; do | |
while read -r filename; do | |
# Set the zonefile name based on the filename | |
zonefile="${outputdir}/$(basename ${filename} .txt).zone" | |
echo "\$TTL 86400" > "${zonefile}" | |
echo "@ IN SOA ${nameserver} admin.example.com. (" >> "${zonefile}" | |
echo " 2023010101 ; Serial" >> "${zonefile}" | |
echo " 3600 ; Refresh" >> "${zonefile}" | |
echo " 1800 ; Retry" >> "${zonefile}" | |
echo " 604800 ; Expire" >> "${zonefile}" | |
echo " 86400 ) ; Negative Cache TTL" >> "${zonefile}" | |
echo ";" >> "${zonefile}" | |
echo "@ IN NS ${nameserver}" >> "${zonefile}" | |
while read -r fileentry; do | |
if [[ ${fileentry} == \#* ]] || [[ -z ${fileentry} ]]; then | |
continue | |
fi | |
parsed=$(echo ${fileentry} | sed -e "s/^\*\.//") | |
echo "${parsed} IN A 192.168.1.1" >> "${zonefile}" # Example IP | |
done <<< $(cat ${basedir}/${filename} | sort) | |
done <<< $(jq -r ".cache_domains[${entry}].domain_files[$fileid]" ${path}) | |
done <<< $(jq -r ".cache_domains[${entry}].domain_files | to_entries[] | .key" ${path}) | |
done <<< $(jq -r '.cache_domains | to_entries[] | .key' ${path}) | |
echo "BIND9 zone files have been generated in ${outputdir}." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment