How to Email Yourself Your IP Address

Absinthe, Programming, Website Development

I’ve used the instructions here to set up a script that emails my desktop’s IP address to me.

Their instructions are five years old, so I had to make some additions:

  • In Gmail, you need to enable Access to Less Secure Apps.
  • I had to add ‘touch ip.txt’ to their bash script right before ‘read ip1 < ip.txt’, otherwise the script just hung at that line forever.
  • ifconfig.me sometimes returns nothing for IP. This makes the routine think your IP has changed (into nothing). To avoid this, I put the call to ifconfig.me inside a while loop that tries to call again until it gets a response.

I need to email myself my IP because, otherwise, my ISP might change my IP address (a la dynamic IP), and then I wouldn’t know where to find my website anymore.

Since now I will always be able to find my home IP, I can always access my website from anywhere in the world.

My script:

#!/bin/sh

SUBJ=”some subject”
EMAIL=”your_email@gmail.com”

ip1=””
ip2=””

touch ip.txt
read ip1 < ip.txt

while [ “$ip2” = “” ]
do
ip2=$(wget -qO- ifconfig.me/ip)
done

if [ “$ip1” = “$ip2” ]
then
exit
else
echo “$ip2” > ip.txt
echo “$ip2” | mail -s $SUBJ $EMAIL
exit
fi

Advertisements

2 thoughts on “How to Email Yourself Your IP Address

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s