How can I query the DNS server

Posted on 2003-11-05
Medium Priority
Last Modified: 2008-02-01
I need to write a number of reports/cubes using the ISA log as the main source of data.

Unfortunately the ISA log only contains the IP address of the client computer and not it's name.
In order to determine the computer's name I (think) I need to read the DNS using the ip address as a key or search argument.

Does anyone know if this is possible or an easy way to determine a computers name from its IP address?

Ideally I woulld like to issue a query from an sql server stored procedure, but failing that a vb.net program.

Thanks in advance

Question by:niall_moore
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
  • +1

Expert Comment

ID: 9691449
Try this, it is a complete vb / asp solution, but you could scavenge what you need from here.


Expert Comment

ID: 9691579
This is what you need:

HOW TO: Determine Computers Name from IP Address

Option Explicit

Private Const WSADescription_Len As Long = 256
Private Const WSASYS_Status_Len As Long = 128
Private Const WS_VERSION_REQD As Long = &H101
Private Const IP_SUCCESS As Long = 0
Private Const SOCKET_ERROR As Long = -1
Private Const AF_INET As Long = 2

Private Type WSADATA
  wVersion As Integer
  wHighVersion As Integer
  szDescription(0 To WSADescription_Len) As Byte
  szSystemStatus(0 To WSASYS_Status_Len) As Byte
  imaxsockets As Integer
  imaxudp As Integer
  lpszvenderinfo As Long
End Type

Private Declare Function WSAStartup Lib "wsock32" _
  (ByVal VersionReq As Long, _
   WSADataReturn As WSADATA) As Long
Private Declare Function WSACleanup Lib "wsock32" () As Long
Private Declare Function inet_addr Lib "wsock32" _
  (ByVal s As String) As Long
Private Declare Function gethostbyaddr Lib "wsock32" _
  (haddr As Long, _
   ByVal hnlen As Long, _
   ByVal addrtype As Long) As Long
Private Declare Sub CopyMemory Lib "kernel32" _
   Alias "RtlMoveMemory" _
  (xDest As Any, _
   xSource As Any, _
   ByVal nbytes As Long)
Private Declare Function lstrlen Lib "kernel32" _
   Alias "lstrlenA" _
  (lpString As Any) As Long
Public Function SocketsInitialize() As Boolean
   SocketsInitialize = WSAStartup(WS_VERSION_REQD, WSAD) = IP_SUCCESS
End Function

Public Sub SocketsCleanup()
   If WSACleanup() <> 0 Then
       MsgBox "Windows Sockets error occurred in Cleanup.", vbExclamation
   End If
End Sub

Public Function GetHostNameFromIP(ByVal sAddress As String) As String
   Dim ptrHosent As Long
   Dim hAddress As Long
   Dim nbytes As Long
   If SocketsInitialize() Then
     'convert string address to long
      hAddress = inet_addr(sAddress)
      If hAddress <> SOCKET_ERROR Then
        'obtain a pointer to the HOSTENT structure
        'that contains the name and address
        'corresponding to the given network address.
         ptrHosent = gethostbyaddr(hAddress, 4, AF_INET)
         If ptrHosent <> 0 Then
           'convert address and
           'get resolved hostname
            CopyMemory ptrHosent, ByVal ptrHosent, 4
            nbytes = lstrlen(ByVal ptrHosent)
            If nbytes > 0 Then
               sAddress = Space$(nbytes)
               CopyMemory ByVal sAddress, ByVal ptrHosent, nbytes
               GetHostNameFromIP = sAddress
            End If
         Else: MsgBox "Call to gethostbyaddr failed."
         End If 'If ptrHosent
      Else: MsgBox "String passed is an invalid IP."
      End If 'If hAddress
   Else: MsgBox "Sockets failed to initialize."
   End If  'If SocketsInitialize
End Function

Private Sub Command1_Click()
   Text2.Text = GetHostNameFromIP(Text1.Text)
End Sub

~ fantasy ~

LVL 67

Expert Comment

ID: 9691598
Not sure how fancy you want to get with it.
A simple PING -A will resolve the IP address
NSLOOKUP will resolve and deliver DNS server information.

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.


Author Comment

ID: 9691768
What I want to do is for a given computer's ip address on my company's network determine the name of that computer.

IQ150 - none of the examples on that site do what I want to do.

Fantasy1001 - the code you present is VB6, whish I dont have access to - so I cannot test it

sirbounty - neither Ping nor NSLookup return what I want - Ping just tells me if a computer is logged on(in my case it may not be) and NSLookup works the other way round (it converts a name to an IP)

Accepted Solution

fantasy1001 earned 375 total points
ID: 9691817
Try this:

Private Function IPtoHost(ByVal IPstr As String) As String
   Dim dns As System.Net.Dns
   Dim Host As New System.Net.IPHostEntry()
   Host = dns.GetHostByAddress(IPstr)
   IPtoHost = Host.HostName
Catch err As Exception
End Try
End Function

~ fantasy ~
LVL 67

Expert Comment

ID: 9693416
Nslookup should work for you.  It queries the information stored in DNS.  If you run it against the IP address, it should give you the name.
The first line is the DNS server name, followed by its address.
The second line is the fully qualified name of the client, followed by its address (using this method - the address you supplied).
For instance - if the address I'm looking at is and I ping it, I'll get a request timed out.
If I ping it with -a, I'll get the name of the host it's registered too, but 1 to 4 'Request timed out' (if I limit it by -n 1)
However, if I type
I'll receive something like this: (whether the device is powered on or not)

Server: Alpha3.galaga.net

Name: Pod7.outerrealm.galaga.net

Maybe I'm not completely understanding your question?  Can you provide more detail?

Author Comment

ID: 9697518
Sirbounty - I have accepted Fantasy's answer as I want to be able to do this programatically but I tried nslookup as you suggest and I dont get similar results.
I get

Server:   []

but thanks for your help anyway

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
The SignAloud Glove is capable of translating American Sign Language signs into text and audio.
Introduction to Processes
Suggested Courses
Course of the Month8 days, 14 hours left to enroll

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question