Created
December 12, 2016 17:32
-
-
Save dicbobz/d533419412a83cca36c14cd8ed172ce4 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 | |
# Check IP addres and convert to in-addr.arpa address | |
# Write the commands to a file. Then execute ./dns-import.log to run migration | |
FILE=$1 | |
TMP="$(mktemp)" | |
LOG="$(pwd)/dns-import.log" | |
DOMAIN="domain.tld" | |
function validate_ip() | |
{ | |
local ipaddr=$1 | |
local code=1 | |
if [[ $ipaddr =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]; then | |
OIFS=$IFS | |
IFS='.' | |
ipaddr=($ipaddr) | |
IFS=$OIFS | |
[[ ${ipaddr[0]} -le 255 && ${ipaddr[1]} -le 255 && ${ipaddr[2]} -le 255 && ${ipaddr[3]} -le 255 ]] | |
code=$? | |
fi | |
return $code | |
} | |
function createPTR() | |
{ | |
local IPADDR=$1 | |
local FQDN=$2 | |
RN=$(echo $IPADDR | awk -F. '{print $4}') | |
RZ=$(echo $IPADDR | awk -F. '{print $3"."$2"."$1".in-addr.arpa."}') | |
echo "/usr/bin/ipa dnsrecord-add ${RZ} ${RN} --ptr-rec ${FQDN}" >> ${LOG} | |
} | |
function processZoneFile() | |
{ | |
# Simple check to see BIND zone header in the file | |
# Remove it | |
if [[ $(head -1 $FILE) =~ *ORIGIN$ ]]; then | |
echo "Likely a bad formatted file. Remove BIND zone header" | |
exit 1 | |
else | |
# Remove ORIGIN, TTL, Comments (;) lines out put to temp file | |
sed -n '/$ORIGIN/!p' ${FILE} | \ | |
sed -n '/$TTL/!p' | \ | |
sed -n '/;/!p' | awk '{print $1, $2, $3}' > ${TMP} | |
while IFS=$' \n\t' read -r HOST TYPE TARGET && [[ -n "$HOST" ]]; do | |
if [ $TYPE == 'A' ]; then | |
echo "/usr/bin/ipa dnsrecord-add ${DOMAIN} ${HOST} --a-rec ${TARGET}" >> ${LOG} | |
FQDN="${HOST}.domain.tld." | |
validate_ip ${TARGET} | |
if [[ $? -eq 0 ]]; then | |
createPTR ${TARGET} ${FQDN} | |
else | |
echo "Error creating PTR record" >> ${LOG} | |
fi | |
elif [ $TYPE == 'CNAME' ] && [[ "$TARGET" == *. ]]; then | |
echo "/usr/bin/ipa dnsrecord-add ${DOMAIN} ${HOST} --cname-rec=\"${TARGET}\"" >> ${LOG} | |
else | |
echo "Bad Input: $HOST $TYPE $TARGET" >> ${LOG} | |
fi | |
done < ${TMP} | |
fi | |
} | |
if [ -f $1 ]; then | |
processZoneFile | |
else | |
echo "Please supply a zone file with BIND zone header removed" | |
exit 1 | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment