Page 1 of 2 12 LastLast
Results 1 to 10 of 20

Thread: How to automatically email me with IP address?

  1. #1
    Join Date
    May 2007
    Location
    Minneapolis
    Beans
    123
    Distro
    Ubuntu 8.04 Hardy Heron

    How to automatically email me with IP address?

    Hi, I'm running an Ubuntu 8.04.1 server on a DHCP network (not behind a router), and although it doesn't happen often I'm afraid the IP address of the server will change and I won't be able to access it anymore. Also, sometimes the modem crashes and comes back up but leaves the server without a connection. Right now I set up a script to basically do ifdown/ifup daily to ensure that if the modem crashes and it loses an IP it will get it back within a day. But, I'm afraid that it will get a different IP when it does this. Is there a way to have the server email me with it's IP address, say, once a week? Also, with my ifdown/ifup script, is there a way to not have any output from the ifdown/ifup commands? Right now I get a "mail" message daily on my server with the output of ifdown/ifup and it's kind of cluttered and annoying to delete. I'm not familiar at all with sending emails via a terminal. Thanks in advance!

    By the way, my idea was to perhaps send me an email with the contents of "wget http://whatismyip.org", since it would only contain the current IP address. Or perhaps getting the IP address via some internal method would work better.
    --iissmart--

  2. #2
    Join Date
    May 2007
    Location
    Phoenix, Arizona USA
    Beans
    2,909
    Distro
    Ubuntu 8.04 Hardy Heron

    Re: How to automatically email me with IP address?

    You could use a dynamic dns provider like no-ip. That way you can always access the server by name no matter what the IP is. You run a client on the server that updates the no-ip service when the IP changes.

    -Tim
    www.pcchopshop.net

    Hard to find and obsolete PC and server parts. "If we can't find it, it probably doesn't exist"

  3. #3
    Join Date
    May 2007
    Location
    Minneapolis
    Beans
    123
    Distro
    Ubuntu 8.04 Hardy Heron

    Re: How to automatically email me with IP address?

    Sorry, I forgot to mention that I already own a domain name for the server, therefore I don't use a dynamic dns provider. So, I just need a script which emails me the IP address of itself, and I'll just put it in /etc/cron.weekly
    --iissmart--

  4. #4
    Join Date
    Nov 2006
    Location
    Belgium
    Beans
    3,025
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: How to automatically email me with IP address?

    so then, if your server has a (domain) name, why do you need to access it by address ? domain names were invented to avoid having to use addresses ...

  5. #5
    Join Date
    Mar 2008
    Beans
    1,755

    Re: How to automatically email me with IP address?

    Also if your ISP does not give you a dynamic address then that would make it static, meaning it would never change.

  6. #6
    Join Date
    Jul 2008
    Beans
    15

    Post Re: How to automatically email me with IP address?

    Well, I guess this would do:
    Code:
    #!/bin/bash
    echo 'Mailing your IP address...'
    ip=`/sbin/ifconfig | grep inet | grep -v inet6 | grep -v 127.0.0.1 | awk '{ print $2 }' | sed -e s'/addr://'`
    echo $ip | mail -s 'IP Address Info' <email>
    Obviously, you'd have to replace <email> with your email address.
    You could crontab the script each hour or so.

  7. #7
    Join Date
    Mar 2006
    Location
    Philadelphia, PA, U.S.
    Beans
    27
    Distro
    Ubuntu 9.04 Jaunty Jackalope

    Re: How to automatically email me with IP address?

    Your best bet would be to write a small shellscript piping the contents of ifconfig to an email message.
    Since you're not behind a router, and your system is (i'm assuming) using the DHCP address itself (not NAT), this will work easily.
    To make it smarter, add an if/then emailing you ONLY if the address changes from whatever it was previously.
    Then, add the script to cron to run hourly or daily or twice a day or whatever makes you comfortable.
    Example:

    Code:
    #!/bin/sh
    
    OLDADDR=`cat ~/dhcp_file`
    CURADDR=`/sbin/ifconfig | grep Bcast`
    
    if [ "$CURADDR" != "$OLDADDR" ]
            then
                    echo "$CURADDR" > ~/dhcp_file
                    mail -s 'Here is my new ip address' your@email.address < ~/dhcp_file
            else
                    exit
    fi
    This will send an email to you every time your address changes, provided you've added it to someone's cron (specifically a user able to run ifconfig).
    I just tested it on my Debian system, and it works with no problem.
    Note: you'll need to install sendmail before being able to do it:
    apt-get install sendmail-bin

    good luck!

    ps, you probably won't ever get an email from this script as most isps don't often change user ip addresses these days, but i agree with you-- caution beats cure every time.
    Last edited by sp00ki; July 7th, 2008 at 07:43 PM.

  8. #8
    Join Date
    Mar 2006
    Location
    Philadelphia, PA, U.S.
    Beans
    27
    Distro
    Ubuntu 9.04 Jaunty Jackalope

    Re: How to automatically email me with IP address?

    Quote Originally Posted by simonapnic View Post
    Well, I guess this would do:
    Code:
    #!/bin/bash
    echo 'Mailing your IP address...'
    ip=`/sbin/ifconfig | grep inet | grep -v inet6 | grep -v 127.0.0.1 | awk '{ print $2 }' | sed -e s'/addr://'`
    echo $ip | mail -s 'IP Address Info' <email>
    Obviously, you'd have to replace <email> with your email address.
    You could crontab the script each hour or so.
    lol... looks like i was beaten to the punch, though i like that mine only sends an email if the address changes as opposed to sending email every time. gives more of an "alert" feel to it (and a duplicate email every hour can be a bit excessive).
    Last edited by sp00ki; July 7th, 2008 at 08:09 PM.

  9. #9
    Join Date
    May 2007
    Location
    Phoenix, Arizona USA
    Beans
    2,909
    Distro
    Ubuntu 8.04 Hardy Heron

    Re: How to automatically email me with IP address?

    Quote Originally Posted by iissmart View Post
    Sorry, I forgot to mention that I already own a domain name for the server, therefore I don't use a dynamic dns provider. So, I just need a script which emails me the IP address of itself, and I'll just put it in /etc/cron.weekly
    Even if you own the domain, if your IP address is not static, you could make your life much easier with a redirect. You would point your domain at the redirect, and they would take care of always maintaining a connection to your server. No funky scripts necessary.

    -Tim
    www.pcchopshop.net

    Hard to find and obsolete PC and server parts. "If we can't find it, it probably doesn't exist"

  10. #10
    Join Date
    Jun 2007
    Location
    Australia
    Beans
    332
    Distro
    Ubuntu Studio 12.04 Precise Pangolin

    Re: How to automatically email me with IP address?

    This thread is great, but I have a question how do you have the internet on without using a router? Does this mean that the computer is attach straight to the phone line? I not trying to be funny can someone tell what this means
    It doesn't aways happen straight away. But time has a way of leveling everything!!!

Page 1 of 2 12 LastLast

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
  •