Get Hostname Of Wifi Hotspot Clients, Together With Banking Concern Fit If It Is Withal Connected.

Posted on

Previous posts demonstrate how to “Retrieve IP in addition to MAC addresses from /proc/net/arp” in addition to “Lookup manufacturer/vendor information from MAC address“. This post demonstrate how to buy the farm the Host Name of the connected clients.

It tin flaming last noted that if nosotros telephone scream upwardly getCanonicalHostName() or getHostName() methods of the InetAddressobjects, it volition provide  the IP address. If nosotros call getLocalHost().getCanonicalHostName() or getLocalHost().getHostName() methods, it volition provide the host name, “localhost”.

Also, if a devices disconnected, the file /proc/net/arp volition non updated immediately. We tin flaming compass upwardly one’s heed if it is however connected past times calling inetAddress.isReachable(timeout). If simulated returned, agency it is disconnected.

MainActivity.java

package com.blogspot.android_er.androidlistclient;  import android.os.AsyncTask; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.ListView; import android.widget.TextView;  import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.net.InetAddress; import java.net.UnknownHostException; import java.util.ArrayList;  world degree MainActivity extends AppCompatActivity {      Button btnRead;     TextView textResult;      ListView listViewNode;     ArrayList<Node> listNote;     ArrayAdapter<Node> adapter;      @Override     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.activity_main);         btnRead = (Button)findViewById(R.id.readclient);         textResult = (TextView)findViewById(R.id.result);          listViewNode = (ListView)findViewById(R.id.nodelist);         listNote = novel ArrayList<>();         ArrayAdapter<Node> adapter =                 novel ArrayAdapter<Node>(                         MainActivity.this,                         android.R.layout.simple_list_item_1,                         listNote);         listViewNode.setAdapter(adapter);          btnRead.setOnClickListener(new View.OnClickListener() {             @Override             world void onClick(View v) {                 novel TaskReadAddresses(listNote, listViewNode).execute();             }         });     }      degree Node {         String ip;         String mac;         String CanonicalHostName;         String HostName;         String LocalHostCanonicalHostName;         String LocalHostHostName;         String remark;         boolean isReachable;          Node(String ip, String mac){             this.ip = ip;             this.mac = mac;             queryHost();         }          @Override         world String toString() {             provide "IP: " + ip + "\n" +                     "MAC: " + mac + "\n" +                     "CanonicalHostName:\t" + CanonicalHostName + "\n" +                     "HostName:\t" + HostName + "\n" +                     "getLocalHost().getCanonicalHostName():\t" + LocalHostCanonicalHostName + "\n" +                     "getLocalHost().getHostName():\t" + LocalHostHostName + "\n" +                     "isReachable: " + isReachable +                     "\n" + remark;         }          somebody void queryHost(){             crusade {                 InetAddress inetAddress = InetAddress.getByName(ip);                 CanonicalHostName = inetAddress.getCanonicalHostName();                 HostName = inetAddress.getHostName();                 LocalHostCanonicalHostName = inetAddress.getLocalHost().getCanonicalHostName();                 LocalHostHostName = inetAddress.getLocalHost().getHostName();                 isReachable = inetAddress.isReachable(3000);              } grab (UnknownHostException e) {                 e.printStackTrace();                 remark = e.getMessage();             } grab (IOException e) {                 e.printStackTrace();                 remark = e.getMessage();             }         }     }      somebody degree TaskReadAddresses extends AsyncTask<Void, Node, Void> {          ArrayList<Node> array;         ListView listView;          TaskReadAddresses(ArrayList<Node> array, ListView v){             listView = v;             this.array = array;             array.clear();             textResult.setText("querying...");         }          @Override         protected Void doInBackground(Void... params) {             readAddresses();             provide null;         }          @Override         protected void onPostExecute(Void aVoid) {             textResult.setText("Done");         }          @Override         protected void onProgressUpdate(Node... values) {             listNote.add(values[0]);             ((ArrayAdapter)(listView.getAdapter())).notifyDataSetChanged();         }          somebody void readAddresses() {             BufferedReader bufferedReader = null;              crusade {                 bufferedReader = novel BufferedReader(new FileReader("/proc/net/arp"));                  String line;                 spell ((line = bufferedReader.readLine()) != null) {                     String[] splitted = line.split(" +");                     if (splitted != naught && splitted.length >= 4) {                         String ip = splitted[0];                         String mac = splitted[3];                         if (mac.matches("..:..:..:..:..:..")) {                             Node thisNode = novel Node(ip, mac);                             publishProgress(thisNode);                         }                     }                 }              } grab (FileNotFoundException e) {                 e.printStackTrace();             } grab (IOException e) {                 e.printStackTrace();             } finally{                 crusade {                     bufferedReader.close();                 } grab (IOException e) {                     e.printStackTrace();                 }             }         }     } }