Results 1 to 9 of 9
  1. #1
    Join Date
    Feb 2017
    Posts
    4
    Rep Power
    0

    Default IP to Workstation name

    Hi,

    I want to try lookup IP Addresses to the Hostname. In Visual Basic it looks like this:

    Java Code:
    For Each Adresse In ListBox1.Items
                Try
                    Label4.Text = ("DNS lookup...: " & Address)
                    Dim DNSEntry As IPHostEntry = Dns.GetHostEntry(Address)
                    Me.Refresh()
    
                Catch ex As Exception
    
                    ListBox2.Items.Add(Address)
                    Me.Refresh()
                End Try
    
    
            Next

    But in Java I can't do it like this I think:

    Java Code:
    if (!inet.isReachable(500)){
            
               if (inet.getCanonicalHostName() == null){
                   System.out.println(inet + " NULL Parameter");
                 
               }
               else {
                   System.out.println(inet + " else loop");
                 
               }
          
            try {                    
                    System.out.println(inet);              
                    jTextArea1.append(inet.getHostName() + " exist\n");  
                    jTextArea1.update(jTextArea1.getGraphics());              
                    txtFld3.update(txtFld3.getGraphics());
        } catch (Exception e) {
                    System.out.println("Error " + e);
        }
          }

    So what the code should so: If IP Address is not reachable then try to lookup the Hostname, if there is no Hostname then do......x y z

    But I don't have a clue :D

    Thanks for help!

  2. #2
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,989
    Rep Power
    8

    Default Re: IP to Workstation name

    What message do you get? Do you get a hostname? Error? Anything?

    Second, what sorcery is this and why is it in there? I have been programming Java for the last 20 years, but never saw the need to update a Swing component like this. Please remove that.
    Java Code:
    jTextArea1.update(jTextArea1.getGraphics());              
    txtFld3.update(txtFld3.getGraphics());
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  3. #3
    Join Date
    Feb 2017
    Posts
    4
    Rep Power
    0

    Default Re: IP to Workstation name

    Hi SurfMan,
    thanks for answering. This sorcery updates the gui elements. Otherwise the IP addresses or the progressbar will not be updating during the IP Range scan.

    Well, I updated the code a little bit:

    Java Code:
    try {
                  	String test = InetAddress.getByName(inet+"").getCanonicalHostName();	
                   
                    
    	} catch (final UnknownHostException e) {
                    
                    jTextArea1.append(inet + " is free to use!\n");
                
    	}
    Now the programm goes into the exception and returns the IP Address. But every Address that is unreachable via ping and it doesn't matter if it is DNS resolvable or not.
    For example, it returns a IP Address that is resolveable with nslookup. :(

  4. #4
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,989
    Rep Power
    8

    Default Re: IP to Workstation name

    If you are doing UI work during a long-running task or on a different thread than the AWT/EDT thread, you should be using a SwingWorker and not the update(Graphics) method, or at least SwingUtilities.invokeLater().

    Why not use getHostName()? Following example works for me:
    Java Code:
    import java.net.InetAddress;
    import java.net.UnknownHostException;
    
    public class Lookup {
        public static void main(String[] args) {
    
            try {
                byte[] address = {62, (byte)210, 93, 105};
                InetAddress byAddress = InetAddress.getByAddress(address);
    
                System.out.println("byAddress = " + byAddress.getHostName());
    
            }
            catch (UnknownHostException e) {
                e.printStackTrace();
            }
    
        }
    }
    Output:
    Java Code:
    byAddress = web.java-forums.org
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  5. #5
    Join Date
    Feb 2017
    Posts
    4
    Rep Power
    0

    Default Re: IP to Workstation name

    Oh I had that output allready. This is the output:

    Java Code:
    byAddress= 192.168.50.1
    byAddress= pcName1.abc.local
    byAddress= pcName2.abc.local
    byAddress= 192.168.50.7
    byAddress= 192.168.50.18
    byAddress= 192.168.50.20
    But the code never gets to the exception. I think the code works right except the hostnames. I just need the IP Addresses.

    If I perform an nslookup to 192.168.50.7 then "192.168.50.7 was not found....non existent domain." This is the exception I expected.

  6. #6
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: IP to Workstation name

    If you're trying to find the host name for that IP address using nslookup you need to set your type to PTR. That will reference the
    in-addr.arpa domain for reverse lookups. Of course, that is probably unrelated to your problem with Java.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  7. #7
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,989
    Rep Power
    8

    Default Re: IP to Workstation name

    Have you tried using InetAddress.getByAddress() instead of InetAddress.getByName()? Does that make a difference?

    Besides that, I don't think a reverse DNS lookup proves whether the host is up or not. If your local OS, router, or ISP caches DNS requests, you can get the hostname <-> ip address conversion without the host being up.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  8. #8
    Join Date
    Feb 2017
    Posts
    4
    Rep Power
    0

    Default Re: IP to Workstation name

    If I use InetAddress.getByAddress() this is the output:
    Java Code:
    java.net.UnknownHostException: /192.168.50.0
    java.net.UnknownHostException: /192.168.50.2
    java.net.UnknownHostException: /192.168.50.4
    java.net.UnknownHostException: /192.168.50.5
    java.net.UnknownHostException: /192.168.50.6
    But behind 192.168.50.4 and 192.168.50.5 I get a DNS Workstation name with nslookup.

    I don't get it. Java can check an IP Address if it is reachable or not but can't check if there is a hostname behind an IP and return true or false (hey there is a host name behind this offline IP so this IP is not free to use) or something like that.

    Can I use a filter or something like that? The previous output seems to be okay. I just need to filter out the workstation names.

    So from:
    Java Code:
    byAddress= 192.168.50.1
    byAddress= pcName1.abc.local
    byAddress= pcName2.abc.local
    byAddress= 192.168.50.7
    byAddress= 192.168.50.18
    byAddress= 192.168.50.20
    to:
    Java Code:
    byAddress= 192.168.50.1
    byAddress= 192.168.50.7
    byAddress= 192.168.50.18
    byAddress= 192.168.50.20

  9. #9
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: IP to Workstation name

    You are correct. DNS caches can be quite long (sometimes weeks) because most host name to address mappings are stable. For ome hosts (e.g. mobile) the cache timeout is usually set to 0. But back to your point, the hosts don't respond to the DNS queries, their resident DNS servers do (or the local one if a recent query has been made). Using a ping (ICMP echo/echo reply) used to be one way to determine if the host is alive but sometimes those messages are blocked. If the service is known, just make a request to the port number and mark the reachability accordingly.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. Replies: 0
    Last Post: 03-02-2008, 07:15 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •