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
file last_ip_addr must exist,
will be erased after reboot
cant be empy at 1st run of script
something else?
does anyone have more developed script, or any other easy way ?
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.
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?
Subject: Your subject goes here
To: Destination <destination@example.com>
From: Sender <sender@example.com>
Body text goes here after a "blank" line
...
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}"
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