Solved

How can I query the DNS server

Posted on 2003-11-05
7
377 Views
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


Niall
 
0
Comment
Question by:niall_moore
  • 2
  • 2
  • 2
  • +1
7 Comments
 

Expert Comment

by:IQ150
Comment Utility
Try this, it is a complete vb / asp solution, but you could scavenge what you need from here.

http://www.hexillion.com/samples/view_src.asp?name=NsLookup.inc.vbs.asp
0
 
LVL 5

Expert Comment

by:fantasy1001
Comment Utility
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
   Dim WSAD As WSADATA
   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
      SocketsCleanup
      Else: MsgBox "String passed is an invalid IP."
      End If 'If hAddress
   Else: MsgBox "Sockets failed to initialize."
   End If  'If SocketsInitialize
End Function

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

~ fantasy ~

0
 
LVL 67

Expert Comment

by:sirbounty
Comment Utility
Not sure how fancy you want to get with it.
A simple PING -A 123.45.67.89 will resolve the IP address
Also,
NSLOOKUP 123.45.67.89 will resolve and deliver DNS server information.
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 

Author Comment

by:niall_moore
Comment Utility
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)
0
 
LVL 5

Accepted Solution

by:
fantasy1001 earned 125 total points
Comment Utility
Try this:

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

~ fantasy ~
0
 
LVL 67

Expert Comment

by:sirbounty
Comment Utility
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 192.168.0.43 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
nslookup 192.168.0.43
I'll receive something like this: (whether the device is powered on or not)

Server: Alpha3.galaga.net
Address: 192.168.0.5

Name: Pod7.outerrealm.galaga.net
Address: 192.168.0.43

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

Author Comment

by:niall_moore
Comment Utility
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:   [10.65.4.2]
Address: 10.65.4.2

but thanks for your help anyway
Niall
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

RIA (Rich Internet Application) tools are interactive internet applications which have many of the characteristics of desktop applications. The RIA tools typically deliver output either by the way of a site-specific browser or via browser plug-in. T…
A short article about problems I had with the new location API and permissions in Marshmallow
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

762 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now