I am in the fun position of having a Raspberry Pi project to build for my day job. It brought to light a familiar challenge of how to trouble-shoot and connect to headless Raspberry Pi boards when you don’t necessarily know the host or IP address.
Instead of grabbing a HDMI cable (and converter), monitor, and keyboard, run this Python script from a familiar command line (might need to run as sudo) to get a color-coded output:
Code
You will need python-nmap module installed.
Get the full code here at this gist.
The bulk of the heavy lifting is in these lines:
nm = nmap.PortScanner()
nm.scan('10.0.1.0/24', '22')
There we are scanning my local IP addresses and the SSH port, port 22.
Once done (can take a few seconds, depending on the size of your network), you can go ahead and iterate over the results:
for host in nm.all_hosts():
(I sort the results but as the IP addresses are not padded with 0, the order is not exactly numerical even after sorting.)
All that is left is to output the info we want, along with a sprinkle of ANSI color coding for good measure:
print(u"\u001b[37m" + "{} ({}) {}\u001b[0m".format(nm[host].hostname(), host, mac))
This sets the forecolor to white, outputs the hostname, IP, and MAC address, then resets the text color.
Extending the code
Of course, this is useful for more than just seeing what your Pi is called or the IP address. Gathering hostnames and mac address could be used for logging the devices on your wifi network over time, or even announce your entry to the building (example previously using Bluetooth).