nixCraft Linux Forum

nixCraft

Linux Tech Support Forum

Automatic IP change notifier

This is a discussion on Automatic IP change notifier within the Shell scripting forums, part of the Development/Scripting category; Hello everyone, First of all thank you for reading this post! Ok, here it goes: What do I wanna create? ...


Go Back   nixCraft Linux Forum > Development/Scripting > Shell scripting

Register FAQ Members List Calendar Mark Forums Read
  #1 (permalink)  
Old 07-11-2007, 05:39 AM
Junior Member
User
 
Join Date: Jul 2007
My distro: Ubuntu
Posts: 5
Rep Power: 0
SAYAYINX is on a distinguished road
Send a message via AIM to SAYAYINX Send a message via Yahoo to SAYAYINX
Cool Automatic IP change notifier

Hello everyone,

First of all thank you for reading this post!

Ok, here it goes:

What do I wanna create?
Answer: I want to create a bash script that checks my IP, and if there has been a change, send me an email, sms or automatically post the IP in a website.

Why should I? There are scripts such as no-ip.org, etc..
Answer: Even though I am using those as a guide, the truth is I want to make it better! Any help would be highly appreciated.

What have you done so far?
Answer: So far, this is all I got but it does not work!

#!/bin/bash
# The ultimate solution to my DNS problem
LOOKUP=`host subdomain.mydomain.com | awk '{print $4}'`
MYIP=`curl -s IP Chicken - What is my IP? Find Your IP Address! | awk '/[0-9]*\.[0-9]*\.[0-9]*\.[0-9]*/ {print $1}'`
# Do the work
if [ "$LOOKUP" = "$MYIP" ]; then
echo "No change in your IP."
else
echo "The IP address has changed. "
#Either send me an: email, sms or post it in a site.
#mail -s "Here is the IP ... " username@mydomain.com
#echo `lynx -auth=${USERNAME}:${PASSWORD} -source "http://subdomain.mydomain.com"`
fi


What seems to be the problem is that the host command has some extra information that does not allow it to make a good comparison, so I always get the message that it has change.

Please help me out, I'm a quick learner!

~Saya
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 07-12-2007, 11:38 PM
nixcraft's Avatar
Never say die
User
 
Join Date: Jan 2005
Location: BIOS
My distro: Any distro with shell
Posts: 966
Rep Power: 10
nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute
Default

Hi,

Can you tell me what you are trying to achieve using above shell script? There may be better way exists to do the same thing. Is your IP changes everyday? DHCP ? Dynamic IP? Let me know
__________________
Vivek | My personal blog
Linux Evangelist
Play hard stay cool
Reply With Quote
  #3 (permalink)  
Old 07-12-2007, 11:47 PM
Junior Member
User
 
Join Date: Jul 2007
My distro: Ubuntu
Posts: 5
Rep Power: 0
SAYAYINX is on a distinguished road
Send a message via AIM to SAYAYINX Send a message via Yahoo to SAYAYINX
Default

I have DHCP, so every day or so my IP changes.

But everytime is different, it can last me 2 days or it can last me 6 hours.

~Saya

P.D Thank you for the reply!
Reply With Quote
  #4 (permalink)  
Old 07-13-2007, 12:52 PM
Ali_ix's Avatar
Junior Member
User
 
Join Date: Jul 2007
Location: Iran, Tehran
My distro: Debian/Ubuntu
Posts: 6
Rep Power: 0
Ali_ix is on a distinguished road
Send a message via Yahoo to Ali_ix
Default

I have an adsl account that works through pppoe, this is my script:

Code:
#!/bin/bash
#set -x

IFACE=ppp0
EMAIL=you@domain
LOG=/your/log/path/ip

OLD_IP=`cat $LOG`

