Results 1 to 10 of 10

Thread: Automatic IP change notifier

  1. #1
    Junior Member
    Join Date
    Jul 2007
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Rep Power
    0

    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

  2. #2
    Never say die nixcraft's Avatar
    Join Date
    Jan 2005
    Location
    BIOS
    Posts
    4,374
    Thanks
    17
    Thanked 754 Times in 496 Posts
    Rep Power
    10

    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
    All [Solved] threads are closed by mods / admin to avoid spam issues. See Howto mark a thread as [Solved]


  3. #3
    Junior Member
    Join Date
    Jul 2007
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Rep Power
    0

    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!

  4. #4
    Junior Member Ali_ix's Avatar
    Join Date
    Jul 2007
    Location
    Iran, Tehran
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Rep Power
    0

    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.

  5. #5
    Junior Member
    Join Date
    Jul 2007
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Rep Power
    0

    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?

  6. #6
    Junior Member Ali_ix's Avatar
    Join Date
    Jul 2007
    Location
    Iran, Tehran
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Rep Power
    0

    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'); ?>

  7. #7
    Junior Member
    Join Date
    Jul 2007
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Rep Power
    0

    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

  8. #8
    Junior Member Ali_ix's Avatar
    Join Date
    Jul 2007
    Location
    Iran, Tehran
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Rep Power
    0

    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.

  9. #9
    Never say die nixcraft's Avatar
    Join Date
    Jan 2005
    Location
    BIOS
    Posts
    4,374
    Thanks
    17
    Thanked 754 Times in 496 Posts
    Rep Power
    10

    Default

    Ali_ix, nice script

    BTW, welcome to forum!
    All [Solved] threads are closed by mods / admin to avoid spam issues. See Howto mark a thread as [Solved]


  10. #10
    Junior Member Ali_ix's Avatar
    Join Date
    Jul 2007
    Location
    Iran, Tehran
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Rep Power
    0

    Default

    Quote Originally Posted by nixcraft View Post
    Ali_ix, nice script

    BTW, welcome to forum!
    Thanks Vivek

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Automatic Mount of Windows Share
    By bayvista in forum Networking, Firewalls and Security
    Replies: 0
    Last Post: 21st February 2008, 11:07 AM
  2. Automatic startup service
    By gthian in forum Shell scripting
    Replies: 0
    Last Post: 24th June 2007, 08:21 AM
  3. Replies: 2
    Last Post: 8th February 2007, 03:45 PM
  4. Automatic Sendmail
    By Kobi in forum Solaris/OpenSolaris
    Replies: 2
    Last Post: 25th January 2007, 12:04 PM
  5. How to Change Password
    By puppen in forum Linux software
    Replies: 6
    Last Post: 6th June 2006, 01:56 PM

Posting Permissions

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

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 37 38 39 40 41