nxShell: setPrimaryName doesn't work [Solved]

Started by BlackMamba, June 13, 2013, 01:05:48 PM

Previous topic - Next topic

BlackMamba

Hi to all!

I'm trying to create a script for nxShell which will allow me to set the primary name of some hosts in my network. Both primary name to set and hostnames to be modified are stored in two files. I created this script:

# coding: utf-8

class SetPrimaryName:
def out(self):
i=0
PrimaryNames = open("/PATH/TO/FILE/PRIMARYNAME").readlines()
for nome in open("/PATH/TO/FILE/HOSTNAMESTOMODIFY").readlines():
nodo = session.findObjectByName(nome.strip())
primaryName = PrimaryNames[i]
if nodo:
nodeID =nodo.getObjectId()
print "To node %s with id %d I want to set Primary Name %s"%(nome.strip(), nodeID, primaryName)
modificatore = NXCObjectModificationData(nodeID)
print
modificatore.setPrimaryName(primaryName)
i+=1
spn = SetPrimaryName()
spn.out()


Everything works fine but nothing happens in NetXMS and the primary name of the selected hosts stays the same. Am I doing something wrong? My doubt is focused on "modificatore", because it's the second time I try to modify data already stored in NetXMS (does anyone remember my still unresolved problem posted here? ) but I'm unable to accomplish this goal.
Thanks in advance for the support!

Victor Kirhenshtein

Hi!

It seems that you don't do real modification. Creating NXCObjectModificationData is not enough - after populating it you should call session.modifyObject with your NXCObjectModificationData object as argument. Should be something like

session.modifyObject(modificatore)

in your case.

Best regards,
Victor

BlackMamba

Thank you Victor! With one answer you solved two problems! Just in case anyone has the same problem I'll post the working code as example of both script:


# coding: utf-8

class SetPrimaryName:
def out(self):
i=0
PrimaryNames = open("PATH/TO/FILE/PRIMARYNAME.TXT").readlines()
for nome in open("/PATH/TO/FILE/HOSTNAMESTOMODIFY.TXT").readlines():
nodo = session.findObjectByName(nome.strip())
primaryName = PrimaryNames[i]
if nodo:
nodeID =nodo.getObjectId()
print "To node %s with id %d I want to set Primary Name %s"%(nome.strip(), nodeID, primaryName)
modificatore = NXCObjectModificationData(nodeID)
modificatore.setPrimaryName(primaryName)
session.modifyObject(modificatore)
i+=1
spn = SetPrimaryName()
spn.out()


If you want to set custom attributes in already existing nodes, or if you want another example, you can find the script here.