Page 1 of 6 123 ... LastLast
Results 1 to 10 of 56

Thread: script to check ip address

  1. #1
    Join Date
    Aug 2008
    Location
    Arizona State
    Beans
    410
    Distro
    Ubuntu

    script to check ip address

    I have a home network which I have SSH set up on. I would like to be able to dial my home network remotely but my ip address changes at least once a day. I called my ISP to see if they can provide me with a static IP address, but they charge a lot of money to lease static IPs to compensate for the assumed increase in bandwidth of running a server.
    I don't plan on running a game server or anything that requires a lot of bandwidth so I don't want to spend the extra money.
    So my plan was to create a simple script that would query my external IP address, echo it to a file and then email the contents of that file to myself at regular intervals.
    I've gotten the first part of the script working and it does echo my external IP. However, the second part which would then email the script to myself I can't figure out. Then the third part I guess would be to run a cron job that will execute the script.

    Here is my script so far.

    Code:
    #!/bin/bash
    
    IPFILE=/home/user/ipaddress
    
    CURRENT_IP=$(wget http://www.whatismyip.org -O - -o /dev/null)
    
    MAIL_FROM="From: insert@email.address-here"
    
    if [ -f $IPFILE ]; then
    KNOWN_IP=$(cat $IPFILE)
    else
    KNOWN_IP=
    fi
    
    if [ "$CURRENT_IP" != "$KNOWN_IP" ]; then
    echo $CURRENT_IP > $IPFILE
    
    MAIL_SUBJECT="IP address at home"
    MAIL_BODY="The IP address at home has been changed to $CURRENT_IP. Update SSH client now."
    
    echo $MAIL_BODY | mail -a "$MAIL_FROM" -s "$MAIL_SUBJECT" -c insert@email.address-here
    logger -t ipcheck -- IP changed to $CURRENT_IP
    else
    logger -t ipcheck -- No IP change
    fi
    When I execute the script, my IP address is echoed to ~/ipaddress. But mail is not being sent to my email address. Do I need to configure mail? I installed 'mailutils' and set the server settings to 'internet site' since I use gmail. Is that incorrect?

  2. #2
    Join Date
    Mar 2006
    Location
    Kitakyushu Japan
    Beans
    9,361
    Distro
    Ubuntu 11.04 Natty Narwhal

    Re: script to check ip address

    This is a much better solution rather than emailing your IP:

    https://help.ubuntu.com/community/DynamicDNS

    In short:
    Configure your router to automatically update your free dynamic domain name URL, or use ddclient on your ssh server to do it.

    Then you can ssh to: ssh you@yourdnsdomain.com instead of having to type in a new IP address every time.
    Last edited by dmizer; December 8th, 2011 at 05:17 PM.

  3. #3
    Join Date
    Aug 2008
    Location
    Arizona State
    Beans
    410
    Distro
    Ubuntu

    Re: script to check ip address

    that was going to be plan B actually. Mostly I was trying to see if I could get the script to work

  4. #4
    Join Date
    Mar 2006
    Location
    Kitakyushu Japan
    Beans
    9,361
    Distro
    Ubuntu 11.04 Natty Narwhal

    Re: script to check ip address

    Quote Originally Posted by N00b-un-2 View Post
    that was going to be plan B actually. Mostly I was trying to see if I could get the script to work
    To get the script to work, you would indeed have to configure sendmail on your server. In most cases, ISPs have smtp blocked (anti spam) so you'd have to jump through some pretty difficult hoops to get everything working. Not impossible though.

  5. #5
    Join Date
    Aug 2008
    Location
    Arizona State
    Beans
    410
    Distro
    Ubuntu

    Re: script to check ip address

    I registered at freedns.afraid.org. I was able to ssh to my home computer from my phone using my name server/dns/whatever you call it. However, I have my doubts that when my ip changes it's going to continue working.

  6. #6
    Join Date
    Mar 2006
    Location
    Kitakyushu Japan
    Beans
    9,361
    Distro
    Ubuntu 11.04 Natty Narwhal

    Re: script to check ip address

    Quote Originally Posted by N00b-un-2 View Post
    I registered at freedns.afraid.org. I was able to ssh to my home computer from my phone using my name server/dns/whatever you call it. However, I have my doubts that when my ip changes it's going to continue working.
    That's why you need to install and configure ddclient: https://help.ubuntu.com/community/Dy...ic_DNS_Updates

  7. #7
    Join Date
    Feb 2011
    Location
    Maryland
    Beans
    2,251

    Re: script to check ip address

    Quote Originally Posted by N00b-un-2 View Post
    I have a home network which I have SSH set up on. I would like to be able to dial my home network remotely but my ip address changes at least once a day. I called my ISP to see if they can provide me with a static IP address, but they charge a lot of money to lease static IPs to compensate for the assumed increase in bandwidth of running a server.
    I don't plan on running a game server or anything that requires a lot of bandwidth so I don't want to spend the extra money.
    So my plan was to create a simple script that would query my external IP address, echo it to a file and then email the contents of that file to myself at regular intervals.
    I've gotten the first part of the script working and it does echo my external IP. However, the second part which would then email the script to myself I can't figure out. Then the third part I guess would be to run a cron job that will execute the script.

    Here is my script so far.

    Code:
    #!/bin/bash
    
    IPFILE=/home/user/ipaddress
    
    CURRENT_IP=$(wget http://www.whatismyip.org -O - -o /dev/null)
    
    MAIL_FROM="From: insert@email.address-here"
    
    if [ -f $IPFILE ]; then
    KNOWN_IP=$(cat $IPFILE)
    else
    KNOWN_IP=
    fi
    
    if [ "$CURRENT_IP" != "$KNOWN_IP" ]; then
    echo $CURRENT_IP > $IPFILE
    
    MAIL_SUBJECT="IP address at home"
    MAIL_BODY="The IP address at home has been changed to $CURRENT_IP. Update SSH client now."
    
    echo $MAIL_BODY | mail -a "$MAIL_FROM" -s "$MAIL_SUBJECT" -c insert@email.address-here
    logger -t ipcheck -- IP changed to $CURRENT_IP
    else
    logger -t ipcheck -- No IP change
    fi
    When I execute the script, my IP address is echoed to ~/ipaddress. But mail is not being sent to my email address. Do I need to configure mail? I installed 'mailutils' and set the server settings to 'internet site' since I use gmail. Is that incorrect?

    If you intend to do an email script, here is a very simple one.

    You need curl and mailutils

    sudo apt-get install curl
    sudo apt-get install mailutils
    #!/bin/bash
    echo "Current IP is $(curl www.whatismyip.org)" | mail -s "IP Address" myemail@domain.com
    put it into your crontab to run once a day or however often you need

  8. #8
    Join Date
    Feb 2011
    Location
    Maryland
    Beans
    2,251

    Re: script to check ip address

    Here I wrote a better one.
    This will do its own automatic check for an IP change. If the ip changes it will notify you via email.

    #!/bin/bash
    echo $(curl www.whatismyip.org) > ~/ipaddress.txt
    if [ "x$(curl www.whatismyip.org)" = "x$(cat ~/ipaddress.txt)" ]
    then
    exit
    else
    echo "Current IP is $(curl www.whatismyip.org)" | mail -s "Your IP Address has changed" mail@domain
    fi

    Run that in a crontab

  9. #9
    Join Date
    Aug 2008
    Location
    Arizona State
    Beans
    410
    Distro
    Ubuntu

    Re: script to check ip address

    the problem i'm running into is that when I run mail no email is sent. I installed mailutils, but I don't think I have it configured right.

  10. #10
    Join Date
    Feb 2011
    Location
    Maryland
    Beans
    2,251

    Re: script to check ip address

    Quote Originally Posted by N00b-un-2 View Post
    the problem i'm running into is that when I run mail no email is sent. I installed mailutils, but I don't think I have it configured right.
    There is nothin to configure in mail utils

Page 1 of 6 123 ... LastLast

Tags for this Thread

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
  •