Monday, January 13, 2014

Find your headless RaspberryPI on the DHCP network

To configure a (Pidora) RaspberryPI to run in headless mode (without mouse/keyboard & display) is very simple: Just create an empty file named "headless" in the "/boot" directory. Now it starts with an IP from your DHCP server. The big question when you now want to SSH to your PI is: Which IP did it get?

Luckily, all PIs have similar MAC addresses, all starting with "B8:27:EB..." and making them easy to identify. Here's a (Linux, but "arp" is also available on Win) command to list all active network devices on the network and grep the potential PIs:

arp -a | grep b8:27:eb

And here we have it as a very simple Java program:

package raspberrypi;

import java.io.BufferedReader;
import java.io.InputStreamReader;

public class FindPIs {

    public static void main(String[] args) {
        try{
            Process p=Runtime.getRuntime().exec("cmd /c arp -a");
            p.waitFor();
            BufferedReader reader=new BufferedReader(new InputStreamReader(p.getInputStream()));
            String line=reader.readLine();
            while(line!=null) {
                if(line.contains("b8-27-eb")){
                    System.out.print("RaspberryPI --> ");
                }
                System.out.println(line);
                line=reader.readLine();
            }
        }
        catch(Exception e) {
            e.printStackTrace();
        }
    }
}

No comments:

Post a Comment