Raspberry Pi email IP at boot

I found a bunch of what seem complicated answers for how to do this. I made my own eventually just using bash and adding it to /etc/rc.local, turns out I didn’t need to have it in rc.local either, if I put it in /etc/network/if-up/ and made it executable.

#!/bin/bash
#Send an email with the local IP
#sleep added to give me a wait to the process during boot and before remove to give the #script some time to run. These may not be needed. Adjust as needed.
sleep 5
#grep -v added to trim out some extra info from the file
#date added so it would append a time stamp to file
ifconfig | grep inet | grep -v inet6 | grep -v 127.0.0.1 >> file.tmp | date >> file.tmp
#-s after mail gives me a subject line
echo “$(cat file.tmp)” | mail -s IPaddress <email@domain.com>
sleep 5
rm file.tmp

I added this line to /etc/rc.local, but I was getting two copies of the email on boot up. So I deleted it.

/etc/network/if-up.d/mailip-1

If I wanted my external IP I could add a line like below. Maybe put a line in cron to run the script every so often and get this info at times other than when I boot the Pi or when renewing the IP.

dig +short myip.opendns.com @resolver1.opendns.com >> file.tmp

To get dig I had to add apt-get install dnsutils.

Advertisements
This entry was posted in Debian, Linux, Uncategorized. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s