Faking a Static IP @ Home

Today I share my BASH script which automatically sends out an e-mail notification whenever the dynamic WAN IP address changes on your (Linux) system.

If you have a Linux box at home, and would like to know its address always, flawlessly, here is one possible solution not based on a third-party (paid) dynamic DNS service provider.

The script uses Ipify.org to fetch the WAN IP address, then sets up an authenticated SMTP session with an e-mail server of your choice and sends the IP address if it changes within intervals of 5 minutes. Note that the authentication require support for AUTH on the SMTP server. Also, you’ll need Perl on your Linux system for Base64 encoding of the authentication string.

To automatically start the script in the background on boot, add a cron entry as follows:

$ crontab -e

On a single line, enter:

@reboot /path/to/update_ip.sh
#!/bin/sh
#
# update_ip.sh: Fake Static IP by geir.kjetil.nilsen@gmail.com (2020)
#

DOMAIN=my.e-mail.domain.goes.here #(example: hotmail.com)
SMTPHOST=my.e-mail.smtp-server.hostname.goes.here #(typically, the SMTP server hostname is smtp.$DOMAIN, e.g. smtp.hotmail.com)
SMTPPORT=465 #(SMTP port number)

MYUSER=my.e-mail.user.name.goes.here
MYHOSTNAME=my.local.hostname.goes.here #(free choice!)
MYPASSWD=my.e-mail.password.goes.here
MYAUTH=$(perl -MMIME::Base64 -e "print encode_base64(\"\000$MYUSER\@$DOMAIN\000$MYPASSWD\")")
MYIP=""

while true; do
MYIP_NEW=$(curl 'https://api.ipify.org?format=text')
if [ "$MYIP" != "$MYIP_NEW" ];
then
if expr "$MYIP_NEW" : '[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*$' >/dev/null; then
MYIP=$MYIP_NEW
# Send e-mail notification
#: '
(echo "ehlo $MYHOSTNAME";
sleep 1;
echo "auth plain $MYAUTH";
sleep 1;
echo "mail from:<$MYUSER@$DOMAIN>";
sleep 1;
echo "rcpt to:<$MYUSER@$DOMAIN>";
sleep 1;
echo "data";
sleep 1;
echo "subject: Oh, behave! $MYHOSTNAME's got a brand new IP!"
sleep 1;
echo $MYIP;
sleep 1;
echo ".";
sleep 1) | openssl s_client -crlf -connect $SMTPHOST:$SMTPPORT
#'
else
echo "Discarding new IP - it is not valid."
fi
fi
# Wait 5 minutes...
sleep 300;
done