.net 实现对DNS服务器的管理

最近在做一个虚拟机的web管理系统,中间遇到一个问题,就是新建的虚拟机不能通过主机名访问,原因是DNS出了问题,没有对应的主机名和IP地址的记录,网络这边暂时又解决不了,只能把这个问题交给我们,让我们通过程序来实现记录的添加与删除了。

解决方案:

1)通过powershell编程来实现。

2)用.net编程来实现。

我找了下.net关于DNS的类库,但没有相应的方法,只能通过WMI编程来间接实现了。 

using  System;
using  System.Collections.Generic;
using  System.Text;
using  System.Management;
using  System.Data;

namespace  TestDNS
{
   
public    class  DNSManage
    {
        
#region  filed
        
private   string  sServerPath;

        
private   string  username  =   null ;

        
private   string  password  =   null ;

        
private   string  DNSName  =   null ;

        
private  ManagementScope DNS;

        
private  System.Management.ManagementObjectCollection Q;

        
private  ManagementClass DnsClass;

        
private  ManagementBaseObject MI;


        
private   string  errMessage  =   null ;
        
        
#endregion

        
#region  property
        
public   string  ErrMessage
        {
            
get
            {
                
return  errMessage;
            }
        }

        
public   string  ServerName
        {
            
set
            {
                
this .sServerPath  =   string .Format( @" \\{0}\root\MicrosoftDNS " , value);
                
this .DNSName  =  value;
            }
        }


        
public   string  userName
        {
            
set
            {
                
this .username  =  value;
            }
        }


        
public   string  passWord
        {
            
set
            {
                
this .password  =  value;
            }
        } 
        
#endregion


       
public  DNSManage()
       {
           sServerPath 
=   @" \\localhost\root\MicrosoftDNS " ;
           DNSName 
=   " localhost " ;
       }

       
public  ManagementObjectCollection QueryDNS( string  query)
       {
           
if  ( ! string .IsNullOrEmpty(username)  &&   ! string .IsNullOrEmpty(password))
           {
               System.Management.ConnectionOptions Conn 
=   new  ConnectionOptions();
               Conn.Username 
=  username;
               Conn.Password 
=  password;
               DNS 
=   new  ManagementScope(sServerPath, Conn);
           }
           
else
           {
               DNS 
=   new  ManagementScope(sServerPath);
           }
           
if  ( ! DNS.IsConnected)
           {
               DNS.Connect();
           }
           System.Management.ManagementObjectSearcher QS 
=   new  ManagementObjectSearcher(DNS,  new  ObjectQuery(query));
           QS.Scope 
=  DNS;
           
return  QS.Get();
       }
       
public  ManagementObjectCollection QueryDNS( string  query,  string  DnsType)
       {
           
this .Create(DnsType);
           System.Management.ManagementObjectSearcher QS 
=   new  ManagementObjectSearcher(DNS,  new  ObjectQuery(query));
           QS.Scope 
=  DNS;
           
return  QS.Get();
       }
       
private   void  Create( string  DnsType)
       {
           
if  ( ! string .IsNullOrEmpty(username)  &&   ! string .IsNullOrEmpty(password))
           {
               System.Management.ConnectionOptions Conn 
=   new  ConnectionOptions();
               Conn.Username 
=  username;
               Conn.Password 
=  password;
               DNS 
=   new  ManagementScope(sServerPath, Conn);
           }
           
else
           {
               DNS 
=   new  ManagementScope(sServerPath);
           }
           
if  ( ! DNS.IsConnected)
           {
               DNS.Connect();
           }
           ManagementPath Path 
=   new  ManagementPath(DnsType);
           
this .DnsClass  =   new  ManagementClass(DNS, Path,  null );
       }

       
public   bool  IsExistsZone( string  domain)
       {

           
try
           {
               Q 
=  QueryDNS( " Select * From MicrosoftDNS_ZONE where ContainerName=' "   +  domain  +   " ' " );
               
foreach  (ManagementObject oManObject  in  Q)
               {
                   
// Console.WriteLine(oManObject["ContainerName"].ToString());
                    return   true ;
               }
               
return   false ;
           }
           
catch  (Exception e)
           {
               errMessage 
=  e.Message;
               Console.WriteLine(e.ToString());
               
return   false ;
           }

       }
  
       
public   bool  IsExistsAType( string  domain,  string  OwnerName)
       {
           
try
           {
               Q 
=  QueryDNS( " Select * From MicrosoftDNS_AType where OwnerName=' "   +  OwnerName  +   " ' and ContainerName=' "   +  domain  +   " ' " );
               
foreach  (ManagementObject oManObject  in  Q)
               {
                   
                   
return   true ;
               }
               
return   false ;
           }
           
catch  (Exception e)
           {
               errMessage 
=  e.Message;
               Console.WriteLine(e.ToString());
               
return   false ;
           }
       }

 
       
public   bool  CreateAType( string  ContainerName,  string  HostName,  string  IPAddress,  string  TTL)
       {
           
try
           {
               
string  OwnerName  =   null ;
               
if  ( string .IsNullOrEmpty(HostName))
               {
                   OwnerName 
=  ContainerName;
               }
               
else
               {
                   OwnerName 
=  HostName  +   " . "   +  ContainerName;
               }
               
this .Create( " MicrosoftDNS_AType " );
          
               
if  ( ! IsExistsZone(ContainerName))
               {
                  
                   errMessage 
=   string .Format( " domain:{0} is not exist " , ContainerName);
                   
return   false ;
               }
               
if  (IsExistsAType(ContainerName, OwnerName))
               {

                   errMessage 
=   string .Format( " {0} exist in {1} " , OwnerName, ContainerName);
                   
return   false ;
               }
               MI 
=  DnsClass.GetMethodParameters( " CreateInstanceFromPropertyData " );
               MI[
" DnsServerName " =  DNSName;
               MI[
" ContainerName " =  ContainerName;
               MI[
" OwnerName " =  OwnerName;
               MI[
" IPAddress " =  IPAddress;
               
if  ( string .IsNullOrEmpty(TTL))
               {
                   TTL 
=   " 3600 " ;
               }
               MI[
" TTL " =  TTL;
               DnsClass.InvokeMethod(
" CreateInstanceFromPropertyData " , MI,  null );
               errMessage 
=   " add  "   +  OwnerName  +   "  success. " ;
               
return   true ;
           }
           
catch  (Exception e)
           {
               errMessage 
=  e.Message;
               Console.WriteLine(e.ToString());
               
return   false ;
           }

       }


       
public   bool  CreateAType( string  DnsServerName,  string  ContainerName,  string  HostName,  uint  RecordClass,  uint  TTL,  string  IPAddress)
       {
           
try
           {
               
string  OwnerName  =   null ;
               
if  ( string .IsNullOrEmpty(HostName))
               {
                   OwnerName 
=  ContainerName;
               }
               
else
               {
                   OwnerName 
=  HostName  +   " . "   +  ContainerName;
               }
               
this .Create( " MicrosoftDNS_AType " );
           
               
if  ( ! IsExistsZone(ContainerName))
               {

                   errMessage 
=   string .Format( " domain:{0} is not exist " , ContainerName);
                   
return   false ;
               }
               
if  (IsExistsAType(ContainerName, OwnerName))
               {
                   errMessage 
=   string .Format( " {0} exist in {1} " , OwnerName, ContainerName);
                   
return   false ;
               }
               MI 
=  DnsClass.GetMethodParameters( " CreateInstanceFromPropertyData " );
               MI[
" DnsServerName " =  DnsServerName;
               MI[
" ContainerName " =  ContainerName;
               MI[
" OwnerName " =  OwnerName;
               MI[
" RecordClass " =  RecordClass;
               MI[
" TTL " =  TTL;
               MI[
" IPAddress " =  IPAddress;
               DnsClass.InvokeMethod(
" CreateInstanceFromPropertyData " , MI,  null );
               errMessage 
=   " add  "   +  OwnerName  +   "  success. " ;
               
return   true ;
           }
           
catch  (Exception e)
           {
               errMessage 
=  e.Message;
               Console.WriteLine(e.ToString());
               
return   false ;
           }

       }


       
public   bool  ModifyAType( string  ContainerName,  string  HostName,  string  IPAddress,  string  TTL)
       {
           
try
           {
               
string  OwnerName  =   null ;
               
if  ( string .IsNullOrEmpty(HostName))
               {
                   OwnerName 
=  ContainerName;
               }
               
else
               {
                   OwnerName 
=  HostName  +   " . "   +  ContainerName;
               }

              
               
if  ( ! IsExistsZone(ContainerName))
               {

                   errMessage 
=   string .Format( " domain:{0} is not exist " , ContainerName);
                   
return   false ;
               }
               
if  ( ! IsExistsAType(ContainerName, OwnerName))
               {
                   errMessage 
=   string .Format( " {0} not exist in {1} " , OwnerName, ContainerName);
                   
return   false ;
               }


               Q 
=  QueryDNS( " Select * From MicrosoftDNS_AType where ContainerName=' "   +  ContainerName  +   " ' and OwnerName=' "   +  OwnerName  +   " ' " " MicrosoftDNS_AType " );

               
foreach  (ManagementObject oManObject  in  Q)
               {

                   
if  (oManObject[ " IPAddress " ].ToString()  ==  IPAddress)
                   {
                       errMessage 
=   OwnerName  +   "  ipaddress is same. " ;
                       
return   false ;
                   }

                   MI 
=  oManObject.GetMethodParameters( " Modify " );
                   MI[
" IPAddress " =  IPAddress;
                   MI[
" TTL " =  TTL;
                   oManObject.InvokeMethod(
" Modify " , MI,  null );
                   errMessage 
=   " modify  "   +  OwnerName  +   "  success. " ;
                   
return   true ;
               }
               errMessage 
=   string .Format( " {0} failed " , OwnerName);
               
return   false ;
           }
           
catch  (Exception e)
           {
               errMessage 
=  e.Message;
               Console.WriteLine(e.ToString());
               
return   false ;
           }
       }

       
public   bool  DelAType( string  ContainerName,  string  HostName)
       {
           
try
           {
               
string  OwnerName  =   null ;
               
if  ( string .IsNullOrEmpty(HostName))
               {
                   OwnerName 
=  ContainerName;
               }
               
else
               {
                   OwnerName 
=  HostName  +   " . "   +  ContainerName;
               }

            
               
if  ( ! IsExistsZone(ContainerName))
               {

                   errMessage 
=   string .Format( " domain:{0} is not exist " , ContainerName);
                   
return   false ;
               }
               
if  ( ! IsExistsAType(ContainerName, OwnerName))
               {

                   errMessage 
=   string .Format( " {0} not exist in {1} " , OwnerName, ContainerName);
                   
return   false ;
               }

               Q 
=  QueryDNS( " Select * From MicrosoftDNS_AType where ContainerName=' "   +  ContainerName  +   " ' and OwnerName=' "   +  OwnerName  +   " ' " " MicrosoftDNS_AType " );

               
foreach  (ManagementObject oManObject  in  Q)
               {
                   oManObject.Delete();
                  
                   
return   true ;
               }
               errMessage 
=   string .Format( " {0}:{1},error " , ContainerName, OwnerName);
               
return   false ;
           }
           
catch  (Exception e)
           {
               errMessage 
=  e.Message;
               Console.WriteLine(e.ToString());
               
return   false ;
           }
       }


       
public  DataTable ListExistsAType( string  ContainerName)
       {

           DataTable table 
=   new  DataTable( " MicrosoftDNS_AType "   +  ContainerName);
           table.Columns.Add(
" OwnerName " );
           table.Columns.Add(
" IPAddress " );
           table.Columns.Add(
" TTL " );
           
try
           {
               Q 
=  QueryDNS( " Select * From MicrosoftDNS_AType where ContainerName=' "   +  ContainerName  +   " ' " );

               
foreach  (ManagementObject oManObject  in  Q)
               {
                   
try
                   {
                       DataRow row 
=  table.NewRow();
                       row[
" OwnerName " =  oManObject[ " OwnerName " ];
                       row[
" IPAddress " =  oManObject[ " IPAddress " ];
                       row[
" TTL " =  oManObject[ " TTL " ];
                       table.Rows.Add(row);
                   }
                   
catch  (Exception e)
                   {
                       Console.WriteLine(e.ToString());
                   }
               }
           }
           
catch  (Exception e)
           {
               errMessage 
=  e.Message;
               Console.WriteLine(e.ToString()); 
           }
        
          
           
return  table;
       }

       
public   bool  Test( string  ContainerName,  string  HostName,  string  IPAddress)
       {
           
string  TTL  =   " 3600 " ;
           
try
           {
               
string  OwnerName  =   null ;
               
if  ( string .IsNullOrEmpty(HostName))
               {
                   OwnerName 
=  ContainerName;
               }
               
else
               {
                   OwnerName 
=  HostName  +   " . "   +  ContainerName;
               }


               
if  ( ! IsExistsZone(ContainerName))
               {

                   errMessage 
=   string .Format( " domain:{0} is not exist " , ContainerName);
                   
return   false ;
               }
               
if  ( ! IsExistsAType(ContainerName, OwnerName))
               {
                   
this .CreateAType(ContainerName, HostName, IPAddress, TTL);
                   
return   true ;
               }


               Q 
=  QueryDNS( " Select * From MicrosoftDNS_AType where ContainerName=' "   +  ContainerName  +   " ' and OwnerName=' "   +  OwnerName  +   " ' " " MicrosoftDNS_AType " );

               
foreach  (ManagementObject oManObject  in  Q)
               {

                   
if  (oManObject[ " IPAddress " ].ToString()  ==  IPAddress)
                   {
                       errMessage 
=  OwnerName  +   "  ipaddress is not be modifid. " ;
                       
return   false ;
                   }

                   MI 
=  oManObject.GetMethodParameters( " Modify " );
                   MI[
" IPAddress " =  IPAddress;
                   MI[
" TTL " =  TTL;
                   oManObject.InvokeMethod(
" Modify " , MI,  null );
                   errMessage 
=   " modify  "   +  OwnerName  +   "  success. " ;
                   
return   true ;
               }
               errMessage 
=   string .Format( " {0} failed " , OwnerName);
               
return   false ;
           }
           
catch  (Exception e)
           {
               errMessage 
=  e.Message;
               Console.WriteLine(e.ToString());
               
return   false ;
           }

           
return   false ;
       }


    }
}

 

这个是我好不容易从网上搜到的,大家也可以参考http://www.jb51.net/article/8045.htm

 

 

你可能感兴趣的:(.net)