[shell help]msmtp send notification after ip change

this is my old topic sSmtp send email with dynamic ip renew script, since ssmtp is no longer supported i cant use this.

current shell looks like this, but it doesnt work.

root@OpenWrt:~/bin# msmtp check_dynamic_ip.sh
^C
root@OpenWrt:~/bin# cat check_dynamic_ip.sh 
FROM=sender@openwrt
HOSTNAME=$(cat /proc/sys/kernel/hostname)
MSG="$HOSTNAME IP change, please review"

MYIP=$(ifconfig pppoe-wan | grep 'inet addr'| awk '{print $2}' | cut -d ':' -f 2)
LASTIPFILE='/tmp/last_ip_addr'
if [ ! -f "$LASTIPFILE" ]
then
    LASTIP="0.0.0.0"
else
    LASTIP=$(cat ${LASTIPFILE})
fi

mailto ()
{
 {
  echo To: "$TO"
  echo From: "$FROM"
  echo Subject: "$MSG"
  echo
  echo "New IP = $MYIP"
 } | msmtp -t < check_dynamic_ip.sh "$TO"
}

if [[ "${MYIP}" != "${LASTIP}" ]]; then
  mailto
  echo -n "${MYIP}" > "${LASTIPFILE}"
fi
root@OpenWrt:~/bin# 

root@OpenWrt:~/bin# cat /etc/msmtprc 
# Example for a system wide configuration file

# A system wide configuration file is optional.
# If it exists, it usually defines a default account.
# This allows msmtp to be used like /usr/sbin/sendmail.
account default

# The SMTP smarthost
host poczta.o2.pl

# Use TLS on port 465. On this port, TLS starts without STARTTLS.
port 465
auth on
user XXXXXX@o2.pl
password XXXXXXX
tls on
tls_starttls off
tls_certcheck off
# Construct envelope-from addresses of the form "user@oursite.example"
from XXXXXXXX@o2.pl
# Do not allow programs to override this envelope-from address via -f
allow_from_override off
# Always set a From header that matches the envelope-from address
set_from_header on

# Syslog logging with facility LOG_MAIL instead of the default LOG_USER
syslog LOG_MAIL
logfileroot@OpenWrt:~/bin# 

My ISP renews connection after 14400 minutes for some reason, no-ip.com crying after renewal after 30 days. in case i forget renew ddns hostname this will be me second backup.
This script is wrong since, msmtp requires separate file to read

There are ddns providers, even free-of-charge ones, that don't require those shenanigans. The sensible solution would be to use one of those, rather than working around the problem with mailing home the IP address.

--
yes, we've all done something like that in the past as an additional belt-and-suspenders safety measure, the problem is just if you do expect to require it. Then it becomes just yet another mission critical aspect that will fail when you need it most, so better make your primary method (ddns), the one that's actually convenient, fail-safe enough to render the suspenders merely decorative and optional.

3 Likes

Maybe look into ntfy.sh

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.