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:

[sourcecode language=”bash”]
yum install postfix
[/sourcecode]

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 :

[sourcecode language=”text”]
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
[/sourcecode]

Now the password file /etc/postfix/passwd has to be created. The password file has to contain the line

[sourcecode language=”text”]
smtp.provider.com:25 username:password
[/sourcecode]

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

[sourcecode language=”bash”]
postmap hash:/etc/postfix/passwd
[/sourcecode]

If
[sourcecode language=”bash”]
postmap -q smtp.provider.com:25 /etc/postfix/passwd
[/sourcecode]

gives you the correct reply everything is working fine.

3. Test Postfix
To test this configuration, postfix has to be started

[sourcecode language=”bash”]
postfix start
[/sourcecode]

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

Create a simple text file test.mail:

[sourcecode language=”text”]
From: you@provider.com
message
(compulsory blank line at the end)

[/sourcecode]

and execute the command

[sourcecode language=”bash”]
sendmail -f you@provider.com -s recipient@provider.com < test.mail
[/sourcecode]

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

[sourcecode language=”text”]
warning: SASL authentication failure: No worthy mechs found

status=deferred (SASL authentication failed; cannot au
thenticate to server smtp.provider.com: no mechanism available)

[/sourcecode]

you probably have to add the following line to your /etc/postfix/main.cf:

[sourcecode language=”text”]
smtp_sasl_security_options = noanonymous
[/sourcecode]

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.

[sourcecode language=”bash”]
#!/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
[/sourcecode]

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:

[sourcecode language=”bash”]
crontab -e
[/sourcecode]

and add the following line:

[sourcecode language=”text”]
0 */6 * * * source /home/user/send_ip.sh ;
[/sourcecode]

Now start the crond and thats it!

[sourcecode language=”bash”]
/sbin/service crond start
chkconfig crond on
[/sourcecode]

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.

[sourcecode language=”bash”]
service sendmail stop
postfix start
[/sourcecode]

Leave a Reply