Welcome to Linux Forums! With a comprehensive Linux Forum, information on various types of Linux software and many Linux Reviews articles, we have all the knowledge you need a click away, or accessible via our knowledgeable members.
Find the answer to your Linux question:
New to Linux Forums? Register here for free!
    Linux Forums > GNU Linux Zone > Linux Programming & Scripting > Creating a script to notify me of an IP address change

Forgot Password?
 Linux Programming & Scripting   C, Perl, PHP, Bash Scripts, anything programming or script related post in here!

Site Navigation
Linux Articles
Linux Forums
Linux Downloads
Linux Hosting
Free Magazines
Job Board
IRC Chat
RSS Feeds


Linux Forum Topics
Linux Forums
Your Distro
Linux Resources
GNU Linux Zone
The Community
Reply
 
Thread Tools Display Modes
Old 11-22-2005   #1 (permalink)
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.
NuttySquirrel is offline  


Reply With Quote
Old 11-22-2005   #2 (permalink)
Linux User
 
Join Date: Aug 2005
Location: Italy
Posts: 401
Send a message via ICQ to burnit
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)
burnit is offline   Reply With Quote
Old 11-22-2005   #3 (permalink)
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.
TheGreen is offline   Reply With Quote
Old 11-23-2005   #4 (permalink)
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?
NuttySquirrel is offline   Reply With Quote
Old 11-23-2005   #5 (permalink)
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
deek is offline   Reply With Quote
Old 11-23-2005   #6 (permalink)
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
NuttySquirrel is offline   Reply With Quote
Old 11-23-2005   #7 (permalink)
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
deek is offline   Reply With Quote
Old 11-24-2005   #8 (permalink)
Linux Newbie
 
Join Date: May 2005
Location: Chennai,TamilNadu, India
Posts: 141
Send a message via MSN to sharonenoch Send a message via Yahoo to sharonenoch
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
sharonenoch is offline   Reply With Quote
Old 12-05-2005   #9 (permalink)
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
NuttySquirrel is offline   Reply With Quote
Old 12-05-2005   #10 (permalink)
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
deek is offline   Reply With Quote
Reply


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 Off
Pingbacks are Off
Refbacks are Off

Free Magazines
Run Your Own Web Server Using Linux & Apache - Free 191 Page Preview
Learn about everything you'll need to build and maintain your Linux servers, and to deploy Web applications to them.
subscribe
Open Source Security Myths Dispelled
Dispel the five major myths surrounding Open Source Security and gain the tools necessary to make a truly informed decision for your IT organization
subscribe
InformationWeek
InformationWeek is the only newsweekly you'll need to stay on top of the latest developments in information technology.
subscribe



All times are GMT. The time now is 08:20 AM.






© 2000 - 2009 - All Rights Reserved - Property of  MAS Media

Content Relevant URLs by vBSEO 3.3.0 RC2