Adding DNS entries with command line on Windows

Hello,

If you are lazy (as we are) or you don’t want to waste all your time adding DNS entries manually, you can use dnscmd via command line on Windows. It’s a nice way to put a large entries from a file or something that needs further configuration.

PROMPT> dnscmd help
dnscmd yourdnsserver /RecordAdd yourdomain.com mynewrecord A ip
so
(creates ftp.domain.com that points to 192.168.1.20)
dnscmd localhost /RecordAdd domain.com ftp A 192.168.1.20

(creates http://www.domain.com that points to 192.168.1.21)
dnscmd localhost /RecordAdd domain.com www A 192.168.1.21

You can use A, CNAME, PTR, TXT etc.

An example of a batch file putting entries in the same IP using loops.

PROMPT> type records.txt
record1
record2
record2
etc …

type dns.bat
@echo off

set dnshost=localhost
set domain=yourdomain.com
set type=A (dns type, PTR, CNAME etc)
set ipserver=192.168.1.20

echo “We are reading line by line records.txt”
for /f %%record in (records.txt) do dnscmd %dnshost% /RecordAdd %domain% %%record %type% %ipserver%

See you!

Advertisements

9 thoughts on “Adding DNS entries with command line on Windows

  1. Hi All,

    I need help on this. I need to ADD a SYSTEM DNS from a Command Prompt.

    And the DNS uses a driver Microsoft Access Driver (*.MDB).

    Please Need your help.

    Sudhir

  2. Good job! I used this to determine the reverselookup-addresses of PCs in our network. The Netbiosname is vt1-05, dns=vt1-05.localdomain.de

    Add a CNAME (=alias)
    dnscmd localhost /RecordAdd localdomain.de pc-richard CNAME vt1-05.localdomain.de

    Change a reverselookup:

    # delete old record
    # ip: _only_ the last number 192.168.81.5 -> 5
    dnscmd localhost /RecordDelete 81.168.192.in-addr.arpa 5 PTR /f
    dnscmd localhost /RecordAdd 81.168.192.in-addr.arpa 5 PTR pc-richard.localdomain.de

  3. Thanks for the info!
    Once I started playing around with this, I found it a little smoother if I had the contents of the text file as host and ip, ie:
    host1 ip1
    host2 ip2

    and then slightly modified the batch file to be:
    FOR /f “tokens=1,2” %a IN (records.txt) DO dnscmd localhost /RecordAdd %domain% %a %type% %b

    Hope this comes in handy for others!

    Thanks again,
    Patrick

    • typo in my FOR line above (it’ll work on command line but not embedded in a batch file)… should have double %’s in front of a and b:
      FOR /f “tokens=1,2” %%a IN (records.txt) DO dnscmd localhost /RecordAdd %domain% %%a %type% %%b

  4. I knew this would be possible. Day after day I have to use public IP’s for remote management and it gets tedious! this way I can just add memorable names to the IP’s saving me a load of hassle. Great article, very well laid out and demo’d.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s