Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Manage DNS server by C# code

Tags:

c#

dns

I need some sample code to create/delete zone and A record in microsoft DNS server by C#

like image 850
Ghooti Farangi Avatar asked Feb 28 '11 18:02

Ghooti Farangi


People also ask

How do I manage a DNS server?

After you set up a DNS server, you can manage the DNS server from the DNS management console. From this management console, you can perform common administrative tasks, such as adding additional zones, changing zone settings, adding A or MX records to an existing zone, and so on.

How do I set up a DNS zone in cPanel?

Click “Configure Address Records” next to each entry and verify that the IP address is the same as your cPanel server. This will add the necessary records to the zone file. Finally, tell your registrar about the DNS server’s domain names. The process differs depending on the registrar, so you should consult their DNS server documentation or

What is Domain Name System (DNS)?

The domain name system is a hierarchical arrangement of servers that fall into two broad categories: recursive and authoritative. The one that knows the mapping between a site’s domain name and IP address is its authoritative DNS server; it’s the definitive source of truth about that domain.

What is the DNS WMI provider?

The DNS WMI Provider enables the administration of DNS Servers from the server itself, or from remote hosts. The general steps necessary to administer a DNS Server using the DNS WMI Provider are: Listing or modifying a server property, or using a method


2 Answers

You have to use WMI to invoke the DNSProvider.

This to add a record:

 public void AddARecord(string hostName, string zone, string iPAddress, string dnsServerName)
 {
      ManagementScope scope = 
         new ManagementScope(@"\\" + dnsServerName + "\\root\\MicrosoftDNS");

      scope.Connect();

      ManagementClass cmiClass =
         new ManagementClass(scope, 
                             new ManagementPath("MicrosoftDNS_AType"),
                             null);

     ManagementBaseObject inParams = 
         cmiClass.GetMethodParameters("CreateInstanceFromPropertyData");

     inParams["DnsServerName"] = this.ServerName;
     inParams["ContainerName"] = zone;
     inParams["OwnerName"] = hostName + "." + zone;
     inParams["IPAddress"] = iPAddress;

     cmiClass.InvokeMethod("CreateInstanceFromPropertyData", inParams, null);
}

You can reference the WMI reference and extend this as you need using the methods and classes http://msdn.microsoft.com/en-us/library/ms682123(v=vs.85).aspx

like image 60
Taylor Bird Avatar answered Sep 27 '22 22:09

Taylor Bird


Microsoft exposes it as a POX service, so you could just push XML over the wire to it, using the System.Net stuff & your user credentials.

http://technet.microsoft.com/en-us/library/dd278634.aspx

like image 41
Paul Avatar answered Sep 27 '22 20:09

Paul