You are not logged in.

#1 2011-03-16 00:03:23

xcrx
Member
Registered: 2011-03-01
Posts: 16

Check IP and Send email from Bash

I am new to bash scripting so I am sorry if this is really easy. I want to check my internet IP and send it in an email to myself whenever it changes. I want to be able to connect to a few services on my computer while I am away and my ISP keeps changing my IP address so I will like a shell script that can run in the background to keep me up to date on my home box's latest IP address.

Is this something that is doable? If so could someone give me a push in the right direction. I read a few how-tos but I was hoping someone could suggest a good command line mailer that I can use with my gmail account and any other useful tips for a newbie.

Offline

#2 2011-03-16 00:32:16

karol
Archivist
Registered: 2009-05-06
Posts: 25,440

Re: Check IP and Send email from Bash

Try 'curl ifconfig.me' & heirloom-mailx for scripting e-mail e.g.

curl ifconfig.me | mailx -s "my IP" login@gmail.com

just use your own e-mail address and of course you can change the subject of the e-mail to something other than "my IP".

You can run it via cron e.g. every hour.

Last edited by karol (2011-03-16 00:40:06)

Offline

#3 2011-03-16 00:41:34

Misfit138
Misfit Emeritus
From: USA
Registered: 2006-11-27
Posts: 4,189

Offline

#4 2011-03-16 00:50:30

xcrx
Member
Registered: 2011-03-01
Posts: 16

Re: Check IP and Send email from Bash

Okay I think I can make the mail portion of that work just fine but ifconfig only gives me my local IP. After a little research I think I am going to use dig to get my outgoing IP.

Thanks for you help karol!

Offline

#5 2011-03-16 01:05:06

karol
Archivist
Registered: 2009-05-06
Posts: 25,440

Re: Check IP and Send email from Bash

xcrx wrote:

ifconfig only gives me my local IP.

I'm not sure if I got this right, but ifconfig.me is a website. I'm not talking about the ifconfig command.

Offline

#6 2011-03-16 01:25:54

xcrx
Member
Registered: 2011-03-01
Posts: 16

Re: Check IP and Send email from Bash

karol wrote:
xcrx wrote:

ifconfig only gives me my local IP.

I'm not sure if I got this right, but ifconfig.me is a website. I'm not talking about the ifconfig command.

Oh okay. Sorry. I am not at home where I can try it out right now. My mistake.

Offline

#7 2011-03-17 17:40:48

emk
Member
Registered: 2010-05-11
Posts: 39

Re: Check IP and Send email from Bash

you might want to consider a dynamic dns service like http://www.dyndns.com. I've been using them for a few years to do what you want to do.

emk

Offline

#8 2011-03-17 18:24:11

Isola
Member
Registered: 2010-02-02
Posts: 99

Re: Check IP and Send email from Bash

Another DNS service is http://freedns.afraid.org

There are many domains to choose from. Some are pretty funny too. Once you've setup the DNS look for the wget script, or direct link to update the DNS. Launch this script via cron every day or hour, depending how often the address changes.

Offline

#9 2011-03-17 20:28:00

graysky
Wiki Maintainer
From: :wq
Registered: 2008-12-01
Posts: 10,597
Website

Re: Check IP and Send email from Bash

I wrote a script to do just this.  It's in the AUR:
http://aur.archlinux.org/packages.php?ID=37553


CPU-optimized Linux-ck packages @ Repo-ck  • AUR packagesZsh and other configs

Offline

#10 2011-03-18 13:47:27

codeRage
Member
Registered: 2011-02-20
Posts: 29

Re: Check IP and Send email from Bash

my script for checking ip from conky

#! /bin/bash
#
# Automated script to retrieve public IP from whatismyip.com

cache=/tmp/`basename $0`
now=`date +%s`
update_int=400                          # 300 or less will get you banned
pattern='([0-9]{1,3}\.){3}[0-9]{1,3}'

doupdate () {
    # Could also try myip.dk, ifconfig.me
    addr=`curl -s http://www.whatismyip.com/automation/n09230945.asp`

    if [[ $? -gt 0 ]]
    then 
        echo 'No address' > $cache/addr
        echo 0          > $cache/time

    elif [[ `echo $addr | egrep $pattern` == '' ]]
    then
        echo 'Remote down' > $cache/addr
        echo 0          > $cache/time
    else
        echo $addr        > $cache/addr
        echo $now         > $cache/time
    fi
}
    
mkdir -p $cache

if [[ -f $cache/time ]]
then
    then=`cat $cache/time`            # Time of last update
    int=$(( $now - $then ))        

    if [[ $int -gt $update_int ]] 
    then
        doupdate
    fi
else
    doupdate
fi

echo `cat $cache/addr` # \(${int}s ago\)

Offline

Board footer

Powered by FluxBB