This post describes to configure a web server in a way that it sents its IP address by email in a defined interval of time. This can be useful for example if your server has a dynamic IP address that changes with the time. In this example the tool Postfix is used to sent the emails instead of sendmail since it works similar and is way easier to configure than sendmail.
1. Install Postfix
In a first step it is necessary to setup and install postfix. You can download the source code here but most recent linux distributions include the package already. In Fedora the setup can simply be done by:
yum install postfix
2. Configure Postfix
You need an SMTP server in order to sent the emails. If you have an email account with SMTP Server you can add the following lines to the postfix configuration file /etc/postfix/main.cf :
relayhost = smtp.provider.com:25
smtpd_sasl_auth_enable = yes
smtpd_sasl_path = smtpd
smtp_sasl_password_maps = hash:/etc/postfix/passwd
smtp_sasl_type = cyrus
smtp_sasl_auth_enable = yes
Now the password file /etc/postfix/passwd has to be created. The password file has to contain the line
To convert the password file in a so called postmap look-up table do the following:
postmap -q smtp.provider.com:25 /etc/postfix/passwd
gives you the correct reply everything is working fine.
3. Test Postfix
To test this configuration, postfix has to be started
Postfix now provides a sendmail command you can use like follows to test your setup:
Create a simple text file test.mail:
(compulsory blank line at the end)
and execute the command
sendmail -f email@example.com -s firstname.lastname@example.org < test.mail
The mail should have been sent now. If something is not working errors can be found in the following file:
If you find something like
warning: SASL authentication failure: No worthy mechs found
status=deferred (SASL authentication failed; cannot au
thenticate to server smtp.provider.com: no mechanism available)
you probably have to add the following line to your /etc/postfix/main.cf:
smtp_sasl_security_options = noanonymous
Howto sent the IP by email:
To sent the IP address of you server by emil you simply you have execute the following simple script send_ip.sh by a cron job.
wget http://checkip.dyndns.com/ -O ip.txt
sendmail -f email@example.com -s firstname.lastname@example.org < ip.txt
rm -f ip.txt
http://checkip.dyndns.com/ can be replaced by any web page that is displaying your IP.
The only thing that is missing now is to setup a cron job that executes the script in a defined interval of time, lets they every six hours.
As root do:
and add the following line:
0 */6 * * * source /home/user/send_ip.sh ;
Now start the crond and thats it!
/sbin/service crond start
chkconfig crond on
You have to make sure, that the postfix mailing system is always running. In case to ensure that postfix sendmail command is not interferring with the “normal” sendmail command stop it.
service sendmail stop