Results 1 to 7 of 7

Thread: Bash script to email you your ip address

  1. #1
    Join Date
    Mar 2011
    Beans
    2

    Post Bash script to email you your ip address

    I made a bash script to find your external ip address, and if it is different to the last time it checked it, it emails it to you.
    It uses msmtp to send the email.

    Code:
    #/bin/bash
    
    # email address which is sending the email
    sender="user@domain.com"
    # email address which is receiving the email
    receiver="user@domain.com"
    # delay time between checks in seconds
    delaytime="1800"
    # file to save previous ip address
    oldip="/home/USER/.ip.address"
    # temp file for email
    tmpemail="/tmp/ip.mail"
    
    touch $oldip
    while true; do
    	rm -f $tmpemail
    	echo "To: $receiver" >> $tmpemail
    	echo "From: $sender" >> $tmpemail
    	echo "Subject: Your Ip" >> $tmpemail
    	echo "" >> $tmpemail
    	ipaddress=$(curl -s http://checkip.dyndns.org | sed 's/[a-zA-Z/<> :]//g')
    	ipaddress2=$(cat $oldip)
    	echo "Is: $ipaddress" >> $tmpemail
    	echo "Was: $ipaddress2" >> $tmpemail
    	echo $ipaddress
    	echo $ipaddress2
    	if [ "$ipaddress" != "$ipaddress2" ]
    		then echo "There different!"
    		cat $tmpemail | msmtp -a default $receiver
    		rm -f $oldip
    		echo $ipaddress >> $oldip
    	fi
    	echo "Continuing..."
    	sleep $delaytime
    done
    This is the first bash script that i've published

  2. #2
    Join Date
    Sep 2006
    Beans
    7,622
    Distro
    Lubuntu Development Release

    use case

    Nice. How do you plan on using it mostly?

  3. #3
    Join Date
    Feb 2006
    Location
    uk
    Beans
    Hidden!

    Re: Bash script to email you your ip address

    to do away with a dynamic dns service, i presume.

    its a nice script, although i wouldn't have put it in a loop. id have made it do a single pass and repeatedly call it from cron.


    also, "they're different"

  4. #4
    Join Date
    Oct 2009
    Beans
    Hidden!
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: Bash script to email you your ip address

    Quote Originally Posted by aeiah View Post
    to do away with a dynamic dns service, i presume.

    its a nice script, although i wouldn't have put it in a loop. id have made it do a single pass and repeatedly call it from cron.


    also, "they're different"
    This.

    I just use dyndns since it's easier to remember a domain name instead of having to look up an ip address.
    Come to #ubuntuforums! We have cookies! | Basic Ubuntu Security Guide

    Tomorrow's an illusion and yesterday's a dream, today is a solution...

  5. #5
    Join Date
    Sep 2006
    Beans
    7,622
    Distro
    Lubuntu Development Release

    dyndns

    Quote Originally Posted by CharlesA View Post
    I just use dyndns since it's easier to remember a domain name instead of having to look up an ip address.
    dyndns is an easy way. I use it, too. There are also other similar services:

    http://dnslookup.me/dynamic-dns/

  6. #6
    Join Date
    Oct 2009
    Beans
    Hidden!
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: dyndns

    Quote Originally Posted by Lars Noodén View Post
    dyndns is an easy way. I use it, too. There are also other similar services:

    http://dnslookup.me/dynamic-dns/
    Yep. I've used no-ip too.
    Come to #ubuntuforums! We have cookies! | Basic Ubuntu Security Guide

    Tomorrow's an illusion and yesterday's a dream, today is a solution...

  7. #7
    Join Date
    Oct 2006
    Beans
    414

    Re: Bash script to email you your ip address

    I just thought about this and before writing it I attempted to search for it.

    It's going to be useful for me because I can remember my ip as it doesn't change often. But when it does it's inconvenient. Also, my dyndns name seems to always expire from lack of use. This is just easier for me because I don't need to use it as often.

    BTW thanks!
    The true way to be humble is not to stoop until you are smaller than yourself, but to stand at your real height against some higher nature that will show you what the real smallness of your greatness is.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •