def __parsePortList(self, pl, tagged): raw_ports = array('B') for i in range(0, len(pl)): raw_ports.extend(unpack('B', pl[i])) mask = [128, 64, 32, 16, 8, 4, 2, 1, 0] port_number = 0 ports = [] port_ids = self.getPortIDs() port_names = self.getPortNames() for port in raw_ports: for slot in range(0, 8): port_number = port_number + 1 if (port & Switch.mask[slot] == Switch.mask[slot]): for port_id in port_ids: if (port_id[0][0][len(port_id[0][0]) - 1] == rfc1902.Integer32(port_number)): for port_name in port_names: if (port_name[0][0][len(port_name[0][0]) - 1] == port_id[0][1]): ports.append( Port(port_name[0][1], tagged, port_number)) return ports
def __parsePortList(self, pl, tagged): ports = [] raw_ports = array('B') #log.debug("port list in __parsePortList "+str(pl)) for i in range(0, len(pl)): ports = [] raw_ports.extend(unpack('B', pl[i])) mask = [128, 64, 32, 16, 8, 4, 2, 1, 0] port_number = 0 for port in raw_ports: for slot in range(0, 8): port_number = port_number + 1 if (port & Switch.mask[slot] == Switch.mask[slot]): ports.append(Port(port_number, tagged, port_number)) if (port_number == self.getNumberofPorts()): return ports return ports
def DELETE__getVLANPorts(self, vlanID): cmd = self.getSNMPgetCommand() + VLAN_PORTS_TABLE + "." + vlanID allPortsBitmap = self.getSNMPResultValueRemoveLF(cmd).replace(" ", "") # Get the IfIndex corresponding port number cmd = self.getSNMPwalkCommand() + PORT_IDS_TABLE macTableIfIndex = self.getSNMPResultTable(cmd) # Get the ifName (e.g. Fa0/2 cmd = self.getSNMPwalkCommand() + IFNAME_TABLE macTableIfName = self.getSNMPResultTable(cmd) # Process each set of 4 bits (one character in the string) at a time ports = [] for x in range(0, len(allPortsBitmap)): # The next loop runs 4 times, once for each of the 4 bits in the character # The mask takes on the values: 1000, 0100, 0010, 0001. The & of the character # and the mask will return 0 if the bit is zero, or 8, 4, 2, or 1 if the bit # is 1, so testing with != 0 will return a boolean for the bit's value mask = 0x8 for y in range(0, 4): bit = (convertHexCharacterToInt(allPortsBitmap[x]) & mask) != 0 mask = mask >> 1 if (bit): # The port number is the bit number (y + 1) plus the number of characters # of four bits that came before it (x * 4) portNumber = str((x * 4) + y + 1) # Get the IfIndexcorresponding port number ifIndex = macTableIfIndex[portNumber] # Get the ifName (e.g. Fa0/2) ifName = macTableIfName[ifIndex] # TODO Check the (un)tagged status of the port p = Port(ifName) ports.append(p) return ports
def getFullVLANInfo(self, theVLANName=None): """\brief Returns a list of VLAN objects consisting of all of the vlan in the switch. If the theVLANName parameter is set, the function returns a single VLAN object corresponding to the requested vlan \param theVLANName (\c string) The name of the VLAN to retrieve \return (\c list of VLAN objects) A list of VLAN objects with the requested information or a VLAN object if a vlan name is specified """ vlans = [] vlan_names = self.getVLANNames() port_ids = self.getPortIDs() # If only wanting information about one vlan, only get that information. if theVLANName != None: vn = {} for v in vlan_names.keys(): if vlan_names.get(v) == theVLANName: vn[v] = theVLANName vlan_names = vn # Create VLAN objects from vlan names for v in vlan_names: externalID = (self.snmp.get(OID.extremeVlanStaticExternalID + (v, )))[0][1] if self.snmp.getErrorStatus(): print "unable to get external id of vlan internal ID " + v vlans.append(VLAN(vlan_names[v], {}, None, v)) else: vlans.append(VLAN(vlan_names[v], {}, externalID, v)) for vlan in vlans: ifStackStatus = self.snmp.walk(OID.ifStackStatus) for ifStackStatusEntry in ifStackStatus: if ifStackStatusEntry[0][0][len(ifStackStatusEntry[0][0]) - 2] == vlan.getInternalID(): vlan.setTaggedID( ifStackStatusEntry[0][0][len(ifStackStatusEntry[0][0]) - 1]) epl = [] upl = [] switches = {} ports = [] remove = [] table2 = table = self.getVLANToInterfacesTable() for t in table: if t == vlan.getInternalID(): for tt in table[t]: for pi in port_ids: if pi[0][1] == rfc1902.Integer32(tt): ports.append( Port(pi[0][0][len(pi[0][0]) - 1], False, tt)) remove.append(tt) # remove found ports from the list for r in remove: table[t].remove(r) # ports left in table[t] at this point are tagged vlans for vt in table[t]: for ids in table2: if vt == ids: for pi in port_ids: if pi[0][1] == rfc1902.Integer32( table2[ids][0]): ports.append( Port(pi[0][0][len(pi[0][0]) - 1], True, table2[ids][0])) vlan.setTaggedID(vt) switches[self.getSwitchName()] = ports vlan.setSwitches(switches) return vlans
def getFullVLANInfo(self, theVLANName=None): """\brief Returns a list of VLAN objects consisting of all of the vlans\ in the switch. If the theVLANName parameter is set, the function returns a sin\gle VLAN object corresponding to the requested vlan \param theVLANName (\c string) The name of the VLAN to retrieve \return (\c list of VLAN objects) A list of VLAN objects with the reque\sted information or a VLAN object if a vlan name is specified """ vlans = [] vlan_names = self.getVLANNames() port_ids = self.getPortIDs() # If only wanting information about one vlan, only get that information. if theVLANName != None: vn = {} for v in vlan_names.keys(): if vlan_names.get(v) == theVLANName: vn[v] = theVLANName vlan_names = vn # Create VLAN objects from vlan names for v in vlan_names: vlans.append(VLAN(vlan_names[v], {}, v, v)) portGeneralAccessTable = self.snmp.walk(OID.netgearGeneralPortAccess) portVlanTaggedTypeTable = self.snmp.walk(OID.netgearVlanTaggedType) for vlan in vlans: ports = [] temp_ports = [] switches = {} for portGeneralAccessTableRow in portGeneralAccessTable: #print portGeneralAccessTableRow if (portGeneralAccessTableRow[0][1] == rfc1902.Integer32( vlan.getID())): vid = portGeneralAccessTableRow[0][1] pid = portGeneralAccessTableRow[0][0][ len(portGeneralAccessTableRow[0][0]) - 1] #print vlan.getID(),vid,pid temp_ports.append(Port(pid, False, pid)) for portVlanTaggedTypeRow in portVlanTaggedTypeTable: vid = portVlanTaggedTypeRow[0][0][ len(portVlanTaggedTypeRow[0][0]) - 1] pid = portVlanTaggedTypeRow[0][0][ len(portVlanTaggedTypeRow[0][0]) - 2] vlanType = portVlanTaggedTypeRow[0][1] #print portVlanTaggedVlanRow if (vid == rfc1902.Integer32(vlan.getID())): if (vlanType == rfc1902.Integer32(1)): temp_ports.append(Port(pid, False, pid)) elif (vlanType == rfc1902.Integer32(2)): temp_ports.append(Port(pid, True, pid)) else: print "unknown vlan type ", pid, vid, vlanType temp_ports.sort() # clean list if (len(temp_ports) > 0): ports.insert(0, temp_ports[0]) temp_ports.remove(ports[0]) while (len(temp_ports) > 0): if ports[0] == temp_ports[0]: temp_ports.remove(ports[0]) elif (ports[0].getInternalID() == temp_ports[0].getInternalID() and ports[0].getPortNumber() == temp_ports[0].getPortNumber()): if (ports[0].getTagged() == True and temp_ports[0].getTagged() == False): ports[0].setTagged(False) temp_ports.remove(temp_ports[0]) else: ports.insert(0, temp_ports[0]) temp_ports.remove(ports[0]) switches[self.getSwitchName()] = ports vlan.setSwitches(switches) return vlans
"Snmp exception getFullVLANInfo from switch" + str(switch_str)) if (vlan_str != None): # adding by vlan string if not self.__switchHasVlanByName(vlan_str, switch_str): self.__createNewVlanByName(vlan_str, switch_obj, switch_str) else: log.info("Adding vlan based on vlan id " + str(vlan_id) + " on switch " + str(switch_str)) if not self.__switchHasVlanById(vlan_id, switch_str): self.__createNewVlanById(vlan_id, switch_obj, switch_str) vlan_str = self.__getVlanNameFromId(switch_str, vlan_id) try: result = switch_obj.addPorts(vlan_str, [Port(port_str, False, port_str)]) except Exception, e: raise SNMPException("Snmp exception addPorts from switch" + str(switch_str)) if result == 0: # write to log file ? log.info("Added port " + str(port_str) + " to vlan " + str(vlan_str)) code = 200 reply = "success" else: log.critical("Failed to add port " + str(port_str) + " to vlan " + str(vlan_str)) code = 400 reply = "failure"
#! /usr/bin/env python from henmanager import HenManager from auxiliary.hen import Node, SwitchNode, VLAN, Port manager = HenManager() manager.initLogging() nodes = manager.getNodes("switch") theSwitch = None for node in nodes.values(): if (node.getNodeID() == "switch9"): theSwitch = node.getInstance() vlan = VLAN("test", None, "100") ports = [] port1 = Port("GigabitEthernet2/1/1", False) port2 = Port("GigabitEthernet2/1/2", True) ports.append(port1) ports.append(port2) vlan.addPorts(ports, "switch7") #table = theSwitch.getFullMACTable() #for entry in table: # print entry print theSwitch.createVLAN(vlan) #print theSwitch.deleteVLAN(vlan.getName()) #print theSwitch.getAvailableTaggedInterfaceInternalID() #print theSwitch.getNextVLANID() #print theSwitch.deleteVLAN("cd") #print theSwitch.addPorts("cd", ports) #print theSwitch.deletePorts("B", ports)