from transaction import commit
from Products.ZenUtils.ZenScriptBase import ZenScriptBase
from xmlrpclib import ServerProxy, ProtocolError
dmd = ZenScriptBase(connect=True).dmd

baseUrl = 'http://*****:*****@localhost:8080'

for d in dmd.Devices.getSubDevices():
    if d.titleOrId():
        try:
            oldname = d.id
            devicename = re.search( r'(10.23.16.2).*', oldname)

            if devicename.group(1) == "10.23.16.2" :
                print 'found:', devicename.group(1) ## 'found word:cat'
                newname = d.titleOrId()
                print 'newname:', newname
                if newname != oldname:
                    devpath = d.getPrimaryUrlPath()
                    print "Renaming %s to %s" % (oldname, newname)
                    url = '/'.join([baseUrl, devpath])
                    serv = ServerProxy( url )
                    serv.renameDevice(newname)
                    sync()
                    commit()
                    sync()
                else:
                    print 'did not find', result.group(1)
        except:
            pass