Dynamic DNS update client using python program for raspberry pi. PART1-(tutorial)

#MESSAGE 2 READERS: This article is for educative and refrence work only. A new modified and easy apprach has been designed and found working even better than this methode..It is posted in http://goo.gl/RqOnnJ.

For those who dont have static ip, the only hope of hosting any server is to use dynamic DNS service.
This article covers a little information about working of dynamic dns and 4 steps to make this process work using python program.

The working of dynamic DNS service is simple.
ddns-for-dvr
The DDNS process consist of 3 simple steps.
1) The system that needs to act as server (the ones without static ip) sends its external ip to an actual ddns provider(a computer with a static ip that stores the external dynamic ip of all clients and their domain name).
For example the camera in the image has an ip address of 117.231.220.140 at a pirticular time, the camera sends this ip address to its ddns server.
2) As this ip address keeps on changing at ISP determined time intervals, it is not feasible to access the camera with this ip address. So the ddns provider replaces this ip address with a domain name for example http://camera1111.ddnsprovidername.com.
So when ever any computer on outside world needs to connect to this camera, they try to access the address http://camera1111.ddnsprovidername.com, and the ddns server replies to the client with its present working dynamic ip 117.231.220.140.
3) Now computers across the world can access the camera using its pirticular dynamic ip address (which changes regularly) by just remembering the address http://camera1111.ddnsprovidername.com which doesnt change at all.

A little history: Here in my case, I have tried dyndns.org and no-ip.org, both of these ddns providers failed to activate my account with a reply email. Later i came to know that they have stopped there free service, and respond to paid services only. So i signed up with http://freedns.afraid.org. Ofcourse at first I was quite ashamed to use the term .afraid.org with my sub-domain name. In this pirticular website the term sub-domain and domain might confuse someone as it confused me at first. The address is http://subdomain.domain.com/org/sh etc. The subdomain can be changed as per need of the client. the domain.com/org/sh etc needs to be selected from the the predefined list. like .afraid.com, .mooo.com, .india.sh etc. To own a user defined domain name is a paid service.

In windows 7, I have tried to host a apache 2.4.4 webserver and got it working successfully, but to keep a windows system running for a long time is not practical in my case. So I switched to raspberry pi debian OS. In this piricular Operating system, to get a ip address-update-client like ddclient is easy, but to configure this program with http://freedns.afraid.org is quite hard. The program version 3.8.0 having its config file in /etc/ddclient.conf replies with error saying the protocol ‘freedns’ is having error.
Many users suggested to follow the official website and download and use the latest program version 3.8.1, which store the config file in /etc/ddclient/ddclient.conf. But here also there were some errors regarding libio-socket-ssl-perl.So finally decided to avoid all such ddclient etc and update the ipaddress using a python program.
I got more info from http://goo.gl/n9xtNv

This entire article was done using ssh into the pi using ubuntu terminal, for directions on how to use the pi headlessly go to http://goo.gl/6mh2cr.

THE 4 STEPS TO ENABLE PYTHON BASED IP UPDATER:
This pirticular python program rests in the directory /etc/cron.hourly/ . All scripts in this folder is executed on hourly basis, so if in case the dynamic ip changes the script will update the ip address with the ddns servers of afraid.org.

Step1: COPY THE DIRECT URL LINK
Before creating this script you need to get your (DIRECT URL LINK) and replace the text in the program. To get the DIRECT URL LINK log on to freedns.afraid.org.
and click the dynamic dns option on the left side and then click the direct url link on the bottom.
freedns2

copy the entire address-bar and paste it in the location of DIRECT URL LINK in the python code below. The picture shows a truancated image for security reasons.
freedns3

Step2: CREATE A PYTHON SCRIPT WITH THE COPIED DIRECT URL LINK
To do this create a python script file in cron.hourly folder using the following command
sudo nano /etc/cron.hourly/freedns
freedns01

copy the following python script
#****************the python code begins here


#!/usr/bin/python

import urllib2
import os.path

OLDIP_FILE = '/var/lib/misc/oldip'

def updatedns(ip):
    print urllib2.urlopen("http://REPLACE WITH THE DIRECT URL LINK ").read().strip()
    f = open(OLDIP_FILE, 'w')
    f.write(ip)
    f.close()

newip = urllib2.urlopen("http://ip.dnsexit.com/ ").read().strip()

if not os.path.exists(OLDIP_FILE):
    updatedns(newip)
else:
    f = open(OLDIP_FILE, 'r')
    oldip = f.read()
    f.close()
    if oldip != newip:
        updatedns(newip)

#******************the code ends here****

freedns1
and paste the correct url link in the DIRECT URL LINK location of the code.

Step3: MAKE THE SCRIPT EXECUTABLE AND FORWARD REQUIRED PORTS IN THE ROUTER.
Finally the python code needs to be marked as executable.
type sudo chmod +x /etc/cron.hourly/freedns
later add link of the freedns file to ~/ folder(home folder)
sudo ln /etc/cron.hourly/freedns ~/
freedns4

For those who like to use 3g data card with the pi, port forwarding is not needed, but for those who are using the pi over lan or wlan network port forwarding is required.
To access the pi from external world dont forget to forward the pirticular port address to the local static ip of the pi(only applicable for those who use wlan or eth0 via router or switch). For example port 22 for SSH, 1723 for PPTP VPN, 80 for HTTP etc.( To know how to set a local static ip for the pi from ubuntu refer to http://goo.gl/O7m0zq). To connect the pi with 3g data card to avoid portforwarding refer to http://goo.gl/w01Bdz.

Step4: MAKE THE SCRIPT JUST TO RUN EVERY 5 MINUTES FROM BOOT TIME
This step is optional but recommended as in my case I use 3g data card with my pi, the crontab executes the task assigned to it in previous step on hourly basis only. So i thought it would be better if its done every 5 minutes as one cant predict when the dynamic ip changes.
The script that needs to be executed is sudo /etc/cron.hourly/freedns. And this code should run right after a working network is dectected.To perform such an action is beyond the scope of this article. This code runs perfectly when entered manually, else I will have to wait for the first 1 hour for the code to self execute buy cron.hourly.

Before testing the working of actual command, i tried a dummy command (to see a response) like
sudo mkdir /home/pi/Desktop/test1.It makes this dummy file to find out which boot execution runs faster.
At first I pasted the dummy command in the /etc/rc.local , it worked as a new folder was created minutes after boot.

Later i tried the same dummy command in crontab based on based on directions from http://goo.gl/PUaS27. Type the command sudo crontab -e(to add command to crontab) and I added the line
@ reboot sudo mkdir /home/pi/Desktop/test1 & . The folder names test1 was dectected right after boot(very fast, no time delay).

If the code entered in crontab is faulty, there will be an error while saving. It said the following error.
no crontab for root – using an empty one
crontab: installing new crontab
“/tmp/crontab.hkY39J/crontab”:23: bad time specifier
errors in crontab file, can’t install.
Do you want to retry the same edit? (y/n)

To read the crontab type the command sudo crontab -l

As crontab does the work faster, probably faster that rc.local, and repeats itself at regular intervals I ask it to, i prefer to use crontab to preform the execution of the code.So type
sudo crontab -e
and insert the following command at the last line of the page
*/5 * * * * sudo /etc/cron.hourly/freedns
save the file and exit.
feedns8

To know more about the meaning of these values go to http://goo.gl/1QHXbi towards the second half of the blog.

And finally its up and running right within 5 minutes from boot provided it has a working network…

Advertisements
This entry was posted in Debian, Electronics, Linux OS, Networking, Raspberry pi, Tech, Ubuntu and tagged , , , , , , , , , , , , , , . Bookmark the permalink.

11 Responses to Dynamic DNS update client using python program for raspberry pi. PART1-(tutorial)

  1. Fantastic goods from you, man. I’ve understand your
    stuff previous to and you are just extremely great. I actually like what you have acquired here, really like what you’re saying and the
    way in which you say it. You make it enjoyable and you still care for to keep it smart.
    I can not wait to read much more from you.
    This is really a great web site.

  2. Admiring the time and energy you put into your blog and detailed information you
    offer. It’s awesome to come across a blog every once in a while that isn’t the same outdated rehashed material.
    Fantastic read! I’ve saved your site and I’m adding your
    RSS feeds to my Google account.

  3. Currentlky it soundfs likoe Expression Engine is the best blogging platform available right now.
    (from what I’ve read) Is that what you are using on your blog?

  4. wonderful publish, very informative. I’m wondering why the opposite experts of this sector don’t realize this.

    You must proceed your writing. I’m sure, you have
    a huge readers’ base already!

  5. I’m not sure exactly why but this weblog is loading very
    slow for me. Is anyone else having this problem or is it a problem on my
    end? I’ll check back later on and see if the problem still exists.

  6. you are a very intelligent truly amazing valuable information great articles

  7. Keesha says:

    You have to become prepared to buy things that are each on-sale which you have
    coupons for.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s