# get current ip on IFACE
    x=`/sbin/ifconfig $IFACE`
    y=${x#*inet addr:}
    y=${y%% *}

# log current ip
    echo $y > $LOG

# check if ip changed

    # if we have disconnected for a long time
    if [[ -z "$OLD_IP" ]] && [[ "$y" ]]; then

        # report new ip    
        echo 'New IP address for' `hostname` 'is:' $y 'on ' `date` | mail -s 'New IP' $EMAIL

    # if ip changed since last check
    elif [[ "$OLD_IP" ]] && [[ "$y" ]]; then

        if [ "$y" != "$OLD_IP" ]; then
        # report new ip    
        echo 'New IP address for' `hostname` 'is:' $y 'on ' `date` | mail -s 'New IP' $EMAIL
        fi
    fi

exit 0
run it every 5 minute via cron.
Reply With Quote
  #5 (permalink)  
Old 07-13-2007, 04:53 PM
Junior Member
User
 
Join Date: Jul 2007
My distro: Ubuntu
Posts: 5
Rep Power: 0
SAYAYINX is on a distinguished road
Send a message via AIM to SAYAYINX Send a message via Yahoo to SAYAYINX
Default

Thanks but I do not know if that script would work...

I am connected via router to the internet, it is not straight through.

Here is a copy of my ifconfig:

sayayinx@sayayinx:~$ ifconfig
eth0 Link encap:Ethernet HWaddr 00:E0:18:5A:G1:F0
inet addr:192.168.1.101 Bcast:255.255.255.255 Mask:255.255.255.0
inet6 addr: fe80::3e0:18ff:fe5a:c1f0/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:31898184 errors:0 dropped:0 overruns:0 frame:0
TX packets:30138060 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:12692099512 (12.5 GiB) TX bytes:12820138230 (12.6 GiB)

lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:16436 Metric:1
RX packets:10926 errors:0 dropped:0 overruns:0 frame:0
TX packets:10926 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:1583104 (1.5 MiB) TX bytes:1583104 (1.5 MiB)

Would it work?
Reply With Quote
  #6 (permalink)  
Old 07-13-2007, 05:30 PM
Ali_ix's Avatar
Junior Member
User
 
Join Date: Jul 2007
Location: Iran, Tehran
My distro: Debian/Ubuntu
Posts: 6
Rep Power: 0
Ali_ix is on a distinguished road
Send a message via Yahoo to Ali_ix
Default

Quote:
Originally Posted by SAYAYINX View Post
Thanks but I do not know if that script would work...

I am connected via router to the internet, it is not straight through.

...
Would it work?
No, it wont.
You should change the "get current ip on IFACE" section.

you can use some "what is my ip" web sites and grep your ip in html page, or if you have and site, web host available, write a simple php script to give JUST the visitor ip.

sample php code that should work:

PHP Code:
<?php echo getenv('REMOTE_ADDR'); ?>
Reply With Quote
  #7 (permalink)  
Old 07-13-2007, 05:48 PM
Junior Member
User
 
Join Date: Jul 2007
My distro: Ubuntu
Posts: 5
Rep Power: 0
SAYAYINX is on a distinguished road
Send a message via AIM to SAYAYINX Send a message via Yahoo to SAYAYINX
Default

I will use my line then:

MYIP=`curl -s IP Chicken - What is my IP? Find Your IP Address! | awk '/[0-9]*\.[0-9]*\.[0-9]*\.[0-9]*/ {print $1}'`

I know it works.

But I have another question:

LOG=/your/log/path/ip <= where is that folder? can i make one up?

OLD_IP=`cat $LOG` <= again, is that a folder? a file?

Thanks for the quick reply
Reply With Quote
  #8 (permalink)  
Old 07-13-2007, 06:01 PM
Ali_ix's Avatar
Junior Member
User
 
Join Date: Jul 2007
Location: Iran, Tehran
My distro: Debian/Ubuntu
Posts: 6
Rep Power: 0
Ali_ix is on a distinguished road
Send a message via Yahoo to Ali_ix
Default

please wrap code with code tag! ([ code ] [ /code ] without space in bracket

Quote:
Originally Posted by SAYAYINX View Post
I will use my line then:

MYIP=`curl -s IP Chicken - What is my IP? Find Your IP Address! | awk '/[0-9]*\.[0-9]*\.[0-9]*\.[0-9]*/ {print $1}'`
I know it works.
yes, this works, but this work better:

Code:
curl -s http://www.ipchicken.com/ | awk '/[0-9]*\.[0-9]*\.[0-9]*\.[0-9]*/ {print $1}' | uniq
Quote:
Originally Posted by SAYAYINX View Post
But I have another question:
LOG=/your/log/path/ip <= where is that folder? can i make one up?
OLD_IP=`cat $LOG` <= again, is that a folder? a file?
you should create the folder and enter full path here.
i have a 'scrpts' folder in my home director. and save log file with a 'log_' prefix with script name there.

ex:
script: /home/ali/scripts/ip_change
log file: /home/ali/scripts/log_ip_change

i have assigned the log file path to LOG variable in script, the in used 'LOG' variable as parameter to 'cat' command to read file content and assigned the result to 'OLD_IP' variable.

you just need to define 'LOG' variable. the LOG variable points to a file.
Reply With Quote
  #9 (permalink)  
Old 07-13-2007, 08:56 PM
nixcraft's Avatar
Never say die
User
 
Join Date: Jan 2005
Location: BIOS
My distro: Any distro with shell
Posts: 966
Rep Power: 10
nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute
Default

Ali_ix, nice script

BTW, welcome to forum!
__________________
Vivek | My personal blog
Linux Evangelist
Play hard stay cool
Reply With Quote
  #10 (permalink)  
Old 07-13-2007, 09:49 PM
Ali_ix's Avatar
Junior Member
User
 
Join Date: Jul 2007
Location: Iran, Tehran
My distro: Debian/Ubuntu
Posts: 6
Rep Power: 0
Ali_ix is on a distinguished road
Send a message via Yahoo to Ali_ix
Default

Quote:
Originally Posted by nixcraft View Post
Ali_ix, nice script

BTW, welcome to forum!
Thanks Vivek
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads

Thread Thread Starter Forum Replies Last Post
Automatic Mount of Windows Share bayvista Networking, Firewalls and Security 0 02-21-2008 11:07 AM
Automatic startup service gthian Shell scripting 0 06-24-2007 08:21 AM
Shell script for automatic conversion of files in tar files kasimani Shell scripting 2 02-08-2007 03:45 PM
Automatic Sendmail Kobi Solaris/OpenSolaris 2 01-25-2007 12:04 PM
How to Change Password puppen Linux software 6 06-06-2006 01:56 PM


All times are GMT +5.5. The time now is 06:42 PM.


Powered by vBulletin® Version 3.7.3 - Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36