DNS bulk PTR records creation

 

My previous article was about bulk DNS records creation in forward lookup zone. This time we will focus on the same activity but in reverse lookup zone. In my opinion this kind of task is much more frequently used that the previous on. You may ask, why? Because in regular basis when you create host (A) record in forward lookup zone you don’t care about pointer (PTR) record in reverse lookup zone. This may happen due to 3 scenarios:

  • You really don’t need PTR record(s) 🙂
  • You have not checked “Create associated pointer (PTR) record” when adding host record

Option for pointer (PTR) record auto creation

  •  You have checked above option but DNS reverse lookup zone does not exist

Reverse lookup zone does not exist

So, one of these cases may lead you to bulk PTR records creation in the future when you realize that you need this kind of record(s). I will try to simply show you, how to do that very quickly using the least administrative effort because using DNS Management console is not very convenient and fast method (you need to create each records separately by hand).

I will show you how to do that using simple scripts based on:

  • Windows DNScmd command
  • Using DNSShell module for Windows PowerShell (really great module)
  • Using native DNS cmd-lets in PowerShell 3.0

Windows DNScmd command is by default available on Windows Server 2008/2008R2 server where DNS role has been installed. To use it on Windows Server 2003, you need to install Support Tools from server’s CD#1

DNSShell module needs to be downloaded separately from Chris blog who is REAL genius in DNS topic. His module for PowerShell is really great and very helpful.

Windows PowerShell 3.0 DNS cmd-lets are available in Windows 8 and Windows Server 2012 and are new feature added by Microsoft to manage DNS server.

PowerShell 3.0 can be also installed on Windows 7/2008/2008R2 for more information about that, please check my another article “Windows Management Framework 3.0 for Windows Server 2008/2008R2”

DNScmd

Before we start preparing script for bulk DNS records creation, let’s check if appropriate reverse lookup zone(s) exist(s). This is mandatory to have reverse lookup zone existing in other case PTR (pointer) records won’t be created! When zone does not exist, you need to create it first before you can start using script for bulk records creation.

After we verified zone existence we can start to creating records. But before that let’s see how DNScmd syntax looks for single pointer (PTR) record. After that we would know which parameters should be inserted into input file for the script.

Note! To get help for DNScmd command you need to specify /? after its name or after any switch you want to get information

Now, we will prepare proper syntax to add single pointer (PTR) record into DNS zone. For that we need to know:

  • DNS server name
  • DNS zone name (reverse lookup zone name)
  • IP address
  • host name

proper syntax to create pointer (PTR) record is:

dnscmd ServerName /RecordAdd DNSReverseZoneName IPAddress RecordType FQDNHostName
 

Below you can find an example syntax

dnscmd %LOGONSERVER% /RecordAdd 1.168.192.in-addr.arpa 100 PTR testHost.testenv.local

DNScmd command execution

instead of %LOGONSERVER% system variable, you can use DNS server name (if all  your DCs are DNS servers too, you can sinply use %LOGONSERVER%, in other case, you need to type DNS server name manually)

As you can see in DNS Management console, new record has been created

DNS record verification

We have complete syntax and now, we can create a script to create many DNS records in short time. First of all, we need an input file containing all required data. To create that file we need put in a flat text file 2 or 3 values:

  • an octet of IP Address for which we want to add PTR record
  • host Fully-Qualified Domain Name
  • optionally reverse lookup zone name (if we want to create PTR records for multiple zones)

An example input file for script (2 values and the same zone)

100 testHost01.testenv.local
101 testHost02.testenv.local
102 testHost03.testenv.local
103 testHost04.testenv.local
104 testHost05.testenv.local

of example input file with 3 values

105 testHost06.testenv.local 1.168.192.in-addr.arpa
106 testHost07.testenv.local 1.168.192.in-addr.arpa
107 testHost08.testenv.local 2.168.192.in-addr.arpa
108 testHost09.testenv.local 2.168.192.in-addr.arpa
109 testHost10.testenv.local 3.168.192.in-addr.arpa

and save this as i.e. newPTR.txt on C-Drive

Now, you can use below script to create many DNS records (case with 2 values in file)

for /f "tokens=1-2" %i in (c:newPTR.txt) do dnscmd %LOGONSERVER% /RecordAdd 1.168.192.in-addr.arpa %i PTR %j

Bulk DNS pointer records created

and you can verify that in DNS Management console

DNS records verification

and now, code for the case with 3 values in file

for /f "tokens=1-3" %i in (c:newPTR.txt) do dnscmd %LOGONSERVER% /RecordAdd %k %i PTR %j

Bulk DNS records created

and you can verify that in DNS Management console once again

DNS records verification

DNSShell module for PowerShell

As I mentioned at the beginning of this article, this is separate module which needs to be downloaded. You can simply download it from

http://www.indented.co.uk/index.php/2010/04/16/dnsshell-zone-and-server-cmdlets/

When you download it, you have to extract content into one of the following locations:

  • %HOMEPATH%DocumentsWindowsPowerShellModules
  • %WINDIR%SYSTEM32WindowsPowerShellv1.0Modules

PowerShell modules path

and import this module before the first use

Import-Module DNSShell

to list all available cmd-lets use

help *DNS*

