Koozali.org: home of the SME Server

Legacy Forums => Experienced User Forum => Topic started by: Bart on September 24, 2003, 08:01:10 PM

Title: ip change notification script help
Post by: Bart on September 24, 2003, 08:01:10 PM
Hello!

Can someone help me with the following:

I'm looking for a script that notifies an external e-mail address of any external ip changes. In other words: i want to receive a mail message (containing the new external ip address of the server) whenever my server reboots and is assigned a new ip.

Dynamic DNS is not an option because i want to use my domain name, and not a mydynamicip.org extension.

Any help is much appreciated.

Bart Wittkampf

ps: any other options you might think of are also very welcome.
Title: Re: ip change notification script help
Post by: Michiel on September 24, 2003, 10:54:35 PM
You can put a script in the ip-change event (/etc/e-smith/events/ip-change) to do whatever action you like when the IP changes.

But why would dyndns not be an option? It's perfectly OK to have two domainnames for the same IP address. The dyndns client will send you a mail whenever the IP changes. Moreover, how can you have your own domain name if you're running on a dynamic IP?

Michiel
Title: Re: ip change notification script help
Post by: Bart Wittkampf on September 25, 2003, 01:39:02 AM
I'm not experieced enought to write my own script for that. Let me tell you about my setup, and what doesn't seem to work.

- I have my own registered domain name: wittkampf.nl which points to my dynamically assigned ip address
- in addition i registered a dynamic ip address at dyndns.org.
- at first i tried to have the domain name (wittkampf.nl) redirect to the dynamic ip account. In that way, my connection would always be working.

Client on the internet -> Enom DNS (reroute to dyndns.org) -> DYNDNS.org -> SME Server with primary domain DYNDNS, secondary domain Enom

I am not sure if this setup is possible; it did not seem to work over here. My guess is that there is a problem with the SME server not knowing which packets are for primary and which ones are for the secondary domain. Could this be the problem?

Michiel thanks for your quick reply,

Bart
Title: Re: ip change notification script help
Post by: Michiel on September 25, 2003, 03:36:33 AM
enom.nl supports dynamic updates, but funnily enough not for .nl domains (see bottom of http://www.enom.nl/helpdesk/dns/dynamicdns.htm). The same is true for dyndns.org.

So unless you know of an other company that does redirect .nl domains, you're out of luck. Your only options are to
1) take an Internet connection with fixed IP
2) use wittkampf.org/net/com
or
3) accept you can't use wittkampf.nl to address your home server.

Michiel
Title: Re: ip change notification script help
Post by: Bart Wittkampf on September 26, 2003, 11:25:34 PM
Michiel, thanks for your help. I'll try option 3 for a while. I might pay the extra buck for a fixed IP though.

Thanks again,

Bart
Title: Re: ip change notification script help
Post by: Jim Danvers on September 27, 2003, 12:18:49 AM
Bart...

If you have a windows nt class machine (nt, 2k, xp) running behind the sme box, I can provide you with a little batch 'solution/home brew' that I use to notify myself of external ip's.  I use this quite successfully to keep track of some machines that I remotely maintain from time to time (my mothers, a local library, and my wifes machine @home).  Caveat - the nt machine will have to be on 24*7 ...  otherwise how else is it going to mail you the external address?   ;)

Let me know / reply back to this forum and I'll provide more info.

-=- jd -=-
Title: Re: ip change notification script help
Post by: Bart Wittkampf on September 27, 2003, 02:44:41 AM
Hi Jim!

I have an XP machine, running as a client. If that machine could be set up to keep track of my SME server, that would be great. I would love to hear your solution.

Bart
Title: Re: ip change notification script help
Post by: Jim Danvers on September 27, 2003, 06:07:07 AM
Bart (and anyone else interested):

What I do is use a few dos based tools (remember that?!  ;) ) and glue 'em together using a little native nt batch/scripting.

I use two things here:  Blat, which is a command line smtp mailer, and Lynx - a character based web browser.

I have zipped up the files you'll need into a little file called "mailip.zip" - grab it here:  http://www.bikeguy.myip.org/mailip.zip

In it you'll find what you need.  You'll need to 'install' blat so that it knows what smtp server to use (can be ip address or name, so long as it can be found - ie; 123.123.123.123 or mysmtp.srv.com ).   Just run blat by itself from the command line w/out any arguments and it will spit out a ton of stuff.  Basically all you need is something like this:

blat -install

Make sure to place the files in the zip into some directory on your nt (xp in your case) path - I like to use c:\sysutil, and you'll be good to go.

Sample .cmd file contents pasted in below.  I numbered the lines in case some of them wrap....

1:  @echo off
2:  ::
3:  :: capture current ip addressing info for this machine
4:  :: and send it off in an e-mail to me
5:  ::
6:  
7:  c: & cd \<> & lynx -source http://www.whatismyip.com > whatismyip.txt
8:  find /i "title" whatismyip.txt > ip_nfo.txt
for /f "tokens=4" %%a in (ip_nfo.txt) do set ipaddy=%%a
9:  c:\sysutil\blat c:\sysutil\ip_nfo.txt -subject "<> IP Address is %ipaddy%" -to <>
10:  cls
11:  exit

