Download ActiveSocket Network Communications Toolkit 4.1  (5094 KB - .exe file)
Download Manual  (505 KB - .htm file)
DNS NsLookup using Visual Basic
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.
ActiveSocket provides an easy-to-use development/scripting interface to use the same operations as NsLookup, through the DnsServer class.
By using ActiveSocket, you can very easily create or enhance Windows applications/scripts with DNS lookup features.
ActiveSocket features the following: ICMP, HTTP and HTTPs (supporting for proxy servers, secure web sites), FTP, DNS, Telnet, NTP time protocol, RSH remote shell script interface, SNMP (Simple Network Management Protcol), SNMP Traps, Sockets, WOL (Wake-On-LAN), and more.
Step 1: Download and install the ActiveSocket Toolkit
Download the ActiveSocket Toolkit from the ActiveXperts Download Site and start the installation.
The installation guides you through the installation process.
Step 2: Create a new Visual Basic project
Launch 'Microsoft Visual Basic' from the Start menu, and choose 'New' from the 'File Menu'. The 'New Project' dialog appears.
Select 'Standard Exe' and click 'OK':

(Click on the picture to enlarge)
Step 3: Refer to the ActiveSocket Library and create the objects
A new Project is created, with a blank form.
First, you must add a reference to ActiveSocket in the project to be able to use the object. To do so, choose 'References...' from the 'Project' menu.
In the 'References' dialog that pops up, enable the 'ActiveSocket 3.1 Type Library' reference as shown in the following picture:

(Click on the picture to enlarge)
Click 'OK' to close the 'References...' dialog.
Then, select the Project form and choose 'View Code' from the context menu:

