Find the answer to your Linux question:
Page 1 of 2 1 2 LastLast
Results 1 to 10 of 18
I have my own server, but from time to time my IP address changes and when I am away from my Linux box I can't log into it, because of ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Nov 2005
    Posts
    4

    Creating a script to notify me of an IP address change


    I have my own server, but from time to time my IP address changes and when I am away from my Linux box I can't log into it, because of the address change. I'm new to all this, so what I'd like to do is create a script that notifies me when the IP address of my machine changes by emailing me the IP address to my yahoo account.

    I know I have to do something like this:

    1) ifconfig > ipaddress.txt
    2) tell it to wait a few minutes, maybe like two or so
    3) get the IP address again only, save it to a different file like ipaddress2.txt...somewhere around here I know I have to cut the IP address out of the txt file
    4) Then compare the two and if they're the same, do nothing, and if they are different email me the new IP

    I know the steps I need to take to get it to work right, I just have a hard time figuring out the code to get it to work right. Thanks for any help.

  2. #2
    Linux User
    Join Date
    Aug 2005
    Location
    Italy
    Posts
    401

    DNS service?

    Why don't used a free DNS? You can setup a free DNS service for your host (www.dyndns.org). This DNS assign a name to your computer, and an utility (ddclient, wich is included in debian for example) will notify the IP change to the DNS.

    So you can access to your machine always using the name of your computer...

    I use it and there was no problem!!!
    When using Windows, have you ever told "Ehi... do your business?"
    Linux user #396597 (http://counter.li.org)

  3. #3
    Just Joined!
    Join Date
    May 2005
    Posts
    52
    no-ip.com does this as well, and they have their own client thats easy to use. I use no-ip.com for a ton of domain names. They are good. Also if you use IPCop as a firewall gateway solution, it has built in support for dyndns and no-ip. really nice.

  4. #4
    Just Joined!
    Join Date
    Nov 2005
    Posts
    4
    I know I can do that but I still think it'd be cool to try to figure this out. Can someone help me start it in the right direction?

  5. #5
    Linux Newbie deek's Avatar
    Join Date
    Mar 2005
    Location
    Fort Wayne, IN
    Posts
    248
    What I am thinking is there is probably a line switch on the ifconfig command to just display the current ip address of say, eth0. This is assuming this machine takes on the external address from your ISP. If not, then things get a bit more complicated, as you are then only looking at your internal addresses, which won't help on the outside world.

    Anyways, after you have that working, then you output to a file, say ipcurrent. I would then create an ipold file, with the same ip. So, your process now, would be to rename ipcurrent to ipold (moving the ip address to the old file for comparison), then run the ifconfig (with switches) and output that to ipcurrent.

    Now you have two files. Use whatever programming/scripts you want to do a file compare (again, if you are only outputting the ip address, you shouldn't have to worry about cleaning the data, as it should always just be an ip address in the file). Using this code, if they are the same, you do nothing, if not, then you can send an email, with the ipcurrent file data to your yahoo account.

    I believe if you have sendmail (or maybe another mail service), you can easily build a script to send email via command line. So you just write that script and insert the ipcurrent data in your email.

    Obviously, I have not done any coding here for you, but I feel if I get into anymore detail, I might as well just write the whole code myself (which I don't feel like doing!). That should point you in the right direction.
    Join the Open Source Revolution. Support GNU/Linux.

    Find me at: www.deeksworld.com
    Registered GNU/Linux User #395777

  6. #6
    Just Joined!
    Join Date
    Nov 2005
    Posts
    4
    OK this is what I have so far...I need some more assistance.

    #!/bin/bash
    extip="`/sbin/ifconfig eth0 | grep 'inet addr' | awk '{print $2}' | sed -e 's/.*://'`"
    extint="ipaddress1"
    wait two minutes how do I do this?
    extip="`/sbin/ifconfig eth0 | grep 'inet addr' | awk '{print $2}' | sed -e 's/.*://'`"
    extint="ipaddress2"
    not sure if any of the stuff below is correct
    for (ipaddress1 <> ipaddress2)
    do email me@yahoo.com
    else do nothing

  7. #7
    Linux Newbie deek's Avatar
    Join Date
    Mar 2005
    Location
    Fort Wayne, IN
    Posts
    248
    If you are doing two files, then you really don't have to wait two minutes. If you put it on a cron job, say, running the program every 15 minutes, or maybe ever hour, then that second file, will always be comparing to whatever your IP address was an hour ago (or 15 minutes ago).

    By default, whatever frequency you run it at, that is what you will comparing to. Make sense?
    Join the Open Source Revolution. Support GNU/Linux.

    Find me at: www.deeksworld.com
    Registered GNU/Linux User #395777

  8. #8
    Linux Newbie
    Join Date
    May 2005
    Location
    Chennai,TamilNadu, India
    Posts
    141
    I also need some help over here since my problem was also related to the sending email part of this question

    I would like to know how to send an email with an attachment using the sendmail command from commandline ( the command that should be given). What data I have is the mailserver ipaddress, smtp username, and a smtp password

  9. #9
    Just Joined!
    Join Date
    Nov 2005
    Posts
    4

    One more thing

    OK, got it almost set up right...but from for ipaddress1 <> ipaddress2 down it doesn't work. What am I doing wrong?

    #!/bin/bash
    extip="`/sbin/ifconfig eth0 | grep 'inet addr' | awk
    '{print $2}' | sed -e 's/.*://'`"
    extint="ipaddress1"
    extip="`/sbin/ifconfig eth0 | grep 'inet addr' | awk
    '{print $2}' | sed -e 's/.*://'`"
    extint="ipaddress2"
    for ipaddress1 <> ipaddress2
    do sendmail time -q2m me@yahoo.com
    else

  10. #10
    Linux Newbie deek's Avatar
    Join Date
    Mar 2005
    Location
    Fort Wayne, IN
    Posts
    248
    I am not familiar with much bash stuff, but the first thing I would do is, before going into the for (which I am assuming is a type of "if" logic, as that is all that needs to be done), print/display the contents of ipaddress1 and ipaddress2, to make sure those variables are holding what you think they should.

    If they are, and the logic is not working correctly, there may be an "invisible" character or something that you are not accounting for.

    Although, it could just be you need to use an "if" statement, as "for" is a loop, and you don't need to loop, right?
    Join the Open Source Revolution. Support GNU/Linux.

    Find me at: www.deeksworld.com
    Registered GNU/Linux User #395777

Page 1 of 2 1 2 LastLast

Posting Permissions

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