Importing DNSShell module and list all available cmd-lets

From now, you have all cmd-lets available. Let’s start to create single host record in DNS using New-DNSRecord cmd-let

To be able to create pointer (PTR) record using DNSShell, you need:

  • DNS zone name (reverse lookup zone name)
  • an octet of IP Address for which we want to add PTR record
  • host name

You will find general syntax below

New-DNSRecord -Name AnOctet -RecordType PTR -ZoneName ReverseZoneName -HostName HostFQDN

and short example

New-DNSRecord -Name 100 -RecordType PTR -ZoneName 1.168.192.in-addr.arpa -HostName testHost01.testenv.local

New-DNSRecord example

and you can see command’s result in DNS Manager

DNS record veryfying

So, now we can create a script to automatically create many DNS records. As for PowerShell is better to use CSV file format instead of flat text file, I would suggest to prepare an example here. CSV file requires a header for each attribute, we need 2 or 3 attributes to accomplish that.

An example CSV file for 2 values

octet,hostName
100,testHost01.testenv.local
101,testHost02.testenv.local
102,testHost03.testenv.local
103,testHost04.testenv.local
104,testHost05.testenv.local

An example CSV file for 3 values

octet,hostName,zoneName
105,testHost06.testenv.local,1.168.192.in-addr.arpa
106,testHost07.testenv.local,1.168.192.in-addr.arpa
107,testHost08.testenv.local,2.168.192.in-addr.arpa
108,testHost09.testenv.local,2.168.192.in-addr.arpa
109,testHost10.testenv.local,3.168.192.in-addr.arpa

Save this file as newPTR.csv on C-Drive and use below script to create DNS records

for 2 values

Import-Module DNSShell
Import-CSV c:newPTR.csv | %{
New-DNSRecord -Name $_."octet" -RecordType PTR -ZoneName 1.168.192.in-addr.arpa -HostName $_."hostName"
}

PowerShell script

and verify results in DNS Manager

DNS records veryfying

and one more case with 3 values in CSV file

Import-Module DNSShell
Import-CSV c:newPTR.csv | %{
New-DNSRecord -Name $_."octet" -RecordType PTR -ZoneName $_."zoneName" -HostName $_."hostName"
}

PowerShell code

and verify in DNS Manager if they were created

DNS records verifying

Native DNS cmd-lets in PowerShell 3.0

This is new feature and can be only used with PowerShell 3.0 which is available in Windows 8 and Windows Server 2012 (or in other Windows versions as it was mentioned at the beginning of this article). There are variety of DNS cmd-lets to manage DNS server and one of  them is Add-DNSServerResourceRecordPTR and we will use it in this article.

Add-DNSServerResourceRecordPTR cmd-let

To create pointer (PTR) record using this cmd-let you need:

  • DNS zone name (reverse lookup zone name)
  • an octet of IP Address for which we want to add PTR record
  • host name

And now for a practice, we will create single DNS record using Add-DNSServerResourceRecordPTR

Add-DNSServerResourceRecordPTR -ZoneName DNSReverseZoneName -Name octet -PTRDomainName hostName

according to above general syntax, let’s create pointer record

Add-DNSServerResourceRecordPTR -ZoneName 1.168.192.in-addr.arpa -Name 100 -PTRDomainName testHost01.testenv.local

PowerShell 3.0 DNS record creation

and as in previous methods, just verify  if DNS record was created

DNS record verification

So, now the last part. We need to prepare script for multiple records creation. As we would need to use CSV file as in previous method (DNSShell module for Windows PowerShell), we will reuse it. An example CSV file is below

for 2 values

octet,hostName
100,testHost01.testenv.local
101,testHost02.testenv.local
102,testHost03.testenv.local
103,testHost04.testenv.local
104,testHost05.testenv.local

and for 3 values

octet,hostName,zoneName
105,testHost06.testenv.local,1.168.192.in-addr.arpa
106,testHost07.testenv.local,1.168.192.in-addr.arpa
107,testHost08.testenv.local,2.168.192.in-addr.arpa
108,testHost09.testenv.local,2.168.192.in-addr.arpa
109,testHost10.testenv.local,3.168.192.in-addr.arpa

and save this as newPTR.csv file on C-Drive. When you do that, use below code for pointer (PTR) records creation

PowerShell 3.0 code for CSV with 2 values

Import-CSV c:newPTR.csv | %{
Add-DNSServerResourceRecordPTR -ZoneName 1.168.192.in-addr.arpa -Name $_."octet "-PTRDomainName $_."hostName"
}

PowerShell 3.0 code

and DNS Manager view to prove that record were created

DNS Manager and newly created DNS records

and the last part with PowerShell 3.0 for DNS, code for CSV file with 3 values

Import-CSV c:newPTR.csv | %{
Add-DNSServerResourceRecordPTR -ZoneName $_."zoneName" -Name $_."octet "-PTRDomainName $_."hostName"
}

just to be sure if records were created, let’s check each reverse lookup zone to verify that

DNS Manager and newly created DNS records

That’s all!

<<< Previous part

Author: Krzysztof Pytko

Facebooktwittergoogle_plusredditpinterestlinkedinmail

Leave a Reply

Your email address will not be published. Required fields are marked *