You are here

Raspberry Pi Phone Home Script

For working with headless Raspberry Pis, I thought it was useful to have the Pi report its address(es) when it boots. Originally I wrote it as a one-liner, but I realized that it was useful to grab the MAC address for debugging porpoises. But to get that, you need to know which interface is active. And this was starting to sound less like a one-liner and more like a script. So I hacked one together in bash.

The script just iterates through the interfaces, checks to see if they're up, and then reports the info back to a script on the server.

#! /bin/bash
# rpi phone home script 20200228 sbrewer
for i in $(ls -1 /sys/class/net)
do
  #echo "Working on $i interface..."
  if [ $(cat /sys/class/net/$i/operstate) = "up" ]
  then
     echo "Reporting $i interface..."
     _IP=$(hostname -I) 
     _HW=$(cat /sys/class/net/$i/address)
     _HN=$(hostname)
     /usr/bin/wget -O/dev/null -q https://bcrc.bio.umass.edu/pitrack/?hwaddr=$_HW\&pi=$_HN\&ip=$_IP
     fi
done