rinotsai
Posts: 2
Joined: Tue Feb 05, 2013 11:20 am

Search all Raspberry Pis in my local network!!

Tue Feb 05, 2013 11:28 am

I'd like to have a program that could find all the Raspberry Pis in my local network. Could anyone show me some ideas or samples for such an application?

User avatar
Davespice
Forum Moderator
Forum Moderator
Posts: 1662
Joined: Fri Oct 14, 2011 8:06 pm
Location: The Netherlands
Contact: Twitter

Re: Search all Raspberry Pis in my local network!!

Tue Feb 05, 2013 11:32 am

Hmm, I would guess you could do this by port scanning every IP address to see if 22 is open? Which would be the SSH server - but this would be dependent on the Linux distro being run on the particular Pi. Easy enough if you know they will always have Raspbian for instance?

You could probably do that with the netcat command. Info here.

Code: Select all

man netcat

pjc123
Posts: 913
Joined: Thu Mar 29, 2012 3:37 pm
Contact: Website

Re: Search all Raspberry Pis in my local network!!

Tue Feb 05, 2013 12:12 pm

You could use nmap. It has a multitude of advanced ways to search a network for specific network devices. A GUI front end is also available.
My Raspberry Pi Project Page:
https://www.flaminghellmet.com/launch/

jamiesk
Posts: 95
Joined: Mon Nov 26, 2012 8:48 pm
Location: Ipswich, Suffolk, England, UK.

Re: Search all Raspberry Pis in my local network!!

Tue Feb 05, 2013 12:12 pm

Ping all the IP addresses, and check the MAC addresses for the first 6 hex characters. Thats about the easiest way.
Pi1 (Nov 2012 loft)= 1KW immersion controller for Solar panel
Pi2 (Jan 2013 living room)=Play thing
Pi3 (Feb 2013 mobile)= Play thing with Tandy Ladder board,breakout board,Nokia display
http://www.raspberrypi.org/phpBB3/viewtopic.php?f=26&t=28193

User avatar
Davespice
Forum Moderator
Forum Moderator
Posts: 1662
Joined: Fri Oct 14, 2011 8:06 pm
Location: The Netherlands
Contact: Twitter

Re: Search all Raspberry Pis in my local network!!

Tue Feb 05, 2013 12:14 pm

jamiesk wrote:Ping all the IP addresses, and check the MAC addresses for the first 6 hex characters. Thats about the easiest way.
Ah yeah! If there is commonality for all Pis this would be a reliable way to detect them irrespective of what distro was running. Good one :)

mikerr
Posts: 2768
Joined: Thu Jan 12, 2012 12:46 pm
Location: UK
Contact: Website

Re: Search all Raspberry Pis in my local network!!

Tue Feb 05, 2013 12:24 pm

MAC address works for wired raspberry pi's

They all start b8:27:eb

Nice lookup here that you could integrate with for other manufacturers
http://hwaddress.com/

e.g.
http://hwaddress.com/?q=B8%3A27%3AEB%3A01%3A02 - gives raspberry pi
http://hwaddress.com/?q=raspberry - gives the raspberry mac address range

For wifi-attached Pi you'll get the mac address of the usb dongle though.
Android app - Raspi Card Imager - download and image SD cards - No PC required !

pjc123
Posts: 913
Joined: Thu Mar 29, 2012 3:37 pm
Contact: Website

Re: Search all Raspberry Pis in my local network!!

Tue Feb 05, 2013 12:30 pm

mikerr wrote: Nice lookup here that you could integrate with for other manufacturers
http://hwaddress.com/
Handy. I will have to add that to my bookmarks.
My Raspberry Pi Project Page:
https://www.flaminghellmet.com/launch/

User avatar
cyrano
Posts: 714
Joined: Wed Dec 05, 2012 11:48 pm
Location: Belgium

Re: Search all Raspberry Pis in my local network!!

Tue Feb 05, 2013 2:00 pm

jamiesk wrote:Ping all the IP addresses, and check the MAC addresses for the first 6 hex characters. Thats about the easiest way.
You could also ping the local broadcast adress (X.X.X.255) or simply 255.255.255.255. The last one is your local network, whatever private addresses are used.

You'll get replies from every device on the local net that responds to ping.

jamiesk
Posts: 95
Joined: Mon Nov 26, 2012 8:48 pm
Location: Ipswich, Suffolk, England, UK.

Re: Search all Raspberry Pis in my local network!!

Tue Feb 05, 2013 5:29 pm

cyrano wrote:
jamiesk wrote:Ping all the IP addresses, and check the MAC addresses for the first 6 hex characters. Thats about the easiest way.
You could also ping the local broadcast adress (X.X.X.255) or simply 255.255.255.255. The last one is your local network, whatever private addresses are used.

You'll get replies from every device on the local net that responds to ping.
I have found on larger networks that all machines cannot answer in the required time. Also there is a finite mac address table size, so ping, check, ping check......
Pi1 (Nov 2012 loft)= 1KW immersion controller for Solar panel
Pi2 (Jan 2013 living room)=Play thing
Pi3 (Feb 2013 mobile)= Play thing with Tandy Ladder board,breakout board,Nokia display
http://www.raspberrypi.org/phpBB3/viewtopic.php?f=26&t=28193

saintdev
Posts: 39
Joined: Mon Jun 18, 2012 10:56 pm

Re: Search all Raspberry Pis in my local network!!

Tue Feb 05, 2013 9:13 pm

This requires you have avahi-daemon running on each Pi.

Code: Select all

