private PDU sendRequestV1V2(PDU pdu, int version) throws Exception { PDU response; CommunityTarget target = new CommunityTarget(); target.setCommunity(new OctetString("public")); target.setAddress(new UdpAddress(m_agent.getInetAddress(), m_agent.getPort())); target.setVersion(version); if (m_timeout > 0) { target.setTimeout(m_timeout); } TransportMapping<UdpAddress> transport = null; try { transport = new DefaultUdpTransportMapping(); Snmp snmp = new Snmp(transport); transport.listen(); ResponseEvent e = snmp.send(pdu, target); response = e.getResponse(); } finally { if (transport != null) { transport.close(); } } return response; }
private PDU sendRequestV3(PDU pdu) throws IOException { PDU response; OctetString userId = new OctetString("opennmsUser"); OctetString pw = new OctetString("0p3nNMSv3"); UserTarget target = new UserTarget(); target.setSecurityLevel(SecurityLevel.AUTH_PRIV); target.setSecurityName(userId); target.setAddress(new UdpAddress(m_agent.getInetAddress(), m_agent.getPort())); target.setVersion(SnmpConstants.version3); if (m_timeout > 0) { target.setTimeout(m_timeout); } else { target.setTimeout(5000); } TransportMapping<UdpAddress> transport = null; try { USM usm = new USM(SecurityProtocols.getInstance(), new OctetString(MPv3.createLocalEngineID()), 0); SecurityModels.getInstance().addSecurityModel(usm); transport = new DefaultUdpTransportMapping(); Snmp snmp = new Snmp(transport); UsmUser user = new UsmUser(userId, AuthMD5.ID, pw, PrivDES.ID, pw); snmp.getUSM().addUser(userId, user); transport.listen(); ResponseEvent e = snmp.send(pdu, target); response = e.getResponse(); } finally { if (transport != null) { transport.close(); } } return response; }
@Before public void setUp() throws Exception { // Create a global USM that all client calls will use SNMP4JSettings.setEnterpriseID(5813); m_usm = new USM(SecurityProtocols.getInstance(), new OctetString(MPv3.createLocalEngineID()), 0); SecurityModels.getInstance().addSecurityModel(m_usm); try { m_agent = MockSnmpAgent.createAgentAndRun( classPathResource("penrose-lldp-mib.properties"), str(InetAddress.getLocalHost()) + "/0"); } catch (Throwable e) { m_agent = MockSnmpAgent.createAgentAndRun( classPathResource("penrose-lldp-mib.properties"), str(InetAddressUtils.ONE_TWENTY_SEVEN) + "/0"); } m_requestedVarbinds = new ArrayList<AnticipatedRequest>(); }
@Before public void setUp() throws Exception { // Create a global USM that all client calls will use MPv3.setEnterpriseID(5813); m_usm = new USM(SecurityProtocols.getInstance(), new OctetString(MPv3.createLocalEngineID()), 0); SecurityModels.getInstance().addSecurityModel(m_usm); m_agent = MockSnmpAgent.createAgentAndRun( classPathResource("penrose-lldp-mib.properties"), "127.0.0.1/1691"); m_requestedVarbinds = new ArrayList<AnticipatedRequest>(); }
@Test public void testGetNextMultipleVarbinds() throws Exception { request(".1.0.8802.1.1.2.1.3.1") .andExpect(".1.0.8802.1.1.2.1.3.1.0", SMIConstants.SYNTAX_INTEGER32, new Integer32(4)); doGetNext(); m_agent.getUsm().setEngineBoots(15); byte[] hexString = new byte[] {(byte) 0x80, (byte) 0x71, (byte) 0x1F, (byte) 0x8F, (byte) 0xAF, (byte) 0xC0}; request(".1.0.8802.1.1.2.1.3.1") .andExpect(".1.0.8802.1.1.2.1.3.1.0", SMIConstants.SYNTAX_INTEGER32, new Integer32(4)); request(".1.0.8802.1.1.2.1.3.2") .andExpect( ".1.0.8802.1.1.2.1.3.2.0", SMIConstants.SYNTAX_OCTET_STRING, new OctetString(hexString)); request(".1.0.8802.1.1.2.1.3.3") .andExpect( ".1.0.8802.1.1.2.1.3.3.0", SMIConstants.SYNTAX_OCTET_STRING, new OctetString("penrose-mx480".getBytes())); doGetNext(); // This statement breaks the internal state of the SNMP4J agent // m_agent.getUsm().setLocalEngine(m_agent.getUsm().getLocalEngineID(), 15, 200); m_agent.getUsm().removeEngineTime(m_usm.getLocalEngineID()); m_usm.removeEngineTime(m_agent.getUsm().getLocalEngineID()); request(".1.0.8802.1.1.2.1.3.1") .andExpect(".1.0.8802.1.1.2.1.3.1.0", SMIConstants.SYNTAX_INTEGER32, new Integer32(4)); doGetNext(); }
@After public void tearDown() throws Exception { if (m_agent != null) { m_agent.shutDownAndWait(); } }