Scripting Tutorial: Dynamic IP sent to your email

Posted: November 20, 2013 in Linux, Networking, Scripting, Tutorial
Tags: , , , , , , , , , , , ,

20131120-002815.jpg
Do you have a Dynamic WAN IP address for your homebrew server? Do you need a way to keep track of the address in the likely event that it changes?

As would be expected, I ran into this problem while using a Dynamic DNS service a few years ago. Whenever I would forget to renew the DNS address, the server would become unreachable when using the URL.

When you factor in an IP address that refreshes with each reboot of the modem (read: power outages), it can quickly become a nightmare attempting to SSH in to check on things from an off-site location. So what do you do when your DNS goes south?

My Solution

I created this script in order to send an e-mail and/or SMS-via-e-mail (ie, 9195551234@sms.provider.com) from your server to the specified address.

You’ll need to flesh out the obvious details in this script, such as your usernames and passwords. You may also need to substitute the GMail server for whichever service you are using.

Also be sure to download/install/configure any dependencies, such as sendEmail.

This BASH script can be set as a cronjob to be activated and sent out at specific times of the day. I used it to send me an email each morning at 9:00am and after every reboot (for maintenance, this was about every 6 months).

Security Issues

There are, of course, downsides to the example code below. Your email account’s password is stored in plaintext in the *.sh file. On top of this, I haven’t spent any time coding the use of SSL but I’m confident that someone can hack it together rather easily. It wouldn’t take more than a couple minutes. If I find the time to fix both issues and add more functionality, I may release it as a GNU utility.

The fine print: In the highly unlikely event that this damages your machine or otherwise has harmful side effects, I will NOT be held liable. You’ve been warned. Remember, CYOA.

sendEmail -v -f root@yourservername -s smtp.gmail.com:587 -xu gmailusername -xp gmailpassword -t mailto@domain.com -o tls=yes -u Current IP address -m Here is your current IP address -a /home/user/ip.txt

The -a option in the script is used to mark the specified file(s) as an attachment to be sent along with your e-mail.

Notice what the -a option points to. We’re about to create the attachment with this next snippet of code.

This script utilizes Curl to obtain your public/WAN IP address. It then saves the address to a text file. This resulting text file serves as the attachment in the e-mail that we’ll be sending.

curl ifconfig.me/ip > /home/user/ip.txt

The Finished Code

Your code should look something like this.

curl ifconfig.me/ip > /home/user/ip.txt

sendEmail -v -f root@yourservername -s smtp.gmail.com:587 -xu gmailusername -xp gmailpassword -t mailto@domain.com -o tls=yes -u Current IP address -m Here is your current IP address -a /home/user/ip.txt

# Use this to clean up if you want.
# rm /home/user/ip.txt

Also, note the use of -f. This dictates the “sent from” address. You can set this to anything you’d like. I wanted mine to show up in my email as coming from “stats@jmserver” so my code looked like -f stats@jmserver

Go ahead and give it a shot. You could also configure the script to run when the machine comes back up after an unexpected reboot, or even after every reboot. I say this because, it’s likely your modem will only go down due to an interruption of power. If your modem went down, there’s a high probability that your server came down with it.

The possibilities here are endless!

Let me know if this helps you at all or if you have any feedback. Also, if you like this type of post, let me know and I’ll be sure to keep them coming. Enjoy!

Leave a comment