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

I had few technical troubles while proceeding with the previous program and its execution technique mentioned http://goo.gl/z8mYGk. Firstly my raspbian OS began to get more laggy due to lots of trial&error methods I did while testing and developing the first 6 articles mentioned in http://goo.gl/cFh1Ly. Secondly my pi had lot of programs installed and updated, which I thought might be a problem for starters without proper direction. And thirdly /var/log/syslog had lot of unwanted logs being recorded apart from the useful cron stats / cron logs.

So I installed a new fresh OS using the method mentioned in http://goo.gl/4bNkyW.
And ssh into my pi using Ubuntu pc as mentioned in http://goo.gl/6mh2cr

At first I entered sudo raspi-config –Change timezone — ASIA –KOLKATTA(INDIA) as I wanted my pi to create log files with present date. Then I ran an update command using sudo apt-get update. Then I started with my experiment on creating a smaller + user-friendly python program to make it easy for testing, as previous method could only be tested by changing IP by reconnecting to network.

STEP1: MAKE NECESSARY FOLDERS
Then I made few new folders using command
sudo mkdir /home/pi/ddns
sudo mkdir /home/pi/ddns/log # to store detailed log file
sudo mkdir /home/pi/ddns/backup-files # to keep program backup at each step

STEP2: CODE THE PYTHON PROGRAM
Then the following command was typed sudo nano /home/pi/ddns/fdupdater.py
The python programs are given below, use the one that suits ur need based on , all was found working upon test. Directions for how get the key from freedns.afraid.org is available at http://goo.gl/z8mYGk

FreeDNS updater program V1:fdupdater.py
WHATS NEW? Just updates the ip with no log file and no viewable response

#!/usb/bin/python
from urllib import urlopen
urlopen("http://freedns.afraid.org/dynamic/update.php?***your key here**********=")

oops!! no log , no sample image

FreeDNS updater program V2:fdupdater.py
WHATS NEW? prints the status message from the website to a specified file in a specified path. refer to http://goo.gl/YRDljO for info on advanced file handling in python

#!/usb/bin/python
import os.path
from urllib import urlopen
f=open('/home/pi/ddns/log/log1','a')
data2=urlopen("http://freedns.afraid.org/dynamic/update.php?***your key here**********=").read()
f.write(data2)
f.close

log1

FreeDNS updater program V3:fdupdater.py
WHATS NEW? prints time and then status message from freedns.afraid.org in a log file

#!/usb/bin/python
import os.path
from urllib import urlopen
import datetime
data2=urlopen("http://freedns.afraid.org/dynamic/update.php?*********your key here").read()
f=open('/home/pi/ddns/log/log3','a')
f.write(datetime.datetime.now().ctime())
f.write(' ')
f.write(data2)
f.close

log3

FreeDNS updater program V4:fdupdater.py
WHATS NEW? prints time| status message| ip address at that time to a log file

#!/usb/bin/python
import os.path
from urllib import urlopen
import datetime
data=urlopen("http://ip.dnsexit.com").read()
data2=urlopen("http://freedns.afraid.org/dynamic/update.php?****your key here****").read().split()
f=open('/home/pi/ddns/log/log4','a')
f.write(datetime.datetime.now().ctime())
f.write(' |Update-Status=')
f.write(data2[0])
f.write(' ')
f.write('|Now IP= ')
f.write(data)
f.close

log4

testing finally

STEP3: ADDING THE FILE EXECUTION TO CRON LIST
Type the command crontab -e

and enter any one of the following text in the last line and save the file and exit.
(to run every 1 minute) */1 * * * * sudo python /home/pi/ddns/fdupdater.py
(to run every 2 minute) */2 * * * * sudo python /home/pi/ddns/fdupdater.py
(to run every 10 minutes) */10 * * * * sudo python /home/pi/ddns/fdupdater.py
(to run every 20 minutes) */20 * * * * sudo python /home/pi/ddns/fdupdater.py
(to run every 30 minutes) */30 * * * * sudo python /home/pi/ddns/fdupdater.py
(to run at 5th 10th 30th and 50th minute) 5,10,30,50 * * * * sudo python /home/pi/ddns/fdupdater.py

crontab
before you add any one of the above to your cron list, read more about these confiurations to select the one that you will need.
For more info about crontab goto http://en.wikipedia.org/wiki/Cron and http://www.pantz.org/software/cron/croninfo.html

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

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

  1. Anonymous says:

    Does your script make updates only when a IP change occurs? Because if not, you can simply use this in cronjob and you are done, this one updates every 4 hours. I don’t need a shorter update cycle since I don’t know any ISP that updates with a high frequency:
    0 */4 * * * sleep 32 ; wget -O – http://freedns.afraid.org/dynamic/update.php?YOURIDHERE >> /tmp/freedn.log 2>&1 &

    Got it from http://freedns.afraid.org/dynamic/index.php?action=script&method=example&data_id=10485466

  2. zilexa says:

    Does your script make updates only when a IP change occurs? That would be best. If not, this is a much more simple solution:
    0 */4 * * * sleep 32 ; wget -O – http://freedns.afraid.org/dynamic/update.php?YOURIDHERE >> /tmp/ddns.log 2>&1 &

    This updates every 4 hours, I don’t need a higher update frequency because I do not know any ISP with a higher update frequency so it would be useless.

    • lorentz says:

      u are rite, isp don’t update them every now an then. the crontab timing will help u change the update intervals.
      i just run it every 5 minutes coz my 3g internet gets disconnected every now and then due to low range. so after reconnection i get a new ip.

  3. zilexa says:

    edit: only update when IP address is changed:
    http://freedns.afraid.org/scripts/dnsactual.sh.txt

    But if your script does the same, I would use yours as it looks simplified, I just dont understand your code (I am not a programmer) and I do understand this one, since it clearly states “Update not required…” somewhere in the script.

    • lorentz says:

      glad u liked. my script open the link at regular intervals. the server at other end automatically updates our ip at that time. there are alternatives that will update only if ip is changes, those methods use an extra webserver to find out our ip. i have mentiond that method also in the first post

  4. Matt says:

    Thanks Lawrence!
    Great tutorial and script and seems to be working fine.
    My only question is, does the script check current IP against history and only update if different?

    • lorentz says:

      No.. actually opening the link automatically updates ip.. this is easier when compared to ip changer script .. the ip changer script needs more processing.. nway u can adjust the timing in crontab to make the timing longer.. the 5 minutes is only for demonstration only..

  5. VBJblS says:

    More Bonuses tramadol overdose vomiting – tramadol 50mg for dogs

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