Python replacement for ez-ipupdate?
Question: I want to update my dynamic dns entry from behind a NAT, which ez-ipupdate
doesn't support. It uses the locally bound ip instead of the external ip
address.

My provider, easydns, only explicitly supports the ez-ipupdate solution on my
platform, Linux.

Instead of writing a python-based deamon to get the external IP address and
put it into the ez-ipupdate config file regularly, I wondered if there was a
way to replace the whole thing with a python script. Maybe it would simplify
things.

(I could not find any information about this on google, so I'm asking and
answering this question here in order to help others.)


Answer: It would simplify things indeed. At least for easydns, ez-ipupdate only really
performs a simple GET request with Basic HTTP Authentication.

The below code is a starting point. It's been tested, it works. It needs the
`requests` and `ipgetter` modules from pypi.

    
    
    import time
    import ipgetter
    import requests
    import datetime
    from requests.auth import HTTPBasicAuth
    
    def update(user, auth_token, hostname, partner="easydns", cache_fn=None):
        if cache_fn is None:
            cache_fn = "/var/cache/ez-ipupdate/default-cache"
    
        my_ip = ipgetter.myip()
    
        with open(cache_fn) as fobj:
            secs, ip = fobj.read().strip().split(",")
        if ip == my_ip:
            return "IP doesn't need updating"
    
        last_update = datetime.datetime.fromtimestamp(int(secs))
        diff = datetime.datetime.now() - last_update
        minutes_since_last_update = diff.total_seconds() / 60.0
        if minutes_since_last_update < 4.99:
            return "Too short time since last update..."
    
        with open(cache_fn, "wb") as fobj:
            fobj.write("{},{}\n".format(int(time.time()), my_ip))
    
        url = (
           'https://api.cp.easydns.com/dyn/ez-ipupdate.php?action=edit'
           '&myip={address}&partner={partner}&wildcard=OFF&hostname={host}'
           ).format(address=my_ip, partner=partner, host=hostname)
    
        r = requests.get(url, auth=HTTPBasicAuth(user, auth_token))
        return "{} {}".format(r.status_code, r.reason)
    

Now just run a script calling the update functions regularly, e.g. using
`crontab -e` and adding this line:

    
    
    */5 * * * * /path/to/script.py
    