(Click on the picture to enlarge)
On top of your code, declare the following object:
Public objDnsServer As DnsServer
Step 4: Create the object
From the Code window, select 'Form'. The Private Sub 'Form_Load()' will be displayed now.
In the 'Form Load' function, create the object in the following way:
Set objDnsServer = CreateObject("ActiveXperts.DnsServer")
Step 5: Query an hostname
When the required DNS object is created, you can implement the code to query a hostname on a DNS server:
Public objDnsServer As DnsServer
Public objConstants As SocketConstants
Private Declare Function GetTempPath Lib "kernel32" Alias "GetTempPathA" (ByVal nBufferLength As Long, ByVal lpBuffer As String) As Long
Private Const MAX_PATH = 260
'///////////////////////////////////////////////////////////////////////
Private Sub CommandLookup_Click()
Dim lType As Long
Dim objDnsRecord As DnsRecord
Select Case (ComboType.ListIndex)
Case 0
lType = objConstants.asDNS_TYPE_A
Case 1
lType = objConstants.asDNS_TYPE_NS
Case 2
lType = objConstants.asDNS_TYPE_CNAME
Case 3
lType = objConstants.asDNS_TYPE_SOA
Case 4
lType = objConstants.asDNS_TYPE_PTR
Case 5
lType = objConstants.asDNS_TYPE_MX
Case 6
lType = objConstants.asDNS_TYPE_AAAA
Case 7
lType = objConstants.asDNS_TYPE_ANY
End Select
objDnsServer.Server = TextServer.Text
objDnsServer.LogFile = TextLogFile.Text
objDnsServer.Lookup TextHost.Text, lType
ListResult.Clear
If (GetResult() = 0) Then
Set objDnsRecord = objDnsServer.GetFirstRecord
On Error Resume Next
While (objDnsServer.LastError = 0)
Select Case objDnsRecord.Type
Case objConstants.asDNS_TYPE_A
ListResult.AddItem ("Type: A")
ListResult.AddItem vbTab & "Name: " & objDnsRecord.Name
ListResult.AddItem vbTab & "IPv4 Address: " & objDnsRecord.Address
Case objConstants.asDNS_TYPE_AAAA
ListResult.AddItem "Type: AAAA"
ListResult.AddItem vbTab & "Name: " & objDnsRecord.Name
ListResult.AddItem vbTab & "IPv6 Address: " & objDnsRecord.Address
Case objConstants.asDNS_TYPE_CNAME
ListResult.AddItem "Type: CNAME"
ListResult.AddItem vbTab & "Name: " & objDnsRecord.Name
ListResult.AddItem vbTab & "Alias: " & objDnsRecord.Address
Case objConstants.asDNS_TYPE_MX
ListResult.AddItem "Type: MX"
ListResult.AddItem vbTab & "Name: " & objDnsRecord.Name
ListResult.AddItem vbTab & "Preference: " & objDnsRecord.Preference
ListResult.AddItem vbTab & "Mail Exchange: " & objDnsRecord.MailExchange
Case objConstants.asDNS_TYPE_NS
ListResult.AddItem "Type: NS"
ListResult.AddItem vbTab & "Name: " & objDnsRecord.Name
ListResult.AddItem vbTab & "Name Server: " & objDnsRecord.NameServer
Case objConstants.asDNS_TYPE_PTR
ListResult.AddItem "Type: PTR"
ListResult.AddItem vbTab & "Name: " & objDnsRecord.Name
ListResult.AddItem vbTab & "Host: " & objDnsRecord.Address
Case objConstants.asDNS_TYPE_SOA
ListResult.AddItem "Type: SOA"
ListResult.AddItem vbTab & "Name: " & objDnsRecord.Name
ListResult.AddItem vbTab & "Name Server: " & objDnsRecord.NameServer
ListResult.AddItem vbTab & "MailBox: " & objDnsRecord.MailBox
ListResult.AddItem vbTab & "Serial: " & objDnsRecord.SerialNumber
ListResult.AddItem vbTab & "Refresh: " & objDnsRecord.RefreshInterval
ListResult.AddItem vbTab & "Retry Interval: " & objDnsRecord.RetryInterval
ListResult.AddItem vbTab & "Expiration Limit: " & objDnsRecord.ExpirationLimit
ListResult.AddItem vbTab & "Minimum TTL: " & objDnsRecord.MinimumTTL
End Select
Set objDnsRecord = objDnsServer.GetNextRecord
On Error Resume Next
Wend
End If
End Sub
'///////////////////////////////////////////////////////////////////////
Private Sub CommandView_Click()
If FileExists(TextLogFile.Text) = True Then
Shell "notepad " + TextLogFile.Text, vbNormalFocus
End If
End Sub
'///////////////////////////////////////////////////////////////////////
Public Function FileExists(sFileName As String) As Boolean
FileExists = CBool(Len(Dir$(sFileName))) And CBool(Len(sFileName))
End Function
'///////////////////////////////////////////////////////////////////////
Private Sub Form_Load()
Set objDnsServer = CreateObject("ActiveXperts.DnsServer")
Set objConstants = CreateObject("ActiveXperts.ASConstants")
SetDefaultLogFile
ComboType.AddItem ("A Record")
ComboType.AddItem ("NS Record")
ComboType.AddItem ("CNAME Record")
ComboType.AddItem ("SOA Record")
ComboType.AddItem ("PTR Record")
ComboType.AddItem ("MX Record")
ComboType.AddItem ("AAAA Record")
ComboType.AddItem ("ANY Record")
ComboType.ListIndex = 7
End Sub
'///////////////////////////////////////////////////////////////////////
Private Function GetResult() As Long
Dim lResult As Long
lResult = objDnsServer.LastError
TextResult.Text = lResult & " : " & objDnsServer.GetErrorDescription(lResult)
GetResult = lResult
End Function
'///////////////////////////////////////////////////////////////////////
Private Function SetDefaultLogFile()
Dim Buffer As String
Buffer = Space(MAX_PATH)
If GetTempPath(MAX_PATH, Buffer) <> 0 Then
TextLogFile.Text = Left$(Buffer, InStr(Buffer, vbNullChar) - 1) & "DnsLog.txt"
Else
TextLogFile.Text = "C:\DnsLog.txt"
End If
End Function
'///////////////////////////////////////////////////////////////////////
You can download the complete sample from our ftp site ftp.activexperts-labs.com/samples/asocket.
There are many other working ActiveSocket scripts on our site and shipped with the product.
The ActiveSocket tool is a Network Communications ActiveX software component (SDK).
This control supports SNMP, SMTP, POP3, Telnet, TCP, NTP, RSH, HTTP, HTTPs, FTP, DNS, ICMP and more, and can be used by any Windows development platform,
including Visual Basic .NET, Visual CSharp .NET,
ASP .NET (VB,CS),
ASP,
Visual Basic,
Visual Studio/Visual C++,
Delphi,
PHP,
ColdFusion,
HTML,
VBScript and any other ActiveX/COM compliant platform. The ActiveSocket Toolkit is an ActiveXperts Software B.V. Product.
|