1

I am looking for a way to get IP address of known hostname at LAN .

The way i worked is to perform a network scan, The scan starts from 192.168.1.1 to 192.168.1.255 ,So i've tried InetAddress.getHostName(),InetAddress.getCanonicalAddress() but retrieves the same IP address.

I've tried make a scan using nslookup . it worked perfectly at java compiler, but it's not possible in android ..

I couldn't test dnsjava library bacause of errors at compiling heading by : NoClassDefFoundError,The file dnsjava-2.1.7.jar is added to libs directory.

any help ? *sorry for my english.

Hamza Hajeir
  • 119
  • 1
  • 8
  • maybe help u : https://stackoverflow.com/questions/34842698/inetaddress-getcanonicalhostname-returns-ip-instead-of-hostname – SAYE May 12 '18 at 21:10
  • Huh? Why would you be scanning anything to do a hostname lookup? I think you're confused. – Gabe Sechan May 12 '18 at 21:23
  • 1
    @SamNikzad I've checked it, first answer isn't working bacause of error mentioned above. couldn't deal with Properties class which used in second answer, cannot resolve method .put().. – Hamza Hajeir May 12 '18 at 21:33
  • @GabeSechan First thing i tried is to search using InetAddress.getbyName([hostname]), but it's not working anyway. Yeah it's working for websites as google.com,facebook.com, but not hostname appeared in DHCP list. So i am looking at reverse direction, scanning the network in a way carrying hostnames. – Hamza Hajeir May 12 '18 at 21:37
  • Why would it? DHCP != DNS. getHostByName does DNS lookups. The documentation says that getHostByName on an IP address will return the address, and just verify that it is n IP address. I think you need to describe exactly what you're passing in and what you would expect as output. Either you're confused on terminology and aren't explaining things well, or you're confused on internet technologies and are trying to do something that will never work. – Gabe Sechan May 12 '18 at 21:44
  • my goal is to find a pre-named micro controller's IP to send commands to it. Even if i program it to get a static IP, this static IP could be leased to another device. so when i send commands as api request, see [link](https://stackoverflow.com/questions/49121119/parse-an-api-link-message-as-a-server-in-c-arduino-ide) Command wont reach micro controller as i send as example : `http://192.168.1.8/?A=data1&B=data2/` the address `192.168.1.8` could be leased to another device. I found a way to change MicroController's hostname. so I am searching IP using this info. @GabeSechan – Hamza Hajeir May 13 '18 at 10:30
  • anyHelp Guys pls ? – Hamza Hajeir May 15 '18 at 22:17

1 Answers1

0

I've written a worked code (at least for my purpose).

I've programmed my microcontroller (ESP8266-NodeMCU) to write back an HTML response carrying a JSON object.

So I'm scanning my network by writing an http request to each address starting from 192.168.1.1 to 192.168.1.255 .

Notes :

  • I've used SyncHttpClient since i need to scan a large number of addresses. I've tried AsyncHttpClient and it doesn't catch the response.
  • I've tried (InetAddress.isReachable()) method to filter addresses, It doesn't work since it always return false.
  • I could't make HttpRequest without adding at least 1 query parameter to the Request.

    boolean found = false;
    
    void getIPAddress()  {
    
      for (int i = 1; i < 255; i++) {
        if (!found) {
            String tryIP = subnet + String.valueOf(i);
            Log.d(TAG, "Trying: " + tryIP);
            final String finalIP = tryIP;
    
            try {
    
                    SyncHttpClient client = new SyncHttpClient();
                    RequestParams params = new RequestParams();
                    params.put("check", "1");
    
                    client.get("http://" + finalIP , params, new JsonHttpResponseHandler() {
    
    
    
                        @Override
                        public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
                            try {
    
                                JSONObject jo = response;
                                String key = jo.getString("controller");
                                Log.d(TAG, "Success : " + response + " and key is : " + key);
    
                                if (key.equals("esplight")) {
    
                                    Log.d(TAG, "Found the ESP! Its IP is :" + finalIP);
    
                                    found = true;
    
    
                                }
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
    
                        }
    
                        @Override
                        public void onFailure(int statusCode, Header[] headers, Throwable throwable, JSONObject errorResponse) {
    
                            Log.d(TAG, "trying to connect failed ");
    
                        }
                    });
    
    
              } catch (Exception e) {
            }
        }
     }
    }
    
Hamza Hajeir
  • 119
  • 1
  • 8