sSmtp send email with dynamic ip renew script

orginal author:

#!/bin/bash

check and send ip address to email

MYIP=ifconfig pppoe-wan | grep 'inet addr'| awk '{print $2}' | cut -d ':' -f 2;
TIME=date;

LASTIPFILE='/tmp/last_ip_addr';
LASTIP=cat ${LASTIPFILE};

if [[ ${MYIP} != ${LASTIP} ]]
then
echo "New IP = ${MYIP}"
echo "sending email.."
echo -e "Hello\n\nTimestamp = ${TIME}\nIP = ${MYIP}\n\nBye" |
/usr/bin/mail -s "[INFO] New IP" nick@some.somewhere;
echo ${MYIP} > ${LASTIPFILE};
else
echo "no IP change!"
fi

root@OpenWrt:~#

there are few problems with this script

  1. file last_ip_addr must exist,
  2. will be erased after reboot
  3. cant be empy at 1st run of script
  4. something else?

does anyone have more developed script, or any other easy way ?

  1. Change LASTIPFILE='/tmp/last_ip_addr'; to LASTIPFILE='/etc/last_ip_addr';.

1 & 3 -- if you don't want to get notifications when LASIPFILE does not exist, change: if [[ ${MYIP} != ${LASTIP} ]] to if [[ -e "$LASTIPFILE" && ${MYIP} != ${LASTIP} ]].

PS. If it's crucial for you to know immediately when IP has changed, for Android there're apps like Join and NotifyMyAndroid which provide (shell one-liner) API for push notifications.

i just want to make this script working.

1 & 3 -- if you don't want to get notifications when LASIPFILE does not exist, change: if [[ ${MYIP} != ${LASTIP} ]] to if [[ -e "$LASTIPFILE" && ${MYIP} != ${LASTIP} ]].

i want to have notifications all the time, so if file doesnt exist, must be created .

actually i have some error:

sh: !=: argument expected
no IP change!

storage under /etc/ doesnt damage in long time flash?

If you want to have notifications "all the time", might as well keep the file in /tmp/. Also, then do if [[ "${MYIP}" != "${LASTIP}" ]]

root@OpenWrt:~# cat /overlay/ip\ check.sh


#!/bin/bash
# check and send ip address to email

MYIP=`ifconfig pppoe-wan | grep 'inet addr'| awk '{print $2}' | cut -d ':' -f 2`;
TIME=`date`;

LASTIPFILE='/tmp/last_ip_addr';
LASTIP=`cat ${LASTIPFILE}`;

if [[ "${MYIP}" != "${LASTIP}" ]]
then
        echo "New IP = ${MYIP}"
        echo "sending email.."

        echo -e "Hello\n\nTimestamp = ${TIME}\nIP = ${MYIP}\n\nBye" | ssmtp -v name@o2.pl | \

        echo ${MYIP} > ${LASTIPFILE};

> else
        echo "no IP change!"
fi
root@OpenWrt:~#

and

*/50 * * * * sh /overlay/ip\ check.sh

i m almost happy with that, works but, sender is hidden and recipient is hidden :frowning:

im total noob with coding can anyone code/syntax it properly? please

You may need to incorporate them in the message -- ssmtp is not a full-featured mail-sending client.

There are a couple options on that man page that may help, such as:

-Ffullname
Set the full name of the sender.

-fname
Sets the name of the ''from'' person. This option is valid only if no From: line is specified within the header of the email.

but it's "easier" (the way I think about things) to include them in the message.

https://www.ietf.org/rfc/rfc2822.txt -- for the format of the message itself

It probably needs to be something like:

Subject: Your subject goes here
To: Destination <destination@example.com>
From: Sender <sender@example.com>

Body text goes here after a "blank" line
...

Haven't tested it, but something like this should work with ssmtp:

#!/bin/sh
TO=recipient@gmail.com
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'
LASTIP=cat ${LASTIPFILE}

mailto ()
{
 {
  echo To: "$TO"
  echo From: "$FROM"
  echo Subject: "$MSG"
  echo
  echo "New IP = $MYIP"
 } | ssmtp "$TO"
}

if [[ "${MYIP}" != "${LASTIP}" ]]; then
  mailto
fi
2 Likes

@tojestzart "Welcome to the Dark Side" :wink:

Shell scripts can be written as many ways as there are authors, it seems. There is no one "right" (though a lot of ways that things don't work).

A couple things I've found helpful over the years:

  • sh isn't bash and many postings use "Bash-isms" that aren't supported with POSIX sh
  • quoting of strings can drive you nuts
  • Using variables with curly braces can makes things clearer ${LABEL}
  • "here documents" are one way to manage "long" variables or pipe input (one tutorial)
  • echo is fine for a single line of output, but printf is a lot more reliable and portable, especially if you need to do it without a newline; printf %s "${SOMESTRING}"

@jeff [edited] @ralpho your beauty works perfectly but:

root@OpenWrt:~# /overlay/ip\ check
-ash: /overlay/ip check: Permission denied
root@OpenWrt:~# sh /overlay/ip\ check
/overlay/ip check: line 11: /tmp/last_ip_addr: not found
root@OpenWrt:~# touch /tmp/last_ip_addr
root@OpenWrt:~# sh /overlay/ip\ check
/overlay/ip check: line 11: /tmp/last_ip_addr: Permission denied
root@OpenWrt:~# sh /overlay/ip\ check
/overlay/ip check: line 11: /tmp/last_ip_addr: Permission denied
root@OpenWrt:~# chmod 777 /tmp/last_ip_addr
root@OpenWrt:~# sh /overlay/ip\ check
root@OpenWrt:~#

after reboot:
there last_ip_addr doesnt exist,
secondly if doesNT (how?), has no privleges.

thanks for you script btw, i need to move my mind personally

# ls -l ip\ check 
-rw-r--r--    1 root     root           557 Aug  2 11:08 ip check
# chmod u+x ip\ check 
# ls -l ip\ check 
-rwxr--r--    1 root     root           557 Aug  2 11:08 ip check

Fixed issue with missing file and spamming

#!/bin/sh
TO=recipient@gmail.com
FROM=sender@openwrt
HOSTNAME=$(cat /proc/sys/kernel/hostname)
MSG="$HOSTNAME IP change, please review"

MYIP=$(ifconfig wlan0 | 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"
 } | ssmtp  "$TO"
}

if [[ "${MYIP}" != "${LASTIP}" ]]; then
  mailto
  echo -n "${MYIP}" > "${LASTIPFILE}"
fi
1 Like

actually i created spambomb for myself which wasnt wanted

cron:

*/3 * * * * sh /overlay/ip\ check

send ip only if changes?

check my previous post for improved script

1 Like

Despite the danger of stating the obvious, you are aware of DDNS?

Both in terms of free-as-in-beer services and free-as-in-freedom example code?

@slh

seems to work? need more testing

slh hmm i dont honestly, i need to have access to router when ip changes,
i know what has been told before is sensitive and even might be/should be hidden,

i need to read more / communicate under our local forums, regards

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