Contact Info

Crumbtrail

ActiveXperts.com » Network Component » How to Use Network Component » DNS » VBScript

DNS NsLookup using VBScript

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 script

Create a new script using your favorite editor. You can simply use notepad. However, a VBScript editor is recommended, so you can browse through objects, objects properties and object functions.

You're now able to write a more advanced script to communicate using the Network Component.

Step 3: Create the Network Component object in VBScript

Create a new VBScript file called DEMO.VBS. It is recommended to insert the following line on top of your code:

Option Explicit

This statement requires that all variable names be defined (with the Dim statement), to avoid simple typos that can cause incredible headaches and long debugging sessions for something that should have never happened.

Now, declare the Network Component object(s):

Dim objDnsServer

Create the Network Component object(s) like this:

Set objDnsServer      = CreateObject( "AxNetwork.DnsServer" )

Now, add the following lines to the file to have your first Network Component VBScript program:

WScript.Echo "Version: " & objDnsServer.Version
WScript.Echo "License Status: " & objDnsServer.LicenseStatus

Appendix: Full source code

You can now connect to a (remote) DNS server and execute a query.

The following VBScript code shows how to lookup a hostname (A-record) and print its IP address:

' ********************************************************************
' ActiveXperts Network Component sample - DNS
'  DNS Sample: simple sample to show how to lookup a hostname on a dns 
'  server. 
' Written by ActiveXperts Software 
'   https://www.activexperts.com
' ********************************************************************

Option Explicit

Dim objDnsServer, objDnsRecord, objConstants, fso, strHost

Set objDnsServer = CreateObject( "AxNetwork.DnsServer" )
Set objConstants = CreateObject( "AxNetwork.NwConstants" )

' A license key is required to unlock this component after the trial period has expired.
' Call 'Activate' with a valid license key as its first parameter. Second parameter determines whether to save the license key permanently 
' to the registry (True, so you need to call Activate only once), or not to store the key permanently (False, so you need to call Activate
' every time the component is created). For details, see manual, chapter "Product Activation".
'
' objDnsServer.Activate "XXXXX-XXXXX-XXXXX", False

' Component info
Wscript.Echo "ActiveXpert Network Component " & objDnsServer.Version
Wscript.Echo "Build: " & objDnsServer.Build & vbCrLf & "Module: " & objDnsServer.Module
Wscript.Echo "License Status: " & objDnsServer.LicenseStatus & vbCrLf & "License Key: " & objDnsServer.LicenseKey & vbCrLf

' Set Logfile
Set fso = CreateObject( "Scripting.FileSystemObject")
objDnsServer.LogFile = fso.GetSpecialFolder(2) & "\Dns.log"
WScript.Echo "Log file: " & objDnsServer.LogFile & vbCrLf

objDnsServer.Server     = ReadInput( "Enter a DNS server", "ns1.interstroom.nl", False )
strHost                 = ReadInput(  "Enter a hostname to lookup", "www.snmptools.net", False )
objDnsServer.Lookup strHost, objConstants.nwDNS_TYPE_ANY
WScript.Echo "Lookup, result: " & objDnsServer.LastError & " (" & objDnsServer.GetErrorDescription( objDnsServer.LastError ) & ")"
If ( objDnsServer.LastError <> 0 ) Then
    WScript.Quit
End If

If ( objDnsServer.IsAuthoritative = True ) Then
    WScript.Echo "Server is an authority for this domain"
Else
    WScript.Echo "Server is not an authority for this domain"
End If

WScript.Echo

Set objDnsRecord = objDnsServer.GetFirstRecord
On Error Resume Next
	
While ( objDnsServer.LastError = 0 )

  Select Case objDnsRecord.Type 
    Case objConstants.nwDNS_TYPE_A
      WScript.Echo "Type             : A"
      WScript.Echo "Name             : " & objDnsRecord.Name
      WScript.Echo "IPv4 Address     : " & objDnsRecord.Address
    Case objConstants.nwDNS_TYPE_AAAA
      WScript.Echo "Type             : AAAA"
      WScript.Echo "Name             : " & objDnsRecord.Name
      WScript.Echo "IPv6 Address     : " & objDnsRecord.Address
    Case objConstants.nwDNS_TYPE_CNAME
      WScript.Echo "Type             : CNAME"
      WScript.Echo "Name             : " & objDnsRecord.Name
      WScript.Echo "Alias            : " & objDnsRecord.Address
    Case objConstants.nwDNS_TYPE_MX 
      WScript.Echo "Type             : MX"
      WScript.Echo "Name             : " & objDnsRecord.Name
      WScript.Echo "Preference       : " & objDnsRecord.Preference
      WScript.Echo "Mail Exchange    : " & objDnsRecord.MailExchange
    Case objConstants.nwDNS_TYPE_NS
      WScript.Echo "Type             : NS"
      WScript.Echo "Name             : " & objDnsRecord.Name
      WScript.Echo "Name Server      : " & objDnsRecord.NameServer
    Case objConstants.nwDNS_TYPE_PTR
      WScript.Echo "Type             : PTR"
      WScript.Echo "Name             : " & objDnsRecord.Name
      WScript.Echo "Host             : " & objDnsRecord.Address
    Case objConstants.nwDNS_TYPE_SOA
      WScript.Echo "Type             : SOA"
      WScript.Echo "Name             : " & objDnsRecord.Name
      WScript.Echo "Name Server      : " & objDnsRecord.NameServer
      WScript.Echo "MailBox          : " & objDnsRecord.MailBox
      WScript.Echo "Serial           : " & objDnsRecord.SerialNumber
      WScript.Echo "Refresh          : " & objDnsRecord.RefreshInterval
      WScript.Echo "Retry Interval   : " & objDnsRecord.RetryInterval
      WScript.Echo "Expiration Limit : " & objDnsRecord.ExpirationLimit
      WScript.Echo "Minimum TTL      : " & objDnsRecord.MinimumTTL
  End Select

  WScript.Echo "TTL              : " & objDnsRecord.TTL
  WScript.Echo
  
  Set objDnsRecord = objDnsServer.GetNextRecord

Wend



' ***************************************************************************
' Function ReadInput
' ***************************************************************************
Function ReadInput( ByVal strTitle, ByVal strDefault, ByVal bAllowEmpty )

 Dim strInput, strReturn
 
 Do
  strInput = inputbox( strTitle, "Enter value", strDefault )
  If ( strInput <> "" ) Then
   strReturn = strInput
  End If
 Loop until strReturn <> "" Or bAllowEmpty
 
 ReadInput = strReturn
End Function

You can download the complete samples here. There are many other working Network Component scripts on our site and shipped with the product.