Find switch ports in script

Started by greensmith, May 06, 2014, 04:46:43 PM

Previous topic - Next topic

greensmith

Hello,

I have been attempting to use nxshell and a script to iterate through each object in my network and tell me switch port information.

However I am stuck at using the ConnectionPoint class and findConnectionPoint.
https://www.netxms.org/documentation/javadoc/latest/org/netxms/client/topology/class-use/ConnectionPoint.html


import org.netxms.client

for node in [o for o in s.getAllObjects() if isinstance(o, objects.Node)]:
NXCSession.findConnectionPoint(node.ZONE0, node.getPrimaryIP())


the code errors out with the following TypeError: findConnectionPoint(): self arg can't be coerced to org.netxms.client
.NXCSession

Trying to use macAddr or just ObjectId complains that it requires 2-3 arguments rather than just 1.

Has anyone else successfully used findConnectionPoint or have any tips to point me in the right direction?

Alex Kirhenshtein

You are trying to call static method — code should be like this:

for node in [o for o in s.getAllObjects() if isinstance(o, objects.Node)]:
point = s.findConnectionPoint(node.ZONE0, node.getPrimaryIP())
print node.getObjectName(), point


And you don't need to import org.netxms.client, it's done automatically by nxshell.
from org.netxms.client import *
from org.netxms.api.client import *

greensmith

Thanks Alex, that works :)


for anyone else interested in this, here is the code i used.

it checks the ip address of each node - if it matches regex (i am only interested in nodes from one subnet) it prints the data in csv format...
NAME,IP,MAC,SWITCH,PORT,DIRECTLYCONNECTED




import re
import csv
import sys

w = csv.writer(sys.stdout, dialect='excel')
w.writerow(['NAME', 'IP', 'MAC', 'SWT', 'PORT', 'DIR'])

for node in [o for o in s.getAllObjects() if isinstance(o, objects.Node)]:
#set node information
#convert ip to string and remove slash
vnodeIp = str(node.getPrimaryIP())
vnodeIp = vnodeIp.replace("/","")
vnodeName = node.getObjectName()
vnodeMac = node.getPrimaryMAC()

#regex to match anything in .2. subnet
subnetRegex = re.match("^\d{1,3}\.\d{1,3}\.2\.\d{1,3}$",vnodeIp)

#if in this subnet run...
if subnetRegex:

#get connectionpoint
point = s.findConnectionPoint(node.ZONE0, node.getPrimaryIP())

#handle connectionpoint data exceptions
try:
vswitchID = s.getObjectName(point.nodeId)
except:
vswitchID = "null"
try:
vportID = s.getObjectName(point.interfaceId)
except:
vportID = "null"
try:
vdirCon = point.directlyConnected
except:
vdirCon = "False"

#print data
w.writerow([
vnodeName,
vnodeIp,
vnodeMac,
vswitchID,
vportID,
vdirCon
])



Again, thanks for the help!