Click to See Complete Forum and Search --> : Send mail at boot


dreamie
01-11-2004, 10:00 AM
Hi!

Im really new to Linux and would like to know how I could do the following things:

1. After system boot I want to execute a number of commands. I want to retrieve the system ip configuration and mail it to a pre-defined address using a pre-defined mailserver.

Something like this:
ifconfig > myip.txt
mail -s "My new ip" -a myip.txt mymail@mydomain.org

2. My problem is that I don't know where to set the smtp server to use, i.e. my ISP smtp server (e.g. smtp.myisp.com). I guess it is in one of all config files on my linux system but havn't been able to locate the correct one.


How do I execute these steps automagically when the system boots? Should I use perl or any other language or is there a much more simple way?

thanks in advance,

Peter

bwkaz
01-11-2004, 05:42 PM
Your system doesn't have an SMTP server set up anywhere. I believe that /bin/mail talks directly to the SMTP server on the other end of the connection (in your example, that's the IP address that the MX record for mydomain.org resolves to).

Unless the "mail" program that you're using is part of Sendmail or another mail daemon -- then it might use localhost, which won't work unless the mail daemon is running. Or it might start up a temporary instance of your mail daemon just to handle this one request -- in effect, this is the same as talking directly to mydomain.org.

You can put the script into /etc/rc.d/init.d/rc.local if you want it to run at the end of booting. If you put it in that file, you'll have to use shell (which is probably easiest anyway -- just copy what you have into the file, after making sure that the "mail -s "My new ip" ..." command works).

nabis
01-11-2004, 06:06 PM
that what I put in /etc/ppp/ip-up:
<snip>
# If you want sendmail to send any mail in /var/spool/mqueue when
# you connect, remove the # below.
#sendmail -q
/sbin/ifconfig ppp0 | grep inet | mail -s "$(date)" nabis@myisp.com
# If you want fetchmail to get your mail when you connect and check
# every 300 seconds = 5 minutes for mail, remove the # below.

#fetchmail -d 300

using Slackware and dialup.

dreamie
01-12-2004, 02:48 AM
Thanks guys!

I will try /etc/rc.d/init.d/rc.local.

I believe that /bin/mail talks directly to the SMTP server on the other end of the connection

So you mean that I can't specify a certain smtp server that /bin/mail (or nail that SuSe has) should use? I believe that mail (nail) is using sendmail, does this change things?

As I am sending mails to my self in this test phase, I use a mail address with the same domain as the smtp server is on. i.e. myisp.com = mydomain.org, but how would "mail" know the actual smtp server is that domain with the smtp prefix? (smtp.myisp.com???). I'm a bit confused here and maybe I need to go back to my school books in networking and smtp but shouldn't you be able to specify a certain smtp server for "mail"?

I more thing. Do I need to be logged on to perform these actions? I don't have any auto-login so I hope not :-)

Im using SuSe 9.0, and just want to send a mail at boot with info of the current ip number. My internet connection is VDSL with DHCP enabled.

thanks again!

/ Peter

bwkaz
01-12-2004, 09:58 PM
Originally posted by dreamie
So you mean that I can't specify a certain smtp server that /bin/mail (or nail that SuSe has) should use? I don't know for sure, but I don't think so.

I believe that mail (nail) is using sendmail, does this change things? Having never used sendmail, I don't know.

Wow, I'm doing good here... or not. :p

As I am sending mails to my self in this test phase, I use a mail address with the same domain as the smtp server is on. i.e. myisp.com = mydomain.org, but how would "mail" know the actual smtp server is that domain with the smtp prefix? (smtp.myisp.com???). This one I can answer -- MX records in the DNS.

Whether your outgoing mail server is doing the sending or your /bin/mail is doing it itself doesn't matter here. They both query your DNS server for an MX (mail exchange) record for the domain that they're sending to. In your example, since you're sending it to myisp.com (uh, I think), the SMTP client (/bin/mail and your normal SMTP server are both SMTP clients at this stage) looks up an MX record in DNS for myisp.com. This will return something like smtp.myisp.com (or mail.myisp.com, or whatever). The SMTP client then looks that name up, and sends the mail to the resulting IP address.

