Send Server Ip Address by Email

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.

Preparations:

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

smtp.provider.com:25 username:password

To convert the password file in a so called postmap look-up table do the following:

postmap hash:/etc/postfix/passwd

If

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 start

Postfix now provides a sendmail command you can use like follows to test your setup:

Create a simple text file test.mail:

From: you@provider.com
message
(compulsory blank line at the end)

and execute the command

sendmail -f you@provider.com -s recipient@provider.com < test.mail

The mail should have been sent now. If something is not working errors can be found in the following file:
/var/log/maillog

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.

#!/bin/bash
# send_ip.sh

wget http://checkip.dyndns.com/ -O ip.txt
sendmail -f you@provider.com -s recipient@provider.com < 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:

crontab -e 

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

Attention!
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
postfix start

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: