简体   繁体   中英

Use dnsjava to get hostname from ip address from 192.168.1.1 to 192.168.1.254

I am trying to use dnsjava in an android app to find hostnames of devices in my local wifi network. Below is the code used:

try
{
String ipAddress = "33.1.168.192";
String dnsblDomain = "in-addr.arpa";
Record[] records;

Lookup lookup = new Lookup(ipAddress + "." + dnsblDomain, Type.PTR);
SimpleResolver resolver = new SimpleResolver();
resolver.setAddress(InetAddress.getByName("192.168.1.1"));
lookup.setResolver(resolver);
records = lookup.run();

if(lookup.getResult() == Lookup.SUCCESSFUL)
{
    for (int i = 0; i < records.length; i++)
    {
        if(records[i] instanceof PTRRecord)
        {
            PTRRecord ptr = (PTRRecord) records[i];
            System.out.println("DNS Record: " + records[0].rdataToString());
        }
    }
} else {
    System.out.println("Failed lookup");
}

} 
catch(Exception e) 
{
System.out.println("Exception: " + e);
}

The code was taken from the below link and it seems to work there for OP: any way to discover Android devices on your network?

192.168.1.33 is an active device on my wifi network . 192.168.1.1 is the router IP . The code reaches "Failed lookup" everytime .

I am not sure where I am going wrong as I am new to dnsJava and Networks. An additional question is , will this yield perfect result when scanned over all 254 ip's ? I am thinking of using this code in prod and need to be sure of that .

Any help is very much appreciated.

PTR records for reverse names are not stored in the order you're thinking. In general terms for IP ABCD you need to resolve DCBAin-addr.arpa , so you'll need to reverse the order of the IP components.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM