auto IP checking and email notification

Status
Not open for further replies.
Joined
Jul 16, 2016
Messages
17
Dear all,
it's my first post, hi everybody!!!
I built my first FN box and thanks to all guides and forum posts I got a successful installation!

I'm only trying to do something and I can't, probably because I'm a complete newbie to this world. My ISP router has dynamic IP; honestly I saw they change it very rarely, but I would like to have a "script" to make my FN checking my router IP and, if changed, send an email.

How can I do that?

Thank you very much!
 

joeschmuck

Old Man
Moderator
Joined
May 28, 2011
Messages
10,996
I'd think you could write a script to periodically check to see if the IP address changes using "curl ifconfig.me/ip" and then once you detect a change, toss yourself an email with sendmail.

If you are asking someone else to do the work for you, well I think it's time to put on your thinking cap. I had no idea how to check for the IP address within FreeNAS but it took me under a minute using Google.

Now if you are trying to have your IP address available so you can remotely access your server, you could just use a free DNS service and setup FreeNAS to forward the IP address whenever it changes.

But, writing a script isn't too bad. There are lots of scripts here that you can use for the main structure and adjust as needed. I have several scripts in these forums and they are typically well commented so you know what each command does.

Good Luck, hope to see your results. And if you run into some problems writing the script, just post what you got and we will offer our two cents to help you along.

EDIT: Also, I'd recommend doing your work within a jail to keep your FreeNAS installation clean.
 
Joined
Jul 16, 2016
Messages
17
Well.. it's harder than what I thought... I'm sure it's pretty easy for many of you, but I really can't understand how it works. I could get my external IP with this easy command:

curl http://ipinfo.io/ip

Typing that in shell I immediately get my IP, as a clean address. Then I found this script for sending an email about SMART:

smartctl -a /dev/ada1 | /usr/bin/mail -s "MyFREENAS HDD /ada1 report " my.email@address.com

It does work for me and I correctly get that email. But... I couldn't "mix them". I tried:

curl http://ipinfo.io/ip | /usr/bin/mail -s "MyFREENAS HDD /ada1 report " my.email@address.com

But I got a strange email, without my IP address. That being said, I don't need just an email with IP address, but an email when my IP changes. So I suppose I have to write a script that checks my IP address and store it in a log, and when it's different it has to send an email.

Please help!! :)

Thx
 

joeschmuck

Old Man
Moderator
Joined
May 28, 2011
Messages
10,996
I would recommend you take a two step approach...

I would do it something like this...

1) Create a script that you can run via CRON periodically, maybe once every 10 minutes.
2) Within that script I'd check to see if a specific file exists first, lets call the file "my_ip_addr.txt". If the file does not exist then run the curl and save the output to file "my_ip_addr.txt"
3) If the file does exist, now run curl but compare it to the data in "my_ip_addr.txt", if it is the same then exit. If it's different then save the new value in the file and send an email with the new IP address.

Just break the steps down into individual parts and test each one individually. Put them together once you have each one working. It may take a while to learn this but I'm sure you will get it.

I'm headed out for dinner but if I can toss you a quick script later tonight to get the ball rolling if you like.
 

joeschmuck

Old Man
Moderator
Joined
May 28, 2011
Messages
10,996
Here is a script from which you can pull inspiration from. I used "curl" because it was easy to implement and you didn't need to know what the interface name was. You will need to change file paths to your liking and adjust the section for sendmail to work, but Google should be able to help you out a lot here. Then place this into the CRON and run it once every 10 minutes or maybe 30 minutes, whatever you are comfortable with.

Good Luck!

Code:
#!/bin/sh
# Script Name: get_url.sh
# This script will check to see if your IP address has changed.  You can
# set it up to send an email out but I have not completed that portion.
#

# Lets define the path to where our files are stored.  This is just an example.

oldfile="/mnt/farm/benchmark/old_ip_addr.txt"
newfile="/mnt/farm/benchmark/new_ip_addr.txt"

# Next lets check if old_ip_addr.txt file exists, meaning we have something to
# compare afgainst to see if the IP address has changed or not.

if [ -f "$oldfile" ]
then
# The file was found which is a good thing.
# Now lets store the current IP address in a file.
curl ifconfig.me/ip > "/mnt/farm/benchmark/new_ip_addr.txt"

# Now lets compare the two files.  If they are the same then nothing has changed.
diff -u "$oldfile" "$newfile" > /dev/null
if [ $? -eq 0 ] ; then
echo "IP Address Has Not Changed, it is still "; cat /mnt/farm/benchmark/new_ip_addr.txt
else
echo "New IP Address Detected:"; cat /mnt/farm/benchmark/new_ip_addr.txt
# Here is where you want to send an email out and send the data in the file
# named /mnt/farm/benchmark/new_ip_addr.txt.
# sendmail yadda yadda yadda
#
#
# And finally we will make the files compare again, ready for the next check.
cp /mnt/farm/benchmark/new_ip_addr.txt /mnt/farm/benchmark/old_ip_addr.txt

fi

else
# The file was not found meaning this is the first time running this program.
# Since the file isn't here, lets create it.
curl ifconfig.me/ip > "/mnt/farm/benchmark/old_ip_addr.txt"
fi


EDIT: You may need to chmod the file to make it executable "chmod +x get_url.sh".
 
Status
Not open for further replies.
Top