I don't know how familiar you are with dos/nt batching.  Remove the numbers and the colon (1: - 11:) before each line above before running the file.  The .cmd file in the zip that you can download is not numbered, but will have to be edited as req'd.

Hope this helps - let us / me know, ok?

-=- jd -=-
Title: Re: ip change notification script help
Post by: Bart Wittkampf on September 27, 2003, 02:03:16 PM
Jim, thanks a lot for the info.

I tried it and it seems to work. This will do for now, but I am still looking for a *magical* 'ip change notification' script that runs purely from my SME box.

Thanks again for your time!

Bart
Title: Re: ip change notification script help
Post by: Bart Wittkampf on September 27, 2003, 02:48:17 PM
I recently discovered another thread concerning ip change notifications:

http://forums.contribs.org/index.php?topic=7619.msg28032#msg28032

Including this script:

-------------------------
Author: Robert
Date:   05-13-03 15:24

Or save the script below as mail-external-ip, put it in /etc/e-smith/events/actions/, and do:
# cd /etc/e-smith/events/ip-change/
# ln -s ../actions/mail-external-ip S95mail-external-ip

This will send an email with the new ip to the specified email addresses each time the ip changes. Be sure to save the script as a Unix text file to avoid problems with CR/LF.

#!/bin/sh

recipients="address1@domain1.tld address2@domain2.tld"
mail -s "hostname -f now at $2" $recipients <As of date the external IP of hostname -f is $2.
EOF
-------------------------

Some people reported problems with mail on the command line, but that works for me. ($ echo "This is test one" |mail -s test1 robert).

I can't seem to get the script running however. What exactly do I have to put in the mail-external-ip file? Everything, or just the 'recipients="addre -> EOF' part? And what do I have to do with the #!/bin/sh ?

Could someone clarify this a bit for me? And is there an easy way to check if the script works?

Thanks a thousand,

Bart
Title: Re: ip change notification script help
Post by: Michiel on September 27, 2003, 04:15:13 PM
I suggest you first read some intriduction to shelll scripting. Just about any book on Linux has a chapter on it, but you can also find tons of pages on the internet (e.g. http://www.tinker.ncsu.edu/LEGO/shell_help.html). It will unleash the true power and flexibility of Linux for you.

Basically you have to put all lines in a file, save it in etc/e-smith/events/ip-change/mail-external-ip and make the file executable. The above link explains pretty well how to do that.

Michiel
Title: Re: ip change notification script help
Post by: Jim Danvers on September 27, 2003, 07:12:12 PM
Bart...  

I forgot to mention (you reminded me) about the 'magic' part of my little 'solution' - you also need to create an AT job on the nt box (cron in linux) to auto-send you the e-mails on X days at X time.  Granted, still not as slick as what could likely be achieved with a pure lnux / SME implementation - but it works.  I have nt boxes all over the place, thus it works for me....

FWIW, on nt4, just open cmd shell and type:

at 12:00 /every:m,w,f c:\sysutil\mail_ip.cmd

The above would send you the external ip address at 12:00pm on monday, wed, and friday.  In 2k and up, there is a graphical thing (scheduler I think its called) in the control panel.  Its use is pretty straight fwd to figure out.

-=- jd -=-
Title: Re: ip change notification script help
Post by: Bart Wittkampf on September 28, 2003, 01:36:54 AM
Hi everyone!

Jim thanks for your update, but I already got it to work (with some help of a roommate). My next challenge is with the script on my SME box. Michiel, I read the page you pointed me to, and it was very helpful! I love the “don’t give people fish, but teach them how to catch fish” approach, and did my best to work on the script.

However I still have some problems, on which I could use some advice from one of the guru’s on this board (smile). Let me tell you what I did, and where I ran into some problems.

Please bear with me, because this was my first attempt at ‘programming’.

Wrote script with PICO and saved to /etc/e-smith/events/actions/mail-external-ip :

     #!/bin/sh
     # script which sends mail when ip address changes
     
     recipients="bart@alternatemailaddress.nl"
     mail -s "hostname -f now at $2" $recipients <     As of date the external IP of hostname -f is $2.
     EOF

Chmodded it:

     # Chmod a+x mail-external ip

Made a shortcut in /etc/e-smith/events/actions/mail-external-ip dir.

     # cd /etc/e-smith/events/ip-change/
     # ln -s ../actions/mail-external-ip S95mail-external-ip


Some observations:
1. Shortcut is named: @S95mail~rnal-ip

My guess is that this isn’t a problem, but just because SME can only handle file types with limited lengths.

2. When trying to execute the script:
     # sh mail-external-ip

I receive the following mail message:

As of Sat Sep 27 22:10:16 CEST 2003 the external IP of Shoot.wittkampf.nl is .

The message is being sent, so that is a good thing, but I am not there yet; can someone shed some light on these things:

1. The mail message I receive, does not capture the external ip address. That seems logical, because the script doesn’t know what variable $2 is. How can I get this variable to be the external ip address?
2. Is this script being ran each time my ip address changes because there is a shortcut in the /actions directory, or do I have to make some more changes?

As always, comments are appreciated,

Bart Wittkampf
Title: Re: ip change notification script help
Post by: Michiel on September 28, 2003, 02:36:45 AM
> My guess is that this isn’t a problem, but just because SME
> can only handle file types with limited lengths.

Hey, this aint MS-DOS 4.2!! Linux can handle filenames longer than you can memorize ;-) (about 255 characters, if I'm not mistaken). The reason you're seing the ~ is probably because of the way you are looking at the direcory. Try ls -l to get a long listing format.

> ip address. That seems logical, because the script doesn’t
> know what variable $2 is. How can I get this variable to be

$2 Refers to the second argument from the command line. Since you ran the script without arguments, $2 will be empty. Try "mail-external-ip one two" to see what I mean.

To get the IP address mailed to you, try the following (untested) script:

#!/bin/sh
recipients="bart@alternatemailaddress.nl"
IP=/sbin/e-smith/db configuration get ExternalIP
mail -s "hostname -f now at $IP" $recipients <As of date the external IP of hostname -f is $IP.
EOF

> 2. Is this script being ran each time my ip address changes
> because there is a shortcut in the /actions directory, or do

The correct way is to put the script in the /actions directory and a link in the /ip-change dir. All the links in the /ip-change dir will be automagically executed when your IP changes.

BTW, "shortcut" is windoweeze. Real men call it links ;-)

