sammysideupp
Posts: 16
Joined: Sun Feb 05, 2017 6:53 pm

Java UI for MAC Addresses (Hotspot)

Mon Feb 06, 2017 4:41 pm

I used the RPi3 Model B as a hotspot. Is there a way or a code to put the scanned MAC Addresses of the connected devices via the arp-scan command to the UI of our standalone Java application? Thanks a lot.
Last edited by sammysideupp on Mon Feb 06, 2017 4:58 pm, edited 1 time in total.

scotty101
Posts: 4106
Joined: Fri Jun 08, 2012 6:03 pm

Re: Java UI

Mon Feb 06, 2017 4:53 pm

Run the arp command using the Java ProcessBuilder.

The output of the command can be re-directed so that you can parse the text and place it in your GUI.
Electronic and Computer Engineer
Pi Interests: Home Automation, IOT, Python and Tkinter

sammysideupp
Posts: 16
Joined: Sun Feb 05, 2017 6:53 pm

Re: Java UI for MAC Addresses (Hotspot)

Wed Feb 22, 2017 7:25 am

Thanks for your help. I would also like to know the complete command that we can use to run the arp-scan in Java

User avatar
clicky
Posts: 501
Joined: Thu Oct 25, 2012 7:34 am

Re: Java UI for MAC Addresses (Hotspot)

Wed Feb 22, 2017 4:11 pm

I thought Java has something like that. What about:

https://docs.oracle.com/javase/8/docs/a ... rface.html

I admit I never tried it, but it is worth giving it a go...
-----------------------------------------------------------------
Scrap this - I must have been so tired when I wrote it.

knute
Posts: 612
Joined: Thu Oct 23, 2014 12:14 am
Location: Texas
Contact: Website

Re: Java UI for MAC Addresses (Hotspot)

Tue Feb 28, 2017 1:14 am

You'll have to play with the size of the ARP component and its font to get it to all fit how you want.

Code: Select all

import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
import javax.swing.*;

public class ARP extends JPanel implements Runnable {
    private volatile Thread thread;
    private volatile boolean runFlag;
    private final java.util.List<String> lines = new ArrayList<>();

    public ARP() {
        setFont(new Font(Font.MONOSPACED,Font.PLAIN,12));
        setPreferredSize(new Dimension(600,250));
    }

    public void start() {
        System.out.println("start");
        if (!runFlag) {
            runFlag = true;
            thread = new Thread(this,"ARP");
            thread.start();
        }
    }

    public void run() {
        System.out.println("run");
        boolean WIN =
         System.getProperty("os.name").toLowerCase().contains("windows");
        String[] command =
         WIN ? new String[] {"arp","-a"} : new String[] {"arp"};
        ProcessBuilder pb = new ProcessBuilder(command);
        while (runFlag) {
            BufferedReader br = null;
            try {
                Process proc = pb.start();
                System.out.println("process started");
                br = new BufferedReader(new InputStreamReader(
                 proc.getInputStream()));
                lines.clear();
                String str;
                while ((str = br.readLine()) != null)
                    lines.add(str);
                repaint();

                Thread.sleep(10000);
            } catch (IOException|InterruptedException ex) {
                lines.clear();
                lines.add(ex.toString());
            } finally {
                if (br != null)
                    try {
                        br.close();
                    } catch (IOException ioe) {
                        ioe.printStackTrace();
                    }
            }
        }
    }

    public void stop() {
        System.out.println("stop");
        runFlag = false;
        thread.interrupt();
    }

    public void paintComponent(Graphics g2D) {
        Graphics2D g = (Graphics2D)g2D;

        g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
         RenderingHints.VALUE_ANTIALIAS_ON);

        g.setColor(Color.WHITE);
        g.fillRect(0,0,getWidth(),getHeight());

        g.setColor(Color.BLACK);
        FontMetrics fm = g.getFontMetrics();
        int x = 10;
        int y = fm.getHeight();
        if (lines.size() > 2) {
            g.drawString(lines.get(0),x,y);
            y += fm.getHeight();
            g.drawString(lines.get(1),x,y);
            for (int i=2; i<lines.size(); i++) {
                y += fm.getHeight();
                g.drawString(lines.get(i),x,y);
            }
        }
    }

    public static void main(String... args) {
        EventQueue.invokeLater(() -> {
            ARP arp = new ARP();
            JFrame f = new JFrame("ARP");
            f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
            f.addWindowListener(new WindowAdapter() {
                public void windowClosing(WindowEvent we) {
                    arp.stop();
                }
            });
            f.add(arp,BorderLayout.CENTER);
            f.pack();
            f.setLocationRelativeTo(null);
            f.setVisible(true);
            arp.start();
        });
    }
}

Return to “Java”