📄 snmptarget.java
字号:
* * @param node The DOM node */public ListeningContextPool createListeningContext(Node node) throws IOException{ ListeningContextPool tContext = null; int portNo = getPort(node, ListeningContextFace.DEFAULT_TRAP_PORT); String bindAddr = Util.getCDataValue(node, BIND); String typeSocket = getSocketType(node); tContext = new ListeningContextPool(portNo, bindAddr, typeSocket); return tContext;}/** * Returns the snmp context specified in node. * * @param node The DOM node */public SnmpContextBasisFace createContext(Node node) throws IOException{ SnmpContextBasisFace cContext = null; Element versionNode = (Element) Util.getChildNode(node, VERSION); String no = "1"; if (versionNode != null) { no = versionNode.getAttribute(VNO); } String host = Util.getCDataValue(node, HOST); int portNo = getPort(node, SnmpContextBasisFace.DEFAULT_PORT); String bindAddr = Util.getCDataValue(node, BIND); String typeSocket = getSocketType(node); if (SNMPv2c.equals(no)) { SnmpContextv2c c = new SnmpContextv2c(host, portNo, bindAddr, typeSocket); String comm = Util.getCDataValue(node, COMMUNITY); c.setCommunity(comm); cContext = c; } else if (SNMPv3.equals(no)) { SnmpContextv3 c = new SnmpContextv3(host, portNo, bindAddr, typeSocket); setUsm(c, node); cContext = c; } else { SnmpContext c = new SnmpContext(host, portNo, bindAddr, typeSocket); String comm = Util.getCDataValue(node, COMMUNITY); c.setCommunity(comm); cContext = c; } return cContext;}/** * Returns the socket type specified in node. * * @param node The DOM node */public String getSocketType(Node node){ Element typeNode = (Element) Util.getChildNode(node, SOCKET_TYPE); String type = STD; if (typeNode != null) { type = typeNode.getAttribute(TYPE); } String typeSocket = SnmpContextBasisFace.STANDARD_SOCKET; if (TCP.equals(type)) { typeSocket = SnmpContextBasisFace.TCP_SOCKET; } return typeSocket;}/** * Returns the port number specified in node. * * @param node The DOM node * @param defValue The default value */public int getPort(Node node, int defValue){ String port = Util.getCDataValue(node, PORT); int portNo = defValue; try { portNo = Integer.parseInt(port); } catch (NumberFormatException exc) { } return portNo;}/** * Sets the USM properties in the context according to node. * * @param c The SNMPv3 context * @param node The DOM node */public void setUsm(SnmpContextv3Face c, Node node){ Element usmNode = (Element) Util.getChildNode(node, USM); if (usmNode != null) { String userName = Util.getCDataValue(usmNode, USERNAME); c.setUserName(userName); Element contextNode = (Element) Util.getChildNode(usmNode, CONTEXT); if (contextNode != null) { String contextId = Util.getCDataValue(contextNode, ID); String contextName = Util.getCDataValue(contextNode, NAME); if (contextId != null) { byte [] bytes = SnmpUtilities.toBytes(contextId.trim()); c.setContextEngineId(bytes); } if (contextName != null) { c.setContextName(contextName.trim()); } } Element authNode = (Element) Util.getChildNode(usmNode, AUTH); if (authNode != null) { String doAuth = authNode.getAttribute(ADO); if (YES.equals(doAuth)) { String authProto = Util.getCDataValue(authNode, APROTO); String authPassw = Util.getCDataValue(authNode, APASSW); c.setUseAuthentication(true); if (authProto != null && authProto.charAt(0) == 'S') { c.setAuthenticationProtocol(SnmpContextv3Face.SHA1_PROTOCOL); } else { c.setAuthenticationProtocol(SnmpContextv3Face.MD5_PROTOCOL); } c.setUserAuthenticationPassword(authPassw); } else { c.setUseAuthentication(false); } } Element privNode = (Element) Util.getChildNode(usmNode, PRIV); if (privNode != null) { String doPriv = privNode.getAttribute(PDO); if (YES.equals(doPriv)) { String privPassw = Util.getCDataValue(privNode, PPASSW); c.setUsePrivacy(true); c.setUserPrivacyPassword(privPassw); } else { c.setUsePrivacy(false); } } }}/** * Returns the string representation of the current context. */public String toString(){ return _context.toString();}/** * Fires a property change listener when all the requests have been * answered. */protected void tellThemWeAreReady(){ if (_nrRequests == 0) { _propertyChangeListeners.firePropertyChange("Finished", null, null); }} /** * Adds a property change listener. * * @param l The listener */public void addPropertyChangeListener(PropertyChangeListener l) { _propertyChangeListeners.addPropertyChangeListener(l);}/** * Removes a property change listener. * * @param l The listener */public void removePropertyChangeListener(PropertyChangeListener l) { _propertyChangeListeners.removePropertyChangeListener(l);}/** * The class One will handle the response of one request. */class One implements Observer{ public void update(Observable obs, Object ov) { Vector message = new Vector(5); Pdu pdu = (Pdu) obs; message.addElement("One.update(): " + pdu.toString()); if (pdu.getErrorStatus() == AsnObject.SNMP_ERR_NOERROR) { try { varbind [] vars = pdu.getResponseVarbinds(); int l = vars.length; for (int i=0; i<l; i++) { varbind var = vars[i]; message.addElement(var.toString()); } } catch (uk.co.westhawk.snmp.stack.PduException exc) { message.addElement("One.update(): PduException " + exc.getMessage()); } } else { message.addElement("One.update(): " + pdu.getErrorStatusString()); } int sz = message.size(); synchronized (_writer) { _writer.println(""); for (int i=0; i<sz; i++) { String str = (String) message.elementAt(i); _writer.println(str); } } _nrRequests --; tellThemWeAreReady(); }} // end class One/** * The class Walk will handle the MIB walk. */class Walk implements Observer{ boolean _first = true; public Walk() { _first = true; } public void update(Observable obs, Object ov) { boolean finished = true; Pdu pdu = (Pdu) obs; Vector message = new Vector(5); if (_first == true) { message.addElement(""); message.addElement("Walk.update(): " + pdu.toString()); _first = false; } if (pdu.getErrorStatus() == AsnObject.SNMP_ERR_NOERROR) { try { boolean anyEndOfMibs = false; varbind [] vars = pdu.getResponseVarbinds(); int l = vars.length; pdu = new GetNextPdu_vec(_context, l); for (int i=0; i<l; i++) { varbind var = vars[i]; message.addElement(var.toString()); AsnObject obj = var.getValue(); anyEndOfMibs = (anyEndOfMibs || obj.getRespType() == AsnObject.SNMP_VAR_ENDOFMIBVIEW); pdu.addOid(var.getOid().toString()); } if (l>0 && anyEndOfMibs == false) { pdu.addObserver(this); pdu.send(); finished = false; } } catch(java.io.IOException exc) { message.addElement("Walk.update(): " + pdu.toString()); message.addElement("Walk.update(): IOException " + exc.getMessage()); } catch (uk.co.westhawk.snmp.stack.PduException exc) { message.addElement("Walk.update(): " + pdu.toString()); message.addElement("Walk.update(): PduException " + exc.getMessage()); } } else { message.addElement("Walk.update(): " + pdu.toString()); message.addElement("Walk.update(): " + pdu.getErrorStatusString()); } int sz = message.size(); synchronized (_writer) { for (int i=0; i<sz; i++) { String str = (String) message.elementAt(i); _writer.println(str); } } if (finished == true) { _nrRequests --; tellThemWeAreReady(); } }} // end class Walk} // end class SnmpTarget
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -