Solved

How can I query the DNS server

Posted on 2003-11-05
7
382 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
ID: 9691449
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
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
   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
ID: 9691598
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
Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 

Author Comment

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

Accepted Solution

by:
fantasy1001 earned 125 total points
ID: 9691817
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
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 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
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:   [10.65.4.2]
Address: 10.65.4.2

but thanks for your help anyway
Niall
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

This is about my first experience with programming Arduino.
This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.

832 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