nxShell and jython[Solved]
-
Hi to all!
I'm sorry if this is the wrong section, but I didn't know where I had to open this topic!
I'm starting using nxShell just how it is suggested in the wiki. I'm trying to create a script which will let me set a custom attribute (End User Info) on some interfaces in some map's nodes. My problem is the setting of the modification data. I understood that I should use a Map imported from java.util, but I'm new in python programming so I'm having problem in setting the Constructor of the Map in the right way. Once I run the script I get a "java.lang.ArrayIndexOutOfBoundsException: -3". Here is my code:
# coding: utf-8 import csv import time from java.util import Map from java.lang import String class XXX: def YYY(self): listaNodi = [] listaNodi = session.getAllObjects() for nodo in listaNodi: IPlista = nodo.getPrimaryIP() #print "IPlista: "+str(IPlista) for infoIP in open("RIGHT/PATH/IP.txt").readlines(): #print "infoIP: "+infoIP #time.sleep(0.2) if str(IPlista) == infoIP: for interface in nodo.getAllChilds(objects.GenericObject.OBJECT_INTERFACE): ID = interface.getObjectId() IDinterfaccia=str(ID) #print "IDinterfaccia: "+IDinterfaccia for interfaccia in open("RIGHT/PATH/INTERFACCIA.TXT").readlines(): #print "interfaccia: "+str(interfaccia) if IDinterfaccia == interfaccia: print "Sono in IDinterfaccia == interfaccia" info = open("RIGHT/PATH/EndUserInfo.txt").readlines() #print "info: "+str(info) modificatore = NXCObjectModificationData(ID) #print "modificatore: "+str(modificatore) Map<String,String> modifica = """new""" Map() # I SUPPOSE THE MISTAKE IS HERE!!! modifica.put("End user Info",info) #print "modifica: "+str(modifica) modificatore = setCustomAttributes(modifica) g = XXX() g.YYY()Could anyone help me to understand where is my mistake?
Thanks in advance! -
Try to change it like this:
# coding: utf-8 import csv import time from java.util import HashMap # CHANGE 1 from java.lang import String class XXX: def YYY(self): listaNodi = [] listaNodi = session.getAllObjects() for nodo in listaNodi: IPlista = nodo.getPrimaryIP() #print "IPlista: "+str(IPlista) for infoIP in open("RIGHT/PATH/IP.txt").readlines(): #print "infoIP: "+infoIP #time.sleep(0.2) if str(IPlista) == infoIP: for interface in nodo.getAllChilds(objects.GenericObject.OBJECT_INTERFACE): ID = interface.getObjectId() IDinterfaccia=str(ID) #print "IDinterfaccia: "+IDinterfaccia for interfaccia in open("RIGHT/PATH/INTERFACCIA.TXT").readlines(): #print "interfaccia: "+str(interfaccia) if IDinterfaccia == interfaccia: print "Sono in IDinterfaccia == interfaccia" info = open("RIGHT/PATH/EndUserInfo.txt").readlines() #print "info: "+str(info) modificatore = NXCObjectModificationData(ID) #print "modificatore: "+str(modificatore) modifica = HashMap() # CHANGE 2 modifica.put("End user Info",info) #print "modifica: "+str(modifica) modificatore = setCustomAttributes(modifica) g = XXX() g.YYY() -
Thank you Alex, but I'm still unable to set the custom attributes on the interface. I updated a little my script, because I have to read from a single file written in a certain way, and then I put your suggestions in it. The reading of the file works in the proper way and I don't get any error running the script, but the custom attributes are empty anyway. Here is my code:
#coding: utf-8 import csv import time from java.util import HashMap # CHANGE 1 from java.lang import String class XXX4: def YYY(self): listaNodi = [] listaNodi = session.getAllObjects() datiFile = open("PATH/Lettura.csv").readlines() for nodo in listaNodi: IPnodo = str(nodo.getPrimaryIP()) for riga in datiFile: print "___________starts for on riga_____________\n" print "riga: "+riga for parola in riga.split(): print "parola: "+parola if IPnodo ==parola: print "Verified IPnodo = parola" time.sleep(10) for interface in nodo.getAllChilds(objects.GenericObject.OBJECT_INTERFACE): print "starts interface for " interfaceName = interface.getObjectName() ID = interface.getObjectId() print "interfaceName: "+interfaceName for parola1 in riga.split(): print "parola1: "+parola1 if interfaceName == parola1: print "Verified interfaceName=parola1" for parola2 in riga.split(): EndUserInfo = parola2 print "EndUserInfo after for: "+EndUserInfo modificatore = NXCObjectModificationData(ID) modifica = HashMap() # CHANGE 2 modifica.put("End user Info",EndUserInfo) modificatore.setCustomAttributes(modifica) time.sleep(10) G = XXX4() G.YYY()Any suggestion?
EDIT: I found out something interesting. I modified the script and my last lines of it now are:
for parola2 in riga.split(): EndUserInfo = str(parola2) print "EndUserInfo dopo cilclo for: "+EndUserInfo modificatore = NXCObjectModificationData(ID) """modifica = HashMap() # CHANGE 2 modifica.put("End User Info",EndUserInfo) print "modifica: "+str(modifica) modificatore.setCustomAttributes(modifica)""" customAttributes = modificatore.getCustomAttributes() print "customAttributes: "+str(customAttributes) #customAttributes.put("End User Info",EndUserInfo) #modificatore.setCustomAttributes(customAttributes)My thought was that, instead of creating a new HasMap, I could get the existing one, supposed to be empty, and use the put metod on it. But the command
print "customAttributes: "+str(customAttributes)returns "none", which is the same result I get using the solution you suggested. Also, if I try to runcustomAttributes.put("End User Info",EndUserInfo)on the terminal I get:customAttributes.put("End User Info",EndUserInfo)
AttributeError: 'NoneType' object has no attribute 'put'It seems like modificatore.getCustomAttributes() is not a Map<String,String> object.
Help! -
After this suggestion by Victor for a similar task, I solved the problem also in this script. The working code is:
#coding: utf-8 import csv import time from java.util import HashMap # CHANGE 1 from java.lang import String class XXX4: def YYY(self): listaNodi = [] listaNodi = session.getAllObjects() datiFile = open("PATH/Lettura.csv").readlines() for nodo in listaNodi: IPnodo = str(nodo.getPrimaryIP()) for riga in datiFile: print "___________starts for on riga_____________\n" print "riga: "+riga for parola in riga.split(): print "parola: "+parola if IPnodo ==parola: print "Verified IPnodo = parola" time.sleep(10) for interface in nodo.getAllChilds(objects.GenericObject.OBJECT_INTERFACE): print "starts interface for " interfaceName = interface.getObjectName() ID = interface.getObjectId() print "interfaceName: "+interfaceName for parola1 in riga.split(): print "parola1: "+parola1 if interfaceName == parola1: print "Verified interfaceName=parola1" for parola2 in riga.split(): EndUserInfo = parola2 print "EndUserInfo after for: "+EndUserInfo modificatore = NXCObjectModificationData(ID) modifica = HashMap() # CHANGE 2 modifica.put("End user Info",EndUserInfo) modificatore.setCustomAttributes(modifica) session.modifyObject(modificatore) time.sleep(10) G = XXX4() G.YYY()
Hello! It looks like you're interested in this conversation, but you don't have an account yet.
Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.
With your input, this post could be even better 💗
Register Login