本文整理汇总了Java中org.snmp4j.Snmp.close方法的典型用法代码示例。如果您正苦于以下问题:Java Snmp.close方法的具体用法?Java Snmp.close怎么用?Java Snmp.close使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.snmp4j.Snmp
的用法示例。
在下文中一共展示了Snmp.close方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: sendTrapV2
import org.snmp4j.Snmp; //导入方法依赖的package包/类
public static void sendTrapV2(String port) throws IOException {
PDU trap = new PDU();
trap.setType(PDU.TRAP);
OID oid = new OID("1.2.3.4.5");
trap.add(new VariableBinding(SnmpConstants.snmpTrapOID, oid));
trap.add(new VariableBinding(SnmpConstants.sysUpTime, new TimeTicks(5000)));
trap.add(new VariableBinding(SnmpConstants.sysDescr, new OctetString("System Description")));
// Add Payload
Variable var = new OctetString("some string");
trap.add(new VariableBinding(oid, var));
// Specify receiver
Address targetaddress = new UdpAddress("127.0.0.1/" + port);
CommunityTarget target = new CommunityTarget();
target.setCommunity(new OctetString("public"));
target.setVersion(SnmpConstants.version2c);
target.setAddress(targetaddress);
// Send
Snmp snmp = new Snmp(new DefaultUdpTransportMapping());
snmp.send(trap, target, null, null);
snmp.close();
}
示例2: 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);
}
示例3: sendSnmpV1Trap
import org.snmp4j.Snmp; //导入方法依赖的package包/类
/**
* This methods sends the V1 trap to the Localhost in port 163
*/
public void sendSnmpV1Trap()
{
try
{
//Create Transport Mapping
TransportMapping transport = new DefaultUdpTransportMapping();
transport.listen();
//Create Target
CommunityTarget comtarget = new CommunityTarget();
comtarget.setCommunity(new OctetString(community));
comtarget.setVersion(SnmpConstants.version1);
comtarget.setAddress(new UdpAddress(ipAddress + "/" + port));
comtarget.setRetries(2);
comtarget.setTimeout(5000);
//Create PDU for V1
PDUv1 pdu = new PDUv1();
pdu.setType(PDU.V1TRAP);
pdu.setEnterprise(new OID(trapOid));
pdu.setGenericTrap(PDUv1.ENTERPRISE_SPECIFIC);
pdu.setSpecificTrap(1);
pdu.setAgentAddress(new IpAddress(ipAddress));
//Send the PDU
Snmp snmp = new Snmp(transport);
System.out.println("Sending V1 Trap to " + ipAddress + " on Port " + port);
snmp.send(pdu, comtarget);
snmp.close();
}
catch (Exception e)
{
System.err.println("Error in Sending V1 Trap to " + ipAddress + " on Port " + port);
System.err.println("Exception Message = " + e.getMessage());
}
}
示例4: closeQuietly
import org.snmp4j.Snmp; //导入方法依赖的package包/类
private void closeQuietly(Snmp session) {
if (session == null) {
return;
}
try {
session.close();
} catch (IOException e) {
ThreadCategory.getInstance(Snmp4JStrategy.class).error("error closing SNMP connection: " + e, e);
}
}
示例5: sendTrapV1
import org.snmp4j.Snmp; //导入方法依赖的package包/类
public static void sendTrapV1(String port) throws IOException {
TransportMapping<?> transport = new DefaultUdpTransportMapping();
transport.listen();
CommunityTarget comtarget = new CommunityTarget();
comtarget.setCommunity(new OctetString(new OctetString("public")));
comtarget.setVersion(SnmpConstants.version1);
comtarget.setAddress(new UdpAddress("127.0.0.1/" + port));
comtarget.setRetries(2);
comtarget.setTimeout(5000);
PDU trap = new PDUv1();
trap.setType(PDU.V1TRAP);
OID oid = new OID("1.2.3.4.5");
trap.add(new VariableBinding(SnmpConstants.snmpTrapOID, oid));
trap.add(new VariableBinding(SnmpConstants.sysUpTime, new TimeTicks(5000)));
trap.add(new VariableBinding(SnmpConstants.sysDescr, new OctetString("System Description")));
// Add Payload
Variable var = new OctetString("some string");
trap.add(new VariableBinding(oid, var));
// Send
Snmp snmp = new Snmp(transport);
snmp.send(trap, comtarget);
transport.close();
snmp.close();
}
示例6: sendTrapV3
import org.snmp4j.Snmp; //导入方法依赖的package包/类
public static void sendTrapV3(String port) {
try {
Address targetAddress = GenericAddress.parse("udp:127.0.0.1/" + port);
TransportMapping<?> transport = new DefaultUdpTransportMapping();
Snmp snmp = new Snmp(transport);
USM usm = new USM(SecurityProtocols.getInstance(), new OctetString(
MPv3.createLocalEngineID()), 0);
SecurityModels.getInstance().addSecurityModel(usm);
transport.listen();
snmp.getUSM().addUser(new OctetString("MD5DES"),
new UsmUser(new OctetString("MD5DES"), null, null, null, null));
// Create Target
UserTarget target = new UserTarget();
target.setAddress(targetAddress);
target.setRetries(1);
target.setTimeout(11500);
target.setVersion(SnmpConstants.version3);
target.setSecurityLevel(SecurityLevel.NOAUTH_NOPRIV);
target.setSecurityName(new OctetString("MD5DES"));
// Create PDU for V3
ScopedPDU pdu = new ScopedPDU();
pdu.setType(ScopedPDU.NOTIFICATION);
pdu.add(new VariableBinding(SnmpConstants.sysUpTime));
pdu.add(new VariableBinding(SnmpConstants.snmpTrapOID, SnmpConstants.linkDown));
pdu.add(new VariableBinding(new OID("1.2.3.4.5"), new OctetString("Major")));
// Send the PDU
snmp.send(pdu, target);
transport.close();
snmp.close();
} catch (Exception e) {
System.err.println("Error in Sending Trap to (IP:Port)=> " + "127.0.0.1" + ":" + port);
System.err.println("Exception Message = " + e.getMessage());
}
}
示例7: 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();
}
}
示例8: main
import org.snmp4j.Snmp; //导入方法依赖的package包/类
public static void main(String[] args) throws Exception
{
System.out.println("SNMP GET-NEXT Simple Request");
// Create TransportMapping and Listen
TransportMapping transport = new DefaultUdpTransportMapping();
transport.listen();
// Create Target Address object
CommunityTarget comtarget = new CommunityTarget();
comtarget.setCommunity(new OctetString(community));
comtarget.setVersion(snmpVersion);
comtarget.setAddress(new UdpAddress(ipAddress + "/" + port));
comtarget.setRetries(2);
comtarget.setTimeout(1000);
// Create the PDU object
PDU pdu = new PDU();
pdu.add(new VariableBinding(new OID(oidValue)));
pdu.setRequestID(new Integer32(1));
pdu.setType(PDU.GETNEXT);
// Create Snmp object for sending data to Agent
Snmp snmp = new Snmp(transport);
System.out.println("Sending GetNext Request to Agent ...");
ResponseEvent response = snmp.getNext(pdu, comtarget);
// Process Agent Response
if (response != null)
{
System.out.println("\nResponse:\nGot GetNext Response from Agent...");
PDU responsePDU = response.getResponse();
if (responsePDU != null)
{
int errorStatus = responsePDU.getErrorStatus();
int errorIndex = responsePDU.getErrorIndex();
String errorStatusText = responsePDU.getErrorStatusText();
if (errorStatus == PDU.noError)
{
System.out.println("Snmp GetNext Response for sysObjectID = " + responsePDU.getVariableBindings());
}
else
{
System.out.println("Error: Request Failed");
System.out.println("Error Status = " + errorStatus);
System.out.println("Error Index = " + errorIndex);
System.out.println("Error Status Text = " + errorStatusText);
}
}
else
{
System.out.println("Error: GetNextResponse PDU is null");
}
}
else
{
System.out.println("Error: Agent Timeout... ");
}
snmp.close();
}
示例9: sendSnmpV2Trap
import org.snmp4j.Snmp; //导入方法依赖的package包/类
/**
* This methods sends the V2 trap to the Localhost in port 163
*/
public void sendSnmpV2Trap()
{
try
{
//Create Transport Mapping
TransportMapping transport = new DefaultUdpTransportMapping();
transport.listen();
//Create Target
CommunityTarget comtarget = new CommunityTarget();
comtarget.setCommunity(new OctetString(community));
comtarget.setVersion(SnmpConstants.version2c);
comtarget.setAddress(new UdpAddress(ipAddress + "/" + port));
comtarget.setRetries(2);
comtarget.setTimeout(5000);
//Create PDU for V2
PDU pdu = new PDU();
// need to specify the system up time
pdu.add(new VariableBinding(SnmpConstants.sysUpTime, new OctetString(new Date().toString())));
pdu.add(new VariableBinding(SnmpConstants.snmpTrapOID, new OID(trapOid)));
pdu.add(new VariableBinding(SnmpConstants.snmpTrapAddress, new IpAddress(ipAddress)));
// variable binding for Enterprise Specific objects, Severity (should be defined in MIB file)
pdu.add(new VariableBinding(new OID(trapOid), new OctetString("Major")));
pdu.setType(PDU.NOTIFICATION);
//Send the PDU
Snmp snmp = new Snmp(transport);
System.out.println("Sending V2 Trap to " + ipAddress + " on Port " + port);
snmp.send(pdu, comtarget);
snmp.close();
}
catch (Exception e)
{
System.err.println("Error in Sending V2 Trap to " + ipAddress + " on Port " + port);
System.err.println("Exception Message = " + e.getMessage());
}
}
示例10: sendTrapV3Auth
import org.snmp4j.Snmp; //导入方法依赖的package包/类
public static void sendTrapV3Auth(String port) throws IOException {
try {
Address targetAddress = GenericAddress.parse("udp:127.0.0.1/" + port);
TransportMapping<?> transport = new DefaultUdpTransportMapping();
Snmp snmp = new Snmp(transport);
USM usm = new USM(SecurityProtocols.getInstance(), new OctetString(
MPv3.createLocalEngineID()), 0);
SecurityModels.getInstance().addSecurityModel(usm);
transport.listen();
snmp.getUSM().addUser(
new OctetString("user"),
new UsmUser(new OctetString("user"), AuthMD5.ID, new OctetString("12345678"),
null, null));
// Create Target
UserTarget target = new UserTarget();
target.setAddress(targetAddress);
target.setRetries(1);
target.setTimeout(11500);
target.setVersion(SnmpConstants.version3);
target.setSecurityLevel(SecurityLevel.AUTH_NOPRIV);
target.setSecurityName(new OctetString("user"));
// Create PDU for V3
ScopedPDU pdu = new ScopedPDU();
pdu.setType(ScopedPDU.NOTIFICATION);
pdu.add(new VariableBinding(SnmpConstants.sysUpTime));
pdu.add(new VariableBinding(SnmpConstants.snmpTrapOID, SnmpConstants.linkDown));
pdu.add(new VariableBinding(new OID("1.2.3.4.5"), new OctetString("Major")));
// Send the PDU
snmp.send(pdu, target);
transport.close();
snmp.close();
} catch (Exception e) {
System.err.println("Error in Sending Trap to (IP:Port)=> " + "127.0.0.1" + ":" + port);
System.err.println("Exception Message = " + e.getMessage());
}
}
示例11: sendTrapV3AuthPriv
import org.snmp4j.Snmp; //导入方法依赖的package包/类
public static void sendTrapV3AuthPriv(String port) throws IOException {
try {
Address targetAddress = GenericAddress.parse("udp:127.0.0.1/" + port);
TransportMapping<?> transport = new DefaultUdpTransportMapping();
Snmp snmp = new Snmp(transport);
USM usm = new USM(SecurityProtocols.getInstance(), new OctetString(
MPv3.createLocalEngineID()), 0);
SecurityModels.getInstance().addSecurityModel(usm);
transport.listen();
snmp.getUSM().addUser(
new OctetString("user"),
new UsmUser(new OctetString("user"), AuthMD5.ID, new OctetString("12345678"),
PrivDES.ID, new OctetString("passphrase")));
// Create Target
UserTarget target = new UserTarget();
target.setAddress(targetAddress);
target.setRetries(1);
target.setTimeout(11500);
target.setVersion(SnmpConstants.version3);
target.setSecurityLevel(SecurityLevel.AUTH_NOPRIV);
target.setSecurityName(new OctetString("user"));
// Create PDU for V3
ScopedPDU pdu = new ScopedPDU();
pdu.setType(ScopedPDU.NOTIFICATION);
pdu.add(new VariableBinding(SnmpConstants.sysUpTime));
pdu.add(new VariableBinding(SnmpConstants.snmpTrapOID, SnmpConstants.linkDown));
pdu.add(new VariableBinding(new OID("1.2.3.4.5"), new OctetString("Major")));
// Send the PDU
snmp.send(pdu, target);
transport.close();
snmp.close();
} catch (Exception e) {
System.err.println("Error in Sending Trap to (IP:Port)=> " + "127.0.0.1" + ":" + port);
System.err.println("Exception Message = " + e.getMessage());
}
}