I'm a bit confused here and maybe I need to go back to my school books in networking and smtp but shouldn't you be able to specify a certain smtp server for "mail"? If it's able to speak SMTP itself, then no, you don't have to. Maybe you can, but you don't have to.

You can do the same type of thing with your normal mail user agent -- specify the target domain as the SMTP server for your outgoing mail. If it's going to anyone not on that domain, then the target mail server will probably refuse to forward it for you, but in most other cases it should accept it.

1 more thing. Do I need to be logged on to perform these actions? I don't have any auto-login so I hope not :-) Not sure. You might have to specify the from address to /bin/mail. Otherwise, it'll probably come from root (since that's who all your init scripts run as).

dreamie
01-13-2004, 03:40 PM
Hello again!

Thanks for all your help! I think I got it to work now. All I needed was actually to add the reply address as bwkas mentioned, and then use the line nabis gave me and I got new mail :D

/sbin/ifconfig eth0 | grep inet | mail -s "BOOT New IP $(date)" -r aname@from.com aname@to.com

There is still one nut to crack though. :rolleyes: It wouldn't work as I put it in the startup file. I am not sure I have internet connection at the time I execute my command in rc.local (or boot.local as it is called in SuSe).

I have now tested with this line:
/sbin/ifconfig eth0 | grep inet > /etc/rc.d/ATEST.txt

it produces an emtpy file at startup, but if I run it manually after logging in it produces a file with the correct IP information

Do you have network connection at the time you reach rc.local? The file information says that things put in it will be executed after booting, just before the first run level... what does that mean?

Any ideas?

Thanks again!

/ Peter :confused:

bwkaz
01-13-2004, 08:43 PM
"Just before the first runlevel" makes it sound like this isn't the right script. My LFS boot scripts run rc.local as the absolute last thing before producing a "login: " prompt.

"Just before the first runlevel" makes me think that this happens before you switch into runlevel 3 or 5 -- and runlevel 3 is what sets up networking. The "login: " prompt gets shown after runlevel 3's scripts all run (so in my case, rc.local is the last thing to run in runlevel 3).

BTW -- runlevels are just states the system can be in. Each runlevel is associated with a group of scripts that get run when the runlevel is entered.

Look through the symlinks in /etc/rc.d/rc3.d (or .../rcX.d if you boot to runlevel X -- a grep initdefault /etc/inittab, then looking for the only number in the line of output, will tell you what runlevel you boot to). The last one in the directory that starts with the letter S will be executed last (they go in order by the numbers after the initial S or K).

If that one doesn't look like it'd be friendly to customization, then create a new file in /etc/rc.d/init.d/ that contains the stuff you want to run. Make it executable, and create a symlink to it in /etc/rc.d/rcX.d (number the symlink so it runs last).

dreamie
01-14-2004, 03:40 AM
I humbly thank you again bwkaz!

Look through the symlinks in /etc/rc.d/rc3.d ... The last one in the directory that starts with the letter S will be executed last (they go in order by the numbers after the initial S or K).
If that one doesn't look like it'd be friendly to customization, then create a new file in /etc/rc.d/init.d/ that contains the stuff you want to run. Make it executable, and create a symlink to it in /etc/rc.d/rcX.d (number the symlink so it runs last).

I boot to runlevel 3, that I know. I will have a peak at /etc/rc.d/rc3.d/ to see if I understand you correctly. As I understand it I have a /etc/init.d and a /etc/rc.d not a /etc/rc.d/init.d ... but that's the same I guess.

I will post a message about this in installation forum to see if anyone knows how it works on SuSe, when I get back home I will try your suggestion as well!

thanks again!

/ Peter

dreamie
01-15-2004, 11:14 AM
Hi Again!

It now works as it should! Thanks to bwkaz and nabis!

I will post thread in "How I Did"-forum with the complete story.

thanks again!

/ Peter