本文整理汇总了Java中org.snmp4j.Snmp.listen方法的典型用法代码示例。如果您正苦于以下问题:Java Snmp.listen方法的具体用法?Java Snmp.listen怎么用?Java Snmp.listen使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.snmp4j.Snmp
的用法示例。
在下文中一共展示了Snmp.listen方法的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: setUpTarget
import org.snmp4j.Snmp; //导入方法依赖的package包/类
/**
* Helper method that initializes the snmp target to listening mode. This
* method is explicitly for V1 and V2 messages. This method will create and
* set up the TransportMapping and target for SNMP V1 and V2. It creates a
* TransportMapping and puts it in the listening mode. Also Creates
* CommunityTarget object and sets SNMP target properties.
*
* @param communityName
* Community name of the target
* @param targetIP
* IP address of Target machine
* @param portNumber
* Port number
* @return The Target created
* @throws IOException
* IOException
*/
private Target setUpTarget( final String communityName, final String targetIP, final int portNumber )
throws IOException
{
final InetAddress inetAddress = InetAddress.getByName( targetIP );
final Address address = new UdpAddress( inetAddress, portNumber );
final OctetString community = new OctetString( communityName );
final TransportMapping transport = new DefaultUdpTransportMapping();
snmp = new Snmp( transport );
snmp.listen();
// Creating the communityTarget object and setting its properties
final CommunityTarget communityTarget = new CommunityTarget();
communityTarget.setCommunity( community );
// TODO Needs to check also for v2 messages
communityTarget.setVersion( SnmpConstants.version1 );
communityTarget.setAddress( address );
// TODO Need to confirm, whether this value needs to be configures
communityTarget.setRetries( SnmpManager.DEFAULT_RETRIES );
// TODO Need to confirm, whether this value needs to be configures
communityTarget.setTimeout( SnmpManager.DEFAULT_TIMEOUT );
return communityTarget;
}
示例2: main
import org.snmp4j.Snmp; //导入方法依赖的package包/类
public static void main(String[] args) throws IOException, InterruptedException {
Snmp snmp = new Snmp(new DefaultUdpTransportMapping());
snmp.listen();
CommunityTarget target = new CommunityTarget();
target.setCommunity(new OctetString("public"));
target.setVersion(SnmpConstants.version2c);
target.setAddress(new UdpAddress("23.23.52.11/161"));
target.setTimeout(3000); //3s
target.setRetries(1);
PDU pdu = new PDU();
pdu.setType(PDU.GETBULK);
pdu.setMaxRepetitions(1);
pdu.setNonRepeaters(0);
VariableBinding[] array = {new VariableBinding(new OID("1.3.6.1.4.1.2000.1.2.5.1.3")),
new VariableBinding(new OID("1.3.6.1.4.1.2000.1.3.1.1.7")),
new VariableBinding(new OID("1.3.6.1.4.1.2000.1.3.1.1.10")),
new VariableBinding(new OID("1.3.6.1.4.1.2000.1.2.5.1.19"))};
pdu.addAll(array);
//pdu.add(new VariableBinding(new OID("1.3.6.1.4.1.2000.1.2.5.1.3")));
ResponseEvent responseEvent = snmp.send(pdu, target);
PDU response = responseEvent.getResponse();
if (response == null) {
System.out.println("TimeOut...");
} else {
if (response.getErrorStatus() == PDU.noError) {
Vector<? extends VariableBinding> vbs = response.getVariableBindings();
for (VariableBinding vb : vbs) {
System.out.println(vb.getVariable().toString());
}
} else {
System.out.println("Error:" + response.getErrorStatusText());
}
}
}
示例3: start
import org.snmp4j.Snmp; //导入方法依赖的package包/类
@Override
public void start() {
// Initialize the connection to the external client
try {
snmp = new Snmp(new DefaultUdpTransportMapping());
snmp.listen();
target = new CommunityTarget();
target.setCommunity(new OctetString("public"));
target.setVersion(SnmpConstants.version2c);
target.setAddress(new UdpAddress(bindAddress + "/" + bindPort));
target.setTimeout(3000); //3s
target.setRetries(1);
pdu.setType(PDU.GETBULK);
pdu.setMaxRepetitions(1);
pdu.setNonRepeaters(0);
} catch (IOException ex) {
//
}
super.start();
}
示例4: startUp
import org.snmp4j.Snmp; //导入方法依赖的package包/类
@Override
public void startUp() throws IOException {
log.info("Snmp Trap Receiver Start");
log.info("listened on " + Configure.getInstance().getUdpTrapIpPort());
ThreadPool pool = ThreadPool.create(Const.THREAD_POOL_NAME, Const.AGENT_THREAD_NUM);
MultiThreadedMessageDispatcher dispatcher = new MultiThreadedMessageDispatcher(pool, new MessageDispatcherImpl());
Address listenAddress = GenericAddress.parse(Configure.getInstance().getUdpTrapIpPort());
TransportMapping transport = new DefaultUdpTransportMapping((UdpAddress) listenAddress);
// ����SNMP������ʹ�俪ʼ����
Snmp snmp = new Snmp(dispatcher, transport);
snmp.getMessageDispatcher().addMessageProcessingModel(new MPv2c());
snmp.listen();
snmp.addCommandResponder(new CommandResponderImpl());
}
示例5: init
import org.snmp4j.Snmp; //导入方法依赖的package包/类
private void init() throws UnknownHostException, IOException {
threadPool = ThreadPool.create("Trap", 4);
dispatcher = new MultiThreadedMessageDispatcher(threadPool,
new MessageDispatcherImpl());
listenAddress = GenericAddress.parse("udp:0.0.0.0/"
+ SnmpPref.getTrapsPort());
DefaultUdpTransportMapping transport = new DefaultUdpTransportMapping(
(UdpAddress) listenAddress);
snmp = new Snmp(dispatcher, transport);
snmp.getMessageDispatcher().addMessageProcessingModel(new MPv1());
snmp.getMessageDispatcher().addMessageProcessingModel(new MPv2c());
snmp.getMessageDispatcher().addMessageProcessingModel(new MPv3());
USM usm = new USM(SecurityProtocols.getInstance(), new OctetString(
MPv3.createLocalEngineID()), 0);
SecurityModels.getInstance().addSecurityModel(usm);
snmp.listen();
logger.debug("Listening for traps on "
+ transport.getListenAddress().toString());
}
示例6: testTrapReceiverWithoutOpenNMS
import org.snmp4j.Snmp; //导入方法依赖的package包/类
public void testTrapReceiverWithoutOpenNMS() throws Exception {
System.out.println("SNMP4J: Register for Traps");
trapCount = 0;
Snmp snmp = new Snmp(new DefaultUdpTransportMapping(new UdpAddress(9162)));
snmp.addCommandResponder(this);
snmp.getUSM().addUser(
new OctetString("opennmsUser"),
new UsmUser(new OctetString("opennmsUser"), AuthMD5.ID, new OctetString("0p3nNMSv3"), PrivDES.ID, new OctetString("0p3nNMSv3")));
snmp.listen();
sendTraps();
System.out.println("SNMP4J: Unregister for Traps");
snmp.close();
System.out.println("SNMP4J: Checking Trap status");
assertEquals(2, trapCount);
}
示例7: SNMPV3Session
import org.snmp4j.Snmp; //导入方法依赖的package包/类
/**
* 创建SNMPV3会话
* @param userInfo
* @throws IOException
* @throws AgentArgumentException
*/
public SNMPV3Session(SNMPV3UserInfo userInfo) throws IOException, AgentArgumentException {
if(StringUtils.isEmpty(userInfo.getAddress())){
throw new AgentArgumentException("SNMPV3Session创建失败:snmp v3协议的访问地址不能为空");
}
if(StringUtils.isEmpty(userInfo.getUsername())){
throw new AgentArgumentException("SNMPV3Session创建失败:snmp v3协议的访问用户名不能为空");
}
if(!StringUtils.isEmpty(userInfo.getAythType()) && StringUtils.isEmpty(userInfo.getAuthPswd())){
throw new AgentArgumentException("SNMPV3Session创建失败:snmp v3协议指定了认证算法 aythType,就必须要指定认证密码");
}
if(!StringUtils.isEmpty(userInfo.getPrivType()) && StringUtils.isEmpty(userInfo.getPrivPswd())){
throw new AgentArgumentException("SNMPV3Session创建失败:snmp v3协议指定了加密算法 privType,就必须要指定加密密码");
}
this.userInfo = userInfo;
snmp = new Snmp(new DefaultUdpTransportMapping());
USM usm = new USM(SecurityProtocols.getInstance(), new OctetString(MPv3.createLocalEngineID(
new OctetString(HostUtil.getHostIp() + UUID.randomUUID().toString())
)), 0);
SecurityModels.getInstance().addSecurityModel(usm);
snmp.listen();
UsmUser user = new UsmUser(
new OctetString(userInfo.getUsername()),
getAuthProtocol(userInfo.getAythType()), new OctetString(userInfo.getAuthPswd()),
getPrivProtocol(userInfo.getPrivType()), new OctetString(userInfo.getPrivPswd()));
snmp.getUSM().addUser(new OctetString(userInfo.getUsername()), user);
target = new UserTarget();
target.setSecurityName(new OctetString(userInfo.getUsername()));
target.setVersion(SnmpConstants.version3);
target.setSecurityLevel(SecurityLevel.AUTH_PRIV);
target.setAddress(GenericAddress.parse(userInfo.getProtocol() + ":" + userInfo.getAddress() + "/" + userInfo.getPort()));
target.setTimeout(TIMEOUT);
target.setRetries(1);
}
示例8: SNMPV3Session
import org.snmp4j.Snmp; //导入方法依赖的package包/类
/**
* 创建SNMPV3会话
* @param userInfo
* @throws IOException
* @throws AgentArgumentException
*/
public SNMPV3Session(SNMPV3UserInfo userInfo) throws IOException, AgentArgumentException {
if(StringUtils.isEmpty(userInfo.getAddress())){
throw new AgentArgumentException("SNMPV3Session创建失败:snmp v3协议的访问地址不能为空");
}
if(StringUtils.isEmpty(userInfo.getUsername())){
throw new AgentArgumentException("SNMPV3Session创建失败:snmp v3协议的访问用户名不能为空");
}
if(!StringUtils.isEmpty(userInfo.getAythType()) && StringUtils.isEmpty(userInfo.getAuthPswd())){
throw new AgentArgumentException("SNMPV3Session创建失败:snmp v3协议指定了认证算法 aythType,就必须要指定认证密码");
}
if(!StringUtils.isEmpty(userInfo.getPrivType()) && StringUtils.isEmpty(userInfo.getPrivPswd())){
throw new AgentArgumentException("SNMPV3Session创建失败:snmp v3协议指定了加密算法 privType,就必须要指定加密密码");
}
this.userInfo = userInfo;
snmp = new Snmp(new DefaultUdpTransportMapping());
USM usm = new USM(SecurityProtocols.getInstance(), new OctetString(MPv3.createLocalEngineID()), 0);
SecurityModels.getInstance().addSecurityModel(usm);
snmp.listen();
UsmUser user = new UsmUser(
new OctetString(userInfo.getUsername()),
getAuthProtocol(userInfo.getAythType()), new OctetString(userInfo.getAuthPswd()),
getPrivProtocol(userInfo.getPrivType()), new OctetString(userInfo.getPrivPswd()));
snmp.getUSM().addUser(new OctetString(userInfo.getUsername()), user);
target = new UserTarget();
target.setSecurityName(new OctetString(userInfo.getUsername()));
target.setVersion(SnmpConstants.version3);
target.setSecurityLevel(SecurityLevel.AUTH_PRIV);
target.setAddress(GenericAddress.parse(userInfo.getProtocol() + ":" + userInfo.getAddress() + "/" + userInfo.getPort()));
target.setTimeout(8000);
target.setRetries(1);
}
示例9: testWalkSystem
import org.snmp4j.Snmp; //导入方法依赖的package包/类
public void testWalkSystem() throws IOException {
Snmp snmp = new Snmp(new DefaultUdpTransportMapping());
TableUtils walker = new TableUtils(snmp, new DefaultPDUFactory());
snmp.listen();
Address addr = new UdpAddress(InetAddress.getLocalHost(), 9161);
//Address addr = new UdpAddress(InetAddressUtils.addr("192.168.0.100"), 161);
Target target = new CommunityTarget(addr, new OctetString("public"));
target.setVersion(SnmpConstants.version1);
target.setTimeout(3000);
target.setRetries(3);
// Implements snmp4j API
@SuppressWarnings("unchecked")
List results = walker.getTable(target, new OID[] {new OID("1.3.6.1.2.1.1")}, null, null);
assertNotNull(results);
assertFalse(results.isEmpty());
assertTrue(results.get(results.size()-1) instanceof TableEvent);
TableEvent lastEvent = (TableEvent)results.get(results.size()-1);
MockUtil.println("Status of lastEvent is "+lastEvent.getStatus());
assertEquals(TableEvent.STATUS_OK, lastEvent.getStatus());
}
示例10: start
import org.snmp4j.Snmp; //导入方法依赖的package包/类
public void start() throws Exception
{
snmp = new Snmp(new DefaultUdpTransportMapping(new UdpAddress(address, port)));
snmp.addCommandResponder(this);
snmp.listen();
}
示例11: testGetV1V2
import org.snmp4j.Snmp; //导入方法依赖的package包/类
/**
* Method for testing snmp V1/V2 get operation while all the input
* parameters are valid. The method validates the value got after the snmp
* get operation against the expected value.
*/
@Test
public void testGetV1V2()
{
try
{
PowerMock.mockStaticNice(InetAddress.class);
final InetAddress inetAddress = EasyMock.createMock(InetAddress.class);
EasyMock.expect(InetAddress.getByName(targetIP)).andReturn(inetAddress);
final UdpAddress address = PowerMock.createMockAndExpectNew(UdpAddress.class, inetAddress,
SnmpManager.DEFAULT_PORT_NUMBER);
final TransportMapping transportMappings = PowerMock
.createMockAndExpectNew(DefaultUdpTransportMapping.class);
final OctetString community = new OctetString(communityName);
EasyMock.replay(inetAddress);
PowerMock.replay(address, UdpAddress.class);
PowerMock.replay(transportMappings, DefaultUdpTransportMapping.class);
PowerMock.replay(InetAddress.class);
final Snmp snmp = PowerMock.createMockAndExpectNew(Snmp.class, transportMappings);
snmp.listen();
EasyMock.expectLastCall();
final CommunityTarget communityTarget = PowerMock.createMockAndExpectNew(CommunityTarget.class);
communityTarget.setCommunity(community);
EasyMock.expectLastCall();
communityTarget.setVersion(SnmpConstants.version1);
EasyMock.expectLastCall();
communityTarget.setAddress(address);
EasyMock.expectLastCall();
communityTarget.setRetries(SnmpManager.DEFAULT_RETRIES);
EasyMock.expectLastCall();
communityTarget.setTimeout(SnmpManager.DEFAULT_TIMEOUT);
EasyMock.expectLastCall();
PowerMock.replay(communityTarget, CommunityTarget.class);
final PDU pdu = PowerMock.createMockAndExpectNew(PDU.class);
pdu.setType(PDU.GET);
EasyMock.expectLastCall();
pdu.add(EasyMock.createMock(VariableBinding.class));
final OID oid = PowerMock.createMock(OID.class, oId);
final VariableBinding variableBinding = PowerMock.createMockAndExpectNew(VariableBinding.class, oid);
pdu.add(variableBinding);
EasyMock.expectLastCall();
snmp.close();
EasyMock.expectLastCall();
PowerMock.replay(oid, OID.class);
final ResponseEvent responseEvent = EasyMock.createMock(ResponseEvent.class);
EasyMock.expect(snmp.get(pdu, communityTarget)).andReturn(responseEvent);
EasyMock.expect(responseEvent.getResponse()).andReturn(pdu).anyTimes();
Vector< VariableBinding > variableBindings = new Vector< VariableBinding >();
variableBindings.add(variableBinding);
EasyMock.expect(pdu.getVariableBindings()).andReturn(variableBindings).anyTimes();
EasyMock.expect(pdu.getErrorStatus()).andReturn(0).anyTimes();
EasyMock.expect(pdu.getErrorStatusText()).andReturn("SNMP_ERROR_SUCCESS").anyTimes();
final Variable variable = new OctetString(getValue);
EasyMock.expect(variableBinding.getVariable()).andReturn(variable);
PowerMock.replay(snmp, Snmp.class);
PowerMock.replay(pdu, PDU.class);
EasyMock.replay(responseEvent);
PowerMock.replay(variableBinding, VariableBinding.class);
final SnmpServiceReturnMesage retnMsgActual = snmpManagerImpl.get(oId, communityName, targetIP,
SnmpManager.DEFAULT_PORT_NUMBER);
assertEquals(variable.toString(), retnMsgActual.getResultObject());
}
catch (Exception e)
{
e.printStackTrace();
}
}
示例12: testGetSysName
import org.snmp4j.Snmp; //导入方法依赖的package包/类
public void testGetSysName() throws IOException {
Snmp snmp = new Snmp(new DefaultUdpTransportMapping());
snmp.listen();
Address addr = new UdpAddress(InetAddress.getLocalHost(), 9161);
//Address addr = new UdpAddress(InetAddressUtils.addr("192.168.0.100"), 161);
Target target = new CommunityTarget(addr, new OctetString("public"));
target.setVersion(SnmpConstants.version1);
target.setTimeout(3000);
target.setRetries(3);
PDUv1 getRequest = new PDUv1();
getRequest.add(new VariableBinding(new OID("1.3.6.1.2.1.1.5.0")));
ResponseEvent e = snmp.get(getRequest, target);
PDU response = e.getResponse();
assertEquals(new OctetString("mockhost"), response.get(0).getVariable());
}
示例13: testWalkSystem
import org.snmp4j.Snmp; //导入方法依赖的package包/类
public void testWalkSystem() throws IOException {
Snmp snmp = new Snmp(new DefaultUdpTransportMapping());
TableUtils walker = new TableUtils(snmp, new DefaultPDUFactory());
snmp.listen();
Address addr = new UdpAddress(InetAddress.getLocalHost(), 9161);
//Address addr = new UdpAddress(InetAddressUtils.addr("192.168.0.100"), 161);
Target target = new CommunityTarget(addr, new OctetString("public"));
target.setVersion(SnmpConstants.version1);
target.setTimeout(3000);
target.setRetries(3);
@SuppressWarnings("unchecked")
List results = walker.getTable(target, new OID[] {new OID("1.3.6.1.2.1.1")}, null, null);
assertNotNull(results);
assertFalse(results.isEmpty());
assertTrue(results.get(results.size()-1) instanceof TableEvent);
TableEvent lastEvent = (TableEvent)results.get(results.size()-1);
MockUtil.println("Status of lastEvent is "+lastEvent.getStatus());
assertEquals(TableEvent.STATUS_OK, lastEvent.getStatus());
}