DNS NsLookup using Delphi
Network Component provides an easy-to-use development interface to a variety of IP protocols. By using Network Component, you can very easily create or enhance applications with network features.
Network Component features the following: DNS, FTP, HTTP, HTTPs, ICMP Ping, IP-to-Country, MSN, NTP, RSH, SCP, SFTP, SNMP v1/v2c (Get, GetNext, Set), SNMP Traps, SNMP MIB, SSH, TCP, Telnet, TFTP, UDP, Telnet, Wake-On-LAN and more.
Network Component can be well integrated into any development platform that supports ActiveX objects.
The Domain Name System (DNS) is the method by which Internet addresses in mnemonic form - such as www.activexperts.com - are converted into the equivalent numeric IP address such as 212.97.55.136. To the user and application process this translation is a service provided either by the local host or from a remote host via the Internet. The DNS server (or resolver) may communicate with other Internet DNS servers if it cannot translate the address itself. DNS names are constructed hierarchically. The highest level of the hierarchy is the last component or label of the DNS address. Labels can be up to 63 characters long and are not case sensitive. A maximum length of 255 characters is allowed. Labels must start with a letter and can only consist of letters, digits and hyphens.
Nslookup is a popular program for UNIX, Linux and Windows to query Internet domain name servers. It allows the user to query name servers for information about various hosts and domains or to print a list of hosts in a domain.
Step 1: Download and install the Network Component
Download Network Component from the ActiveXperts Download Site and start the installation. The installation guides you through the installation process.
Step 2: Create a new Delphi Project
Launch Borland Delphi from the Start menu. Choose 'New' from the 'File' menu and select your preferred kind of application, for instance: 'VCL Forms Application - Delphi for Win32'. A new Form is displayed in the workspace.
Step 3: Refer to the Network Component Library and create the objects
Now that a new project has been created, you must add a reference to Network Component in the project to be able to use the Network Component object. To do so, choose 'Import Component...' from the 'Component' menu. The Import Components' dialog appears. Select 'Import a Type Library':
In the 'Registered Type Libraries' page, select 'Network Component 4.4 Type Library' and click 'Next':
In the 'Components' page, leave all fields default and click 'Next':
In the 'Install' page, select 'Create Unit' and click 'Next':
The interface code is generated now and is shown in the AxNetwork_TLB tab of the project.
Step 4: Declare and create the object
From the Project Manager, open Unit1.bas and add the AxNetwork_TLB to the 'Uses' statement to refer to the Network Component library:
Appendix: Full source code
program DNSdemo; {$APPTYPE CONSOLE} uses SysUtils, ActiveX, AxNetwork_TLB in '..\..\Imports\AxNetwork_TLB.pas'; {==============================================================================} {= R e a d I n p u t ========================================================} {==============================================================================} function ReadInput(strTitle:string):string; var strInput:string; begin strInput:=''; writeln(strTitle); while(strInput = '')do begin Write(' > '); ReadLn(strInput); end; ReadInput := strInput; end; {==============================================================================} {==============================================================================} {= T H E P R O G R A M I T S E L F ========================================} {==============================================================================} {==============================================================================} var objDNSserver:DnsServer; objConstants:NwConstants; strDNSserver, strHost, strQueryResult:string; objDNSrecord:DnsRecord; vt:OleVariant; begin CoInitialize(nil); strDnsServer := ''; strHost := ''; strQueryResult := ''; objDNSserver := CoDnsServer.Create(); objConstants := CoNwConstants.Create(); objDNSrecord := CoDnsRecord.Create(); // A license key is required to unlock this component after the trial period // has expired. Assign the LicenseKey property every time a new instance of // this component is created (see code below). Alternatively, the LicenseKey // property can be set automatically. This requires the license key to be // stored in the registry. For details, see manual, chapter // "Product Activation". { objDNSserver.LicenseKey = 'XXXXX-XXXXX-XXXXX'; } Writeln('ActiveXperts Network Component ' + objDNSserver.Version + sLineBreak + 'Build: ' + objDNSserver.Build + sLineBreak + 'Module: ' + objDNSserver.Module + sLineBreak + 'License Status: ' + objDNSserver.LicenseStatus + sLineBreak + 'License Key: ' + objDNSserver.LicenseKey + sLineBreak); // To enable logging,assign a valid log file to the LogFile property objDnsServer.LogFile := 'C:\DnsDemo.log'; strDNSserver := ReadInput( 'Enter DNS server: ' ); strHost := ReadInput( 'Enter domain or host: ' ); objDnsServer.Server := strDnsServer; objDnsServer.Lookup( strHost, objConstants.nwDNS_TYPE_ANY ); WriteLn('Lookup, result: ' + IntToStr(objDnsServer.LastError) + ' (' + objDnsServer.GetErrorDescription(objDnsServer.LastError) + ')' ); if( objDnsServer.LastError = 0 )then begin if( objDnsServer.IsAuthoritative = true )then WriteLn('Server is an authority for this domain' ) else WriteLn('Server is not an authority for this domain'); end; writeln; if( objDnsServer.LastError = 0 )then begin vt:=objDnsServer.GetFirstRecord (); while( objDnsServer.LastError = 0 )do begin objDNSrecord := IDispatch ( vt ) as IDnsRecord; if ( objDnsRecord.type_ = objConstants.nwDNS_TYPE_A )then begin strQueryResult := strQueryResult+'Type: A' + sLineBreak; strQueryResult := strQueryResult+'Name: ' + objDnsRecord.Name + sLineBreak; strQueryResult := strQueryResult+'Address: ' + objDnsRecord.Address + sLineBreak; end; if ( objDnsRecord.Type_ = objConstants.nwDNS_TYPE_AAAA )then begin strQueryResult := strQueryResult+'Type: AAAA' + sLineBreak; strQueryResult := strQueryResult+'Name: ' + objDnsRecord.Name + sLineBreak; strQueryResult := strQueryResult+'Address: ' + objDnsRecord.Address + sLineBreak; end; if(objDnsRecord.Type_ = objConstants.nwDNS_TYPE_CNAME )then begin strQueryResult := strQueryResult +'Type: CNAME' + sLineBreak; strQueryResult := strQueryResult +'Name: ' + objDnsRecord.Name + sLineBreak; strQueryResult := strQueryResult +'Alias: ' + objDnsRecord.Address + sLineBreak; end; if(objDnsRecord.Type_ = objConstants.nwDNS_TYPE_NS )then begin strQueryResult := strQueryResult +'Type: NS' + sLineBreak; strQueryResult := strQueryResult +'Name: ' + objDnsRecord.Name + sLineBreak; strQueryResult := strQueryResult +'NameServer: '+ objDnsRecord.NameServer + sLineBreak; end; if(objDnsRecord.Type_ = objConstants.nwDNS_TYPE_MX )then begin strQueryResult := strQueryResult +'Type: MX' + sLineBreak; strQueryResult := strQueryResult +'Name: ' + objDnsRecord.Name + sLineBreak; strQueryResult := strQueryResult +'Preference: ' + IntToStr(objDnsRecord.Preference) + sLineBreak; strQueryResult := strQueryResult +'MailExchange: ' + objDnsRecord.MailExchange + sLineBreak; end; if ( objDnsRecord.Type_ = objConstants.nwDNS_TYPE_PTR )then begin strQueryResult := strQueryResult +'Type: PTR' + sLineBreak; strQueryResult := strQueryResult +'Name: ' + objDnsRecord.Name + sLineBreak; strQueryResult := strQueryResult +'Address: ' + objDnsRecord.Address + sLineBreak; end; if ( objDnsRecord.Type_ = objConstants.nwDNS_TYPE_SOA )then begin strQueryResult := strQueryResult + 'Type: SOA' + sLineBreak; strQueryResult := strQueryResult + 'Name: ' + objDnsRecord.Name + sLineBreak; strQueryResult := strQueryResult + 'Name Server: ' + objDnsRecord.NameServer + sLineBreak; strQueryResult := strQueryResult + 'MailBox: ' + objDnsRecord.MailBox + sLineBreak; strQueryResult := strQueryResult + 'Serial: ' + objDnsRecord.SerialNumber + sLineBreak; strQueryResult := strQueryResult + 'Refresh: ' + IntToStr(objDnsRecord.RefreshInterval) + sLineBreak; strQueryResult := strQueryResult + 'Retry Interval: ' + IntToStr(objDnsRecord.RetryInterval) + sLineBreak; strQueryResult := strQueryResult + 'Expiration Limit: ' + IntToStr(objDnsRecord.ExpirationLimit) + sLineBreak; strQueryResult := strQueryResult + 'Minimum TTL: ' + IntToStr(objDnsRecord.MinimumTTL) + sLineBreak; end; vt:=objDnsServer.GetNextRecord (); end; end; writeln(strQueryResult); WriteLn('Ready.'); sleep(3000); writeln('Done. Pressto close this window');readln; end.
You can download the complete samples here. There are many other working Network Component scripts on our site and shipped with the product.