M
Title: Re: ip change notification script help
Post by: Michiel on September 28, 2003, 02:44:58 AM
Oups, should be:

#!/bin/sh
recipients="bart@alternatemailaddress.nl"
IP=/sbin/e-smith/db configuration get ExternalIP
mail -s "hostname -f now at $IP" $recipients <As of date the external IP of hostname -f is $IP.
EOF
Title: Re: ip change notification script help
Post by: Bart Wittkampf on September 28, 2003, 03:21:24 PM
Oops sorry about the ‘shortcut’ part!  I will call them ‘links’ from now on :-)

# sh mail-external-ip one two

Returned a mail with: “your IP address is now two”. So that means that $2 needs to be my external ip. Let me take a look at your script:

#!/bin/sh
recipients="bart@alternatemailaddress.nl"
IP=/sbin/e-smith/db configuration get ExternalIP
mail -s "hostname -f now at $IP" $recipients <As of date the external IP of hostname -f is $IP.
EOF

I tested:

# sbin/e-smith/db configuration get ExternalIP

Which resulted in my IP address. So that is a good thing. I PICO’ed the script, and saved it to mail-external-ip.

Now let’s test the script:

# sh mail-external-ip

Yes, I received a mail with my external IP address. So it seems the script works. Now it is waiting till my external IP address changes, so I can see it in action. Is there a way to force an IP change (with a release command?)? And one more question: does this script still work after rebooting the server?

Have a nice day!

Bart
Title: Re: ip change notification script help
Post by: Bart Wittkampf on September 28, 2003, 05:07:43 PM
I checked the script with
# /sbin/dhcpcd eth1 -k
# /sbin/dhcpcd eth1 -n

(kill & renew of lease)

It worked. There is one strange thing however. I checked it twice and the first time I received a mail from root@myadress, and the second time from anonymous@myadress. Pretty strange...... anyone have an explanation?

Bart
Title: Re: ip change notification script help
Post by: Robert on September 29, 2003, 12:41:08 AM
Maybe I can help, since I wrote that mail-external-ip scriptlet. Of course the scriptlet won't know what $2 is outside the proper context. That's why you put it in the ip-change event. signal-event calls ip-change (1st argument) with the new external ip as second argument. Look at set-external-ip. I think it's overkill to first store the ip in the configuration dbfile and then fire up a perl script to get it out of the dbfile again. That's why I used the positional parameter. HTH
Title: Re: ip change notification script help
Post by: Bart Wittkampf on September 29, 2003, 01:05:06 AM
Haha! I checked it, and the original script works exactly as it should work:

#!/bin/sh

recipients="address1@domain1.tld address2@domain2.tld"
mail -s "hostname -f now at $2" $recipients <As of date the external IP of hostname -f is $2.
EOF

I feel kinda stupid now, but at least a learned a few things about SME!

Thanks for the update!

Bart
Title: Re: ip change notification script help
Post by: Jim Danvers on September 29, 2003, 06:39:59 AM
Hey Bart, and everyone else...

This really turned out to be quite the little thread, eh?  Bart - I'd say that you learned a little more than just something relevant to SME - you (we) all prolly picked up on a thing or two on linux and some basic bash/shell scripting in general.  Good stuff indeed.  I'm planning on playing around with the results of this thread myself (the script that you are now using) on another machine that I'm thinking about cobbing together.  I don't really have a need for it on the sme box that I use @present for my own uses as I use a dyndns service for my little domain name.  The other box that I'm thinking about running though won't have a domain name attached to it - this solution would be perfect.  ;)

-=- jd -=-