Retrieve IP and MAC addresses from /proc/net/arp

Last post "Display WiFi Hotspot clients by "cat /proc/net/arp"" display arp as human readable string, but not machine readable. It's another version to retrieve IP and MAC addresses, store in a ArrayList.


MainActivity.java
package com.blogspot.android_er.androidlistclient;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

Button btnRead;
TextView textResult;

ArrayList<Node> listNote;

@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);

listNote = new ArrayList<>();

btnRead.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
readAddresses();
textResult.setText("");
for(int i=0; i<listNote.size(); i++){
textResult.append(i + " ");
textResult.append(listNote.get(i).toString());
textResult.append("\n");
}
}
});
}

private void readAddresses() {
listNote.clear();
BufferedReader bufferedReader = null;

try {
bufferedReader = new BufferedReader(new FileReader("/proc/net/arp"));

String line;
while ((line = bufferedReader.readLine()) != null) {
String[] splitted = line.split(" +");
if (splitted != null && splitted.length >= 4) {
String ip = splitted[0];
String mac = splitted[3];
if (mac.matches("..:..:..:..:..:..")) {
Node thisNode = new Node(ip, mac);
listNote.add(thisNode);
}
}
}

} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally{
try {
bufferedReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

class Node {
String ip;
String mac;

Node(String ip, String mac){
this.ip = ip;
this.mac = mac;
}

@Override
public String toString() {
return ip + " " + mac;
}
}
}


layout/activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp"
android:orientation="vertical"
tools:context="com.blogspot.android_er.androidlistclient.MainActivity">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:autoLink="web"
android:text="http://android-er.blogspot.com/"
android:textStyle="bold" />

<Button
android:id="@+id/readclient"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAllCaps="false"
android:text="Read Ip/MAC addresses"/>

<TextView
android:id="@+id/result"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:typeface="monospace"
android:textSize="24sp"/>
</LinearLayout>


Next:
Lookup manufacturer info by MAC address, using www.macvendorlookup.com API
Get HostName of WiFi hotspot clients, and check if it is still connected

Retrieve IP and MAC addresses from /proc/net/arp Retrieve IP and MAC addresses from /proc/net/arp Reviewed by Pendik on 05.47 Rating: 5

Tidak ada komentar:

Diberdayakan oleh Blogger.