If you want to connect to your Digital Video Recorder (DVR) or ownCloud installation from the internet, you need to know to which IP address you need to connect (See this DVR article as well). The bad thing however is that the most of us don’t have a fixed IP address on the internet (this might change in the future with IPv6). This means your IP address can change in time. There are several payed services to solve this problem like DynDNS, DuckDNS, DtDNS, No-IP, etc. Most of the time you are the only user of the services you are hosting at home, if so than there is a cheap solution.
As described above the whole thing is, that you need to know which IP address your Raspberry Pi has. The solution is to write a script that checks your external IP address and that sends you an email in case the external IP address has changed.
Login on your Raspberry Pi with SSH:
ssh pi@your IP address
cd ~
nano ./ipaddrcheck.sh |
Now copy and paste the following code to nano (of course you need to change username@domain.ext to your own email address):
#!/bin/bash
curl -o ~/newip ifconfig.co
cmp ~/newip ~/oldip >/dev/null || {
mv ~/newip ~/oldip
mailx -s “I – IP ${HOSTNAME} changed” username@domain.ext < ~/oldip
} |
Use Ctrl O to save the file
Use Ctrl X to exit the nano editor
Make let’s make the script executable:
sudo chmod +x ./ipaddrcheck.sh |
Let me roughly explain what the script does:
It’s a bash script that checks the external IP address by using curl ifconfig.co and writes the results to a file called newip.
After that it checks whether the IP address has changed by comparing the files newip and oldip, in case the two files are not the same it sends you an email.
Now we need to install the necessary packages to sent mail:
sudo apt-get install ssmtp heirloom-mailx |
The next thing is to configure your Raspberry Pi so it’s able to send mail. Therefore you need to add the following lines, at the end of the file /etc/nail.rc :
Now copy and paste the following code to nano (of course you need to change: smtp.domain.ext, username@domain.ext, password and email sender’s nice name to your own email settings):
# Smtp server
set smtp-use-starttls
set ssl-verify=ignore
set smtp=smtp://smtp.domain.ext
set smtp-auth=login
set smtp-auth-user=” username@domain.ext”
set smtp-auth-password=”password”
set from=”email sender’s nice name” |
Use Ctrl O to save the file
Use Ctrl X to exit the nano editor
Now we need schedule and execute the script by using crontab:
Add the following line, at the end of the file (it schedules the ipaddrcheck.sh every hour):
0 * * * * /home/pi/ipaddrcheck.sh |
Use Ctrl O to save the file
Use Ctrl X to exit the nano editor
You can check the date and time stamp of the file ~/newip to see whether your script ran.
Changing the IP address in ~/oldip enables you to check whether emailing works, as it should send you an email the next time the sript runs.
PS. if mailing yourself doesn’t work, try editing the /etc/ssmtp/ssmtp.conf file (as it heavily depends on your mailserver configuration).
Suggestions for improving this article are welcome, please let me know and drop me a line .