Re: External scripts/code for NetworkManagerDispatcher collection.



On Tue, 2006-10-24 at 14:09 +0200, Stefan Schmidt wrote:
> Collect scripts and programs whcih communicate with nm or
> NetworkManagerDispatcher. There are so many cases which we don't like
> to handle direct in the nm code, but with external scripts.

Here is another one:
- Updates your (public) IP address in DynDNS

-- 
Saikat
#!/usr/bin/env python
# Copyright (C) 2006 Saikat Guha <saikat cs cornell edu>
#
# This program is free software; you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free Software
# Foundation; either version 2 of the License, or any later version.

import dbus, socket, string, os
import httplib, urllib, base64
import pynotify

hostname = "change.this.dynalias.net"
username = "your_username"
password = "your_password"

def notify_dynupdate(error, host, ip = None):
    format = "<b>Host</b>: %(host)s\n<b>IP:</b> %(ip)s"
    text =  format % {"error" : "", "ip" : ip, "host" : host}
    if error is None:
        title = "DynDNS Updated"
    else:
        title = "DynDNS: %s" % error
    n = pynotify.Notification(title, text)
    n.show()

def get_error(resp, err = None):
    if resp == httplib.UNAUTHORIZED:
        return "Authorization failure"

def dynupdate(hostname, username, password):
    pynotify.init("hud-dyndns")
    conn = httplib.HTTPConnection("checkip.dyndns.org")
    conn.request("GET", "/")
    resp = conn.getresponse()
    if resp.status == httplib.OK:
        data = resp.read()
        ip = data.split(":")[1].split("<")[0].strip()
        conn = httplib.HTTPConnection("members.dyndns.org")
        params = urllib.urlencode({"system":"dyndns", "hostname":hostname, "myip":ip})
        headers = {"Authorization":"Basic "+base64.b64encode(username+":"+password),
            "User-Agent":"httplib/2.4",
            "Host":"members.dyndns.org",
            "Accept":"*/*"}
        conn.request("GET", "/nic/update?" + params, "", headers)
        resp = conn.getresponse()
        data = resp.read()
        result = data.split(" ")[0]
        if resp.status == httplib.OK:
            notify_dynupdate(None, hostname, ip)
        else:
            notify_dynupdate(get_error(resp.status), hostname, ip)
    else:
        notify_dynupdate("Cannot determine IP address", hostname, "unknown")

dynupdate(hostname, username, password)

Attachment: signature.asc
Description: This is a digitally signed message part



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]