Script to keep ZoneMinder email options IP address current

Forum for questions and support relating to the 1.28.x releases only.
Locked
kenneth558
Posts: 18
Joined: Mon Jul 27, 2015 3:57 am

Script to keep ZoneMinder email options IP address current

Post by kenneth558 »

This is for all who have dynamic addressing from their ISP and don't want to use a 3rd party domain-name service like dyndns.org. (I decided I didn't want to use dyndns.org for reasons now forgotten, maybe because I didn't want their daemon running on my server? I didn't know I could use ddclient instead of their daemon. Or maybe I had concerns about expiration/renewal/longevity of the service?)

I set cron to run this script every half hour. It emails me when the IP address changes, but the biggest thing it does is keep ZoneMinder's ZM_URL up-to-date that you can encode in emails and texts sent out by any filters you have set for that (%EP% and %EPS%, and the video variables, too). If you know how to accomplish the mysql command without including the password in clear text in this script, maybe you could let us all know.

This script uses 'dig' to get the public IP address (which I believe I had to install from repositories) but there are other ways you could choose to get your public IP.

#!/bin/bash
#site-specific variables:
webpageprotocol=https
mysqlpsswd=
emailsubjectline='New IP address for surveillance system'
emaildestination='email@gmail.com'
file_to_store_ip='/usr/oldip'

#variable to hold gotten public IP address
ip_from_resolver=$(dig +short myip.opendns.com @resolver1.opendns.com)

function valid_ip() # credit to Mitch Frazier, author of this function
#Just keep in mind that this will break if/when IPv6 addressing is used
{
local stat=1
if [[ $1 =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]; then
OIFS=$IFS
IFS=$1
ip=($1)
IFS=$OIFS
[[ ${ip[0]} -le 255 && ${ip[1]} -le 255 \
&& ${ip[2]} -le 255 && ${ip[3]} -le 255 ]]
stat=$?
fi
return $stat
}

if ( valid_ip $ip_from_resolver );
then
if ( [ -f $file_to_store_ip ] );
then
if ( valid_ip $(< $file_to_store_ip) && [ $ip_from_resolver == $(< $file_to_store_ip) ] );
then
exit
fi
fi
else
exit
fi
echo $webpageprotocol://$ip_from_resolver/zm | mail -s "$emailsubjectline" $emaildestination
echo $ip_from_resolver > $file_to_store_ip
mysql -D zm -u root -p$mysqlpsswd -e "update Config set Value='$webpageprotocol://$ip_from_resolver/zm' where Name='ZM_URL';"
Last edited by kenneth558 on Wed Oct 14, 2015 9:15 pm, edited 6 times in total.
Nerre
Posts: 100
Joined: Thu Sep 25, 2014 10:22 am

Re: Script to keep ZoneMinder email options IP address curre

Post by Nerre »

I prefer using a dynamic DNS service and never have to worry about IP addresses.
User avatar
knight-of-ni
Posts: 2404
Joined: Thu Oct 18, 2007 1:55 pm
Location: Shiloh, IL

Re: Script to keep ZoneMinder email options IP address curre

Post by knight-of-ni »

Writing your own scripts to do stuff can certainly be fun.

In this case however, the Linux application "ddclient" does exactly this.
It is available in the base repository of most linux distros.
Visit my blog for ZoneMinder related projects using the Raspberry Pi, Orange Pi, Odroid, and the ESP8266
All of these can be found at https://zoneminder.blogspot.com/
kenneth558
Posts: 18
Joined: Mon Jul 27, 2015 3:57 am

Re: Script to keep ZoneMinder email options IP address curre

Post by kenneth558 »

So noted in the OP. Thank you kindly.
Locked