ActiveSocket Toolkit Add network capabilities to any Windows or .NET application

Quicklinks


DNS NsLookup using Powershell

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: 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.

Step 1: Download and install the ActiveSocket Toolkit

Download ActiveSocket 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 Powershell 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 ActiveSocket Toolkit.

Step 3: Create the ActiveSocket object in Powershell

Create a new Powershell file called DEMO.PS1.

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.

Create the ActiveSocket object(s) like this:

$objDnsServer = new-object -comobject ActiveXperts.DnsServer

Now, add the following lines to the file to have your fist ActiveSocket Powershell program:

Write-Host "ActiveSocket Version " $objDnsServer.Version "; Build " $objDnsServer.Build "; Module " $objDnsServer.Module
Write-Host "Expiration date: " $objDnsServer.ExpirationDate

Step 4: Execute a DNS lookup query

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

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

#################################################################################
# ActiveSocket - Powershell script
# © Copyright ActiveXperts Software B.V.
#
# For more information about ActiveSocket, please
# visit the online ActiveSocket page at:
# http://www.activexperts.com
#################################################################################
#  DNS Sample: simple sample to show how to lookup a hostname on a dns 
#  server. 
#################################################################################

cls

#################################################################################
# Functions --------------------------------------------------------------------#
#################################################################################


#################################################################################
# ReadInput --------------------------------------------------------------------#

function ReadInput($strTitle, $strDefault, $bAllowEmpty)
{
	$strReturn = ""
	do
		{
		     $strInput = Read-host $strTitle, $strDefault
		     if($strInput -ne "") 
				{
		          $strReturn = $strInput
				}
			
			if($bAllowEmpty -eq 1)
				{
					break
				}
		}		
	while($strReturn -eq "")

	return $strReturn
}
	
#################################################################################
# THE SCRIPT ITSELF ------------------------------------------------------------#
#################################################################################

$objDnsServer = new-object -comobject ActiveXperts.DnsServer
$objConstants = new-object -comobject ActiveXperts.ASConstants

# Write some information to console
Write-Host "ActiveSocket Version " $objDnsServer.Version "; Build " $objDnsServer.Build "; Module " $objDnsServer.Module
Write-Host "Expiration date: " $objDnsServer.ExpirationDate

$objDnsServer.LogFile	 = "C:\DnsLog.txt"
$objDnsServer.Server     = ReadInput "Enter a DNS server" "[ ns1.interstroom.nl ]" 0
$strHost                 = ReadInput "Enter a hostname to lookup" "[ www.snmptools.net ]" 0
$objDnsServer.Lookup($strHost, $objConstants.asDNS_TYPE_ANY)
Write-Host "Lookup, result: " $objDnsServer.LastError " ("  $objDnsServer.GetErrorDescription($objDnsServer.LastError) ")"
if($objDnsServer.LastError -ne 0)
	{
		exit
	}	

if($objDnsServer.IsAuthoritative)
	{
		Write-Host "Server is an authority for this domain"
	}	
else
	{
		Write-Host "Server is not an authority for this domain"
	}	

Write-Host

$objDnsRecord = $objDnsServer.GetFirstRecord()
#On Error Resume Next
	
while($objDnsServer.LastError -eq 0)
	{
		switch($objDnsRecord.Type)
			{
				$objConstants.asDNS_TYPE_A
					{
						Write-Host "Type             : A"
						Write-Host "Name             : " $objDnsRecord.Name
						Write-Host "IPv4 Address     : " $objDnsRecord.Address
					}	
				$objConstants.asDNS_TYPE_AAAA
					{
				        Write-Host "Type             : AAAA"
				        Write-Host "Name             : " $objDnsRecord.Name
				        Write-Host "IPv6 Address     : " $objDnsRecord.Address
					}	
				$objConstants.asDNS_TYPE_CNAME
					{
				        Write-Host "Type             : CNAME"
				        Write-Host "Name             : " $objDnsRecord.Name
				        Write-Host "Alias            : " $objDnsRecord.Address
					}	
				$objConstants.asDNS_TYPE_MX 
					{
				        Write-Host "Type             : MX"
				        Write-Host "Name             : " $objDnsRecord.Name
				        Write-Host "Preference       : " $objDnsRecord.Preference
				        Write-Host "Mail Exchange    : " $objDnsRecord.MailExchange
					}	
				$objConstants.asDNS_TYPE_NS
					{
				        Write-Host "Type             : NS"
				        Write-Host "Name             : " $objDnsRecord.Name
				        Write-Host "Name Server      : " $objDnsRecord.NameServer
					}	
				$objConstants.asDNS_TYPE_PTR
					{
				        Write-Host "Type             : PTR"
				        Write-Host "Name             : " $objDnsRecord.Name
				        Write-Host "Host             : " $objDnsRecord.Address
					}	
				$objConstants.asDNS_TYPE_SOA
					{	
				        Write-Host "Type             : SOA"
				        Write-Host "Name             : " $objDnsRecord.Name
				        Write-Host "Name Server      : " $objDnsRecord.NameServer
				        Write-Host "MailBox          : " $objDnsRecord.MailBox
				        Write-Host "Serial           : " $objDnsRecord.SerialNumber
				        Write-Host "Refresh          : " $objDnsRecord.RefreshInterval
				        Write-Host "Retry Interval   : " $objDnsRecord.RetryInterval
				        Write-Host "Expiration Limit : " $objDnsRecord.ExpirationLimit
				        Write-Host "Minimum TTL      : " $objDnsRecord.MinimumTTL
					}
			}
			
	    Write-Host "TTL              : " $objDnsRecord.TTL "`n"
			
	    $objDnsRecord = $objDnsServer.GetNextRecord()
	}

To run the code, start Powershell and browse to the location of the file you just created. Enter .\Demo.ps1 to run the code. Notice that if the script is not working, you have to change the execution policy; you can do that with the following command:

Set-ExecutionPolicy -unrestricted

There are many working samples included with the product. You can also find them on the ActiveXperts FTP site: ftp.activexperts-labs.com/samples/network-component.