~ $ avahi-browse --terminate --resolve --parsable _workstation._tcp | grep '^=.*\\091b8\\05827\\058eb.*$'
Will give you some output like

Code: Select all

=;eth0;IPv6;rapier\032\091b8\05827\058eb\0584a\05814\05806\093;Workstation;local;rapier.local;2601:1:XXXX:XXXX:XXXX:XXXX:XXXX:XXXX;9;
=;eth0;IPv4;rapier\032\091b8\05827\058eb\0584a\05814\05806\093;Workstation;local;rapier.local;10.20.20.25;9;
You may want to just filter the IPv4 addresses if you have IPv6 dual stack, like I do. Otherwise I wouldn't worry about it.

Most likely would be easy to do with the Avahi API.
SELECT `signature` FROM `users` WHERE `username`='saintdev';
Empty set (0.00 sec)

User avatar
[email protected]
Posts: 2020
Joined: Tue Feb 07, 2012 2:14 pm
Location: Devon, UK
Contact: Website

Re: Search all Raspberry Pis in my local network!!

Tue Feb 05, 2013 10:23 pm

rinotsai wrote:I'd like to have a program that could find all the Raspberry Pis in my local network. Could anyone show me some ideas or samples for such an application?
If e.g. your LAN is 192.168.254.0/24 then create a bash script:

Code: Select all

#!/bin/bash
# findPi:
#       Find all Pi's on the LAN

fping -a -r1 -g 192.168.254.0/24  &> /dev/null
arp -n | fgrep " b8:27:eb"
This produces:

Code: Select all

192.168.254.30           ether   b8:27:eb:a5:16:f6   C                     eth0
192.168.254.33           ether   b8:27:eb:94:39:fd   C                     eth0
192.168.254.31           ether   b8:27:eb:87:37:da   C                     eth0
which is currently correct for my LAN.

It can be optimised, but I'll leave that as an excercise for you.

-Gordon
--
Gordons projects: https://projects.drogon.net/

efflandt
Posts: 359
Joined: Mon Dec 03, 2012 2:47 am
Location: Elgin, IL USA

Re: Search all Raspberry Pis in my local network!!

Wed Feb 06, 2013 7:24 pm

avahi-daemon as mentioned would be the easiest way. Then you can find them with Apple zeroconfig, Bonjour in Windows, or avahi-browse in Linux (so PuTTY in Windows or ssh in Linux can easily find them by hostname.local). To tell which is which, it would help to give each SD card a unique name in /etc/hostname and /etc/hosts.

I can even ssh into a Pi from my smart phone. Although, ConnectBot does not recognize zeroconfig names, I can find their IP's from the phone using a Zeroconfig Browser app.

User avatar
[email protected]
Posts: 2020
Joined: Tue Feb 07, 2012 2:14 pm
Location: Devon, UK
Contact: Website

Re: Search all Raspberry Pis in my local network!!

Wed Feb 06, 2013 7:29 pm

efflandt wrote:avahi-daemon as mentioned would be the easiest way. Then you can find them with Apple zeroconfig, Bonjour in Windows, or avahi-browse in Linux (so PuTTY in Windows or ssh in Linux can easily find them by hostname.local). To tell which is which, it would help to give each SD card a unique name in /etc/hostname and /etc/hosts.

I can even ssh into a Pi from my smart phone. Although, ConnectBot does not recognize zeroconfig names, I can find their IP's from the phone using a Zeroconfig Browser app.
Which is fine if the devices all run it. Mine don't.

-Gordon
--
Gordons projects: https://projects.drogon.net/

rinotsai
Posts: 2
Joined: Tue Feb 05, 2013 11:20 am

Re: Search all Raspberry Pis in my local network!!

Thu Feb 07, 2013 8:00 am

Wow!! Thanks for all the replies!! I'll try it!! Thanks for you all!!

rtfmoz
Posts: 7
Joined: Wed Mar 27, 2013 8:39 pm

Re: Search all Raspberry Pis in my local network!!

Tue Jun 11, 2013 1:03 am

Adjust network to suit...

sudo nmap -sP 192.168.0.0/24 | awk '/^Nmap/ { printf $5" " } /MAC/ { print }' - | grep Raspberry

User avatar
DaveDriesen
Posts: 113
Joined: Sun Mar 31, 2013 8:28 pm
Location: Top of the food chain
Contact: Website

Re: Search all Raspberry Pis in my local network!!

Tue Jun 11, 2013 7:43 am

FWIW, the arp command can also be of some use if your pi's are active..

The following crude oneliner extracts hostname, IP and hardware address:

Code: Select all

arp -a | sed 's/[()]//g' | sed 's/at //g' | awk '{print $1, $2, $3;}'
This method does not perform any scan, it will only report devices that have made it into the arp cache already. But the upside is that it is non-intrusive, you can script it to actively track the contents of the arp tables and maintain the results in a mysql table or whatever.

Dave Driesen
Linux dev and oldskool elite

joeschmo
Posts: 9
Joined: Sun Feb 24, 2013 12:19 am

Re: Search all Raspberry Pis in my local network!!

Sun Apr 26, 2015 1:19 pm

Im looking to do the same thing myself, are all of the PI's belong to you or will random ones be arriving to the network? In my case they all belong to me so I was thinking about making a virtual subnet in the interfaces file that way each one of the PI's could have its own unique IP that does not affect its connectivity otherwise and than you could run nmap in that subnet as it would be assumed that anything in that subnet is a pi. Also maybe Bonjour https://learn.adafruit.com/bonjour-zero ... x/overview

Return to “General programming discussion”