RasPi Series {5} – Configuring Multiple DynDNS entries with Python

Because the FritzBox is not able to manage multiple DynDNS entries, I had to write a script that does this task.

    Inhaltsangabe
  1. What is Python 3?
  2. The Python 3 Script
  3. Upload and run a script on the Raspberry Pi

In the last post we learned what a Dynamic DNS is and what it’s good for. We have seen examples where a Dynamic DNS can be used and how much it supports us in our everyday lives.

Now let’s look at a script I wrote in Python 3 to direct all my subdomains linked to a DynDNS to my IP address.

What is Python 3?

Python is a high-level programming language , which is very popular because of its possibilities and its performance. Almost everything is possible with it and it is also used in many areas.

Python is also used in the programming of artificial intelligence. The largest companies on the Internet also use this programming language for various processes. For example

  1. Google
  2. Facebook
  3. Instagram
  4. Spotify
  5. Netflix
  6. etc.

Unfortunately, I can not give you a crash course on Python, because the language is very complicated and I myself am learning it still since a year.

So that you can still understand what I do in the code, I will comment on the lines of code.

The Python 3 Script

# Import of the libraries
import urllib.request
import socket

# The Domains and the access data are saved in a variable
domains = [
{
    "domain" : "domain1.talhasariyuerek.com",
    "username" : "MyUser1",
    "password" : "MyPassword1"
},
{
    "domain" : "domain2.talhasariyuerek.com",
    "username" : "MyUser2",
    "password" : "MyPassword2"
},
{
    "domain" : "domain3.talhasariyuerek.com",
    "username" : "MyUser3",
    "password" : "MyPassword3"
},
]

# Getting current ip address
current_ip = urllib.request.urlopen('https://api.ipify.org').read().decode('utf8')

# Declaring the function wich sets the new ip address
def set_new_ip(domain):
  # Declaring the http athenticator
  password_mgr = urllib.request.HTTPPasswordMgrWithDefaultRealm()
  # The final url is saved in a variable
  top_level_url = "https://dyndns.kasserver.com/?myip=" + current_ip
  # The password is passed to the http authenticator
  password_mgr.add_password(None, top_level_url, domain['username'], domain['password'])
  # Declaring the url opener
  opener = urllib.request.build_opener(urllib.request.HTTPBasicAuthHandler(password_mgr))
  # Opening the url
  opener.open(top_level_url)

# Querying all the given domains and their access informations
for domain in domains:
  try:
    # Detecting the domain ip address
    domain_ip = socket.gethostbyname(domain['domain'])

    # Checking if the ip address of the domain is different than the own ip address
    if current_ip != domain_ip:
      print( domain['domain'] + " has another IP(" + domain_ip + ")! Your IP is: " + current_ip + " Changing!!" )
      # If the ip Address is different, the function for the ip address change is executed
      set_new_ip(domain)

  except Exception as e:
    # If the ip address of the domain could not be checked the update is also executed
    set_new_ip(domain)

The variable where the domains and, usernames and passwords are stored can be customized and added to your provider.

Upload and run a script on the Raspberry Pi

Saves this script under the name dyndns.py. To upload it, open MobaXterm and connect to your Raspberry Pi.

Red: Show / hide sidebar
Blue: create folder
Green: folder content

You see what is shown in the screenshot above. If the sidebar is not displayed, click on SFTP on the left side (marked red in the screenshot) and then the sidebar will be displayed.

Then you create a folder and name it Cron . You can create a folder by clicking on the button (marked in blue).

After you have created a folder, you double-click on the folder and drag your file in there . The file is uploaded. To test the file, enter the following in the command line:

cd Cron && python3 dyndns.py

This will execute this Python script and if no error message appears, you have done everything right. Congratulations!

In the later tutorials, I’ll talk about how to automatically call this script and other scripts on your Raspberry Pi at specific intervals. In this case, this will serve to ensure that the IP address of the Raspberry Pi will always be accessible via your domain.

Topics

Cloud DIY DNS DynDNS python Raspberry Pi

Beitrag teilen

WhatsApp it

Folgen Sie uns