Note: This example assumes that the forward and reverse zones are already set as in the PowerDNS server.
The PowerDNS accepts the API PATCH calls with the JSON API.
The way it has been done in the example below using the JSON data templates for A and PTR records additions may not be optimal and it is very much possible that there are many ways of doing the same in a more proficient manner.
The JSON template file for adding the A record is named as "curlfileArecord.json" and looks like
{"rrsets": [
{"name": "fqdn.",
"type": "A",
"ttl": 86400,
"changetype": "REPLACE",
"set-ptr": true,
"records": [ {"content": "ip", "disabled": false} ]
}
]
}
The JSON template file for adding the PTR record is names as "curlfilePTRrecord.json" and looks like
[root@powerdns ~]# cat addPTRrecord.template.json
{
"rrsets": [
{"name": "reversedIP.in-addr.arpa.",
"type": "PTR",
"ttl": 86400,
"changetype": "REPLACE",
"records": [ {"content": "fqdn.", "disabled": false} ]
}
]
}
The Actual BASH Wrapper script is the below.
[root@powerdns ~]# cat powerdnsAddRecords.sh
#!/bin/bash
# This script adds the A record as well as the PTR record to the DNS
# Takes 3 arguments exactly
# Positional arguments:
# Argument1: IP address of the record to be added to the DNS
# Argument2: Short name of FQDNm if FQDN is server1.example.com, the Argument will be the short name of the argument like server1
# Argument3: This is the name of the of domain in the format example.com if the server FQDN is server1.example.com
# Exit with 1 if the Argument passed is not equal to 3
echo "$#"
if [ "$#" != 3 ]; then
echo "$0: Usage : $(basename $0) <server ip address> <server short name> <domain name>"
exit 1;
fi
ip=$1
serverShortName=$2
domain=$3
fqdn=${serverShortName}.${domain}
# See the FQDN formation
echo $fqdn
# format the IP last 2 octets to have the reverse zone name
reversedForZone=$(echo $ip |awk -F "." '{print $2"." $1}')
# Reverse all the octets so as to have the PTR record to be added to the reverse zone
reversedIP=$(echo $ip| awk -F "." '{print $4 "." $3 "." $2 "." $1 }')
# Calculation of the reverse Zone
reverseZone="${reversedForZone}.in-addr.arpa"
# Calculation of the reverse zone URL for PowerDNS API call
reverseZoneApi="http://172.16.192.236:8081/api/v1/servers/localhost/zones/${reverseZone}."
echo $reverseZoneApi
# Calculation of the forward zone PowerDNS API
domainname=$domain
forwardZoneApi="http://172.16.192.236:8081/api/v1/servers/localhost/zones/${domainname}."
echo $forwardZoneApi
# Use the JSON template for A record so as to generate a file to be used to pass as data to CURL API call
cat addArecord.template.json | sed -e 's|fqdn|'"$fqdn"'|g' -e 's|ip|'"$ip"'|g' > curlfileArecord.json
# Use the JSON template for PTR record so as to generate the file to be used to pass as data to CURL API Call
cat addPTRrecord.template.json | sed -e 's|reversedIP|'"$reversedIP"'|g' -e 's|fqdn|'"$fqdn"'|g' > curlfilePTRrecord.json
# Add the A Record
curl -X PATCH -H 'X-API-Key: securekeyhere' $forwardZoneApi --data @curlfileArecord.json
# Add the PTR Record
curl -X PATCH -H 'X-API-Key: securekeyhere' $reverseZoneApi --data @curlfilePTRrecord.json
[root@powerdns ~]#
Sample run of the script is as
[root@powerdns ~]# bash -x powerdnsAddRecords.sh 172.16.150.189 server1 example.com
+ echo 3
3
+ '[' 3 '!=' 3 ']'
+ ip=172.16.150.189
+ serverShortName=server1
+ domain=example.com
+ fqdn=server1.example.com
+ echo server1.example.com
server1.example.com
++ awk -F . '{print $2"." $1}'
++ echo 172.16.150.189
+ reversedForZone=16.172
++ echo 172.16.150.189
++ awk -F . '{print $4 "." $3 "." $2 "." $1 }'
+ reversedIP=189.150.16.172
+ reverseZone=16.172.in-addr.arpa
+ reverseZoneApi=http://172.16.192.236:8081/api/v1/servers/localhost/zones/16.172.in-addr.arpa.
+ echo http://172.16.192.236:8081/api/v1/servers/localhost/zones/16.172.in-addr.arpa.
http://172.16.192.236:8081/api/v1/servers/localhost/zones/16.172.in-addr.arpa.
+ domainname=example.com
+ forwardZoneApi=http://172.16.192.236:8081/api/v1/servers/localhost/zones/example.com.
+ echo http://172.16.192.236:8081/api/v1/servers/localhost/zones/example.com.
http://172.16.192.236:8081/api/v1/servers/localhost/zones/example.com.
+ cat addArecord.template.json
+ sed -e 's|fqdn|server1.example.com|g' -e 's|ip|172.16.150.189|g'
+ sed -e 's|reversedIP|189.150.16.172|g' -e 's|fqdn|server1.example.com|g'
+ cat addPTRrecord.template.json
+ curl -X PATCH -H 'X-API-Key: securekeyhere' http://172.16.192.236:8081/api/v1/servers/localhost/zones/example.com. --data @curlfileArecord.json
+ curl -X PATCH -H 'X-API-Key: securekeyhere' http://172.16.192.236:8081/api/v1/servers/localhost/zones/16.172.in-addr.arpa. --data @curlfilePTRrecord.json
[root@powerdns ~]#
If the script is run without any arguments
[root@powerdns ~]# bash powerdnsAddRecords.sh
0
powerdnsAddRecords.sh: Usage : powerdnsAddRecords.sh <server ip address> <server short name> <domain name>
[root@powerdns ~]#