Pi-oi: a Tool for Finding the Local IP Address of Your Raspberry Pi
Finding your Raspberry Pi's IP address to open an SSH connection to sync your devices.
Join the DZone community and get the full member experience.
Join For FreeIntroduction
A Raspberry Pi machine is the most interesting thing I've bought this year. Why is it interesting? I think because it’s small and very powerful, it can run any program written in my favourite languages like Java, Javascript (or Node.js), and PHP. It’s an awesome machine I couldn't imagine when I was a student.
I want to write some small applications in Raspberry Pi to control my home appliances remotely, and the first thing I have to do before working with Raspberry Pi is open an SSH connection to it. It means that I must know the local IP address of the device. I have tried using some tools to scan all devices in my local network to find the dynamic IP that the router set for my Raspberry Pi, including arp, ping, nmap, Angry IP scanner (on Windows), and Smart IP scanner (on Windows), but all of them did not satisfy me because:
- The command line tools (arp, ping, nmap) usually couldn’t find my RPi and they did not work like some instructions I found on the Internet.
- IP scanner tool (Angry IP scanner, Smart IP scanner) could scan all machines on my local network in about 3 minutes, and after getting the list of IP addresses I had to check each IP consequently by attempting to open an ssh connection to port 22 to know whether or not it’s my Raspberry Pi. This is really not a convenient way!
So I decided to develop my own algorithm to resolve all these stuffs. The algorithm uses two following knowledge:
- In a local network, the dynamic IP addresses usually are provided in continuous ranges. For example, 192.168.1.100, 192.168.1.101 … So if I find out an active IP address, I will continue expanding the scanning range with assumption the other active address could be very near the current address.
- Raspberry Pi usually installs a Linux OS and runs a SSH server at port 22. If I try to open a socket connection to this port, it will response information about its SSH server version and OS, for example: “ SSH-2.0-OpenSSH_6.0p1 Debian-4”, “ SSH-2.0-OpenSSH_6.6.1p1 Ubuntu-2ubuntu2.3”, based on this information we can guess the Pi’s address, for example my Pi installs Debian OS so the first one is my Pi.
Pi-oi algorithm
Following is the pseudo code of the algorithm for single thread scanning:
myLocalAddresses <— get local IP addresses //example: 192.168.1.23
for each myLocalAddress in myLocalAddresses do
networkPrefix <— get network prefix of myLocalAddress // example: 192.168.1
initPivot <— get host number of myLocalAddress // example: 23
pendingHostNumbers <— [1,255] - {initPivot} // pending for scanning
pivot, scanningRange <— null
while (pendingHostNumbers is not empty) OR (scanningRange is not null) OR (pivot is not null) do
if scanningRange is null and pivot is null then
scanningRange <— pick range from pendingHostNumbers around initPivot
else if scanningRange is not empty then
scan all ip addresses in scanningRange and print out result if found a machine open SSH port
if last ip address in scanningRange is active
scanningRange <— pick a range from pendingHostNumbers around scanningRange.last
else if first ip address in scanningRange is active
scanningRange <— pick a range in pendingHostNumbers around scanningRange.first
else
scanningRange <— null
pivot <— pick a new pivot from pendingHostNumbers
end if
else if pivot is not null then
scan pivot
if pivot is active then
scanningRange <— pick a range from pendingHostNumbers around pivot
else
pivot <— pick a new pivot from pendingHostNumbers
end if
else
pivot <— pick a new pivot from pendingHostNumbers
scanningRange <— null
end if
end of while
end of for
In the implementation I split the range [1,255] in 5 continuous parts and start a 5-thread executor-service for scanning faster.
Implementation and Test Result
I used Java to implement this algorithm and tested in some local networks. Result is Pi-oi usually found my Raspberry Pi in less than 30 seconds. Following are shot screens of my tests:
Test in my home:
Test at my company:
Visit and fork my project at: http://github.com/thoqbk/pi-oi
Opinions expressed by DZone contributors are their own.
Comments