本文整理汇总了Java中org.snmp4j.Snmp.send方法的典型用法代码示例。如果您正苦于以下问题:Java Snmp.send方法的具体用法?Java Snmp.send怎么用?Java Snmp.send使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.snmp4j.Snmp
的用法示例。
在下文中一共展示了Snmp.send方法的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: 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());
}
}
}
示例2: sendTrap
import org.snmp4j.Snmp; //导入方法依赖的package包/类
/**
* Send next-in-line trap from queue to to SNMP server
*/
public void sendTrap() {
String trapVal = trapQueue.poll();
if (trapVal == null) {
return;
}
try {
PDUv1 trapPdu = createTrapPDU(trapVal);
DefaultUdpTransportMapping tm = new DefaultUdpTransportMapping();
Snmp snmp = new Snmp(tm);
tm.listen();
OctetString community = new OctetString(SNMP_XAP_COMMUNITY);
Address add = GenericAddress.parse("udp" + ":" + snmpServerIP + "/" + snmpServerPort);
CommunityTarget target = new CommunityTarget(add, community);
target.setVersion(SnmpConstants.version1);
target.setRetries(0);
target.setTimeout(5000);
snmp.send(trapPdu, target);
}
catch (IOException e) {
e.printStackTrace();
}
}
示例3: snmpGet
import org.snmp4j.Snmp; //导入方法依赖的package包/类
/**
* 获取指定OID的 get
*
* @param snmp
* @param target
* @param oid
* @return
* @throws IOException
*/
public static PDU snmpGet(Snmp snmp, Target target, String oid) throws IOException {
ScopedPDU pdu = new ScopedPDU();
pdu.setType(PDU.GET);
pdu.add(new VariableBinding(new OID(oid)));
ResponseEvent responseEvent = snmp.send(pdu, target);
PDU response = responseEvent.getResponse();
if(response == null){
log.warn("response null - error:{} peerAddress:{} source:{} request:{}",
responseEvent.getError(),
responseEvent.getPeerAddress(),
responseEvent.getSource(),
responseEvent.getRequest());
}
return response;
}
示例4: 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();
}
示例5: send
import org.snmp4j.Snmp; //导入方法依赖的package包/类
public void send() throws IOException {
// Create PDU
PDU trap = new PDU();
trap.setType(PDU.TRAP);
if (this.varBinds.size() == 0) {
addDefaultTrap();
}
// Add the varbinds to the trap
for (VariableBinding vb : this.varBinds) {
trap.add(vb);
}
// Set our target
Address targetaddress = new UdpAddress(getTargetAddress());
CommunityTarget target = new CommunityTarget();
// Set the community read string
target.setCommunity(new OctetString(this.community));
// Set the version of the trap
target.setVersion(version.version);
target.setAddress(targetaddress);
LOG.info("trap: {}",trap);
// Send the trap
Snmp snmp = new Snmp(new DefaultUdpTransportMapping());
snmp.send(trap, target, null, null);
}
示例6: 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());
}
}
示例7: 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();
}
示例8: 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());
}
}
示例9: getIpOfGateway
import org.snmp4j.Snmp; //导入方法依赖的package包/类
public static String getIpOfGateway(){
String gatewayIpString=null; //网关ip地址是这个字符串的子串
String gatewayIp=null; // 这是代表网关ip
try {
/**
* Set properties of target
*/
CommunityTarget localhost = new CommunityTarget();
Address address = GenericAddress.parse("udp:127.0.0.1/161");
localhost.setAddress(address);
localhost.setCommunity(new OctetString("public"));
localhost.setRetries(2);
localhost.setTimeout(5*60);
localhost.setVersion(SnmpConstants.version2c);
/**
* Set protocols of UDP and SNMP
*/
TransportMapping transport = new DefaultUdpTransportMapping();
transport.listen();
Snmp protocol = new Snmp(transport);
/**
* OID binding
*/
PDU requestPDU = new PDU();
requestPDU.add(new VariableBinding(new OID("1.3.6.1.2.1.4.21.1.7")));//ipRouteNextHop
requestPDU.setType(PDU.GETNEXT);
/**
*
*/
ResponseEvent responseEvent = protocol.send(requestPDU, localhost);
PDU responsePDU=responseEvent.getResponse();
if(responsePDU!=null){
VariableBinding getIp=responsePDU.get(0);
gatewayIpString=getIp.toString();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
gatewayIp=gatewayIpString.substring(31);
return gatewayIp;
}
示例10: 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());
}
}
示例11: 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());
}
}
示例12: 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());
}
}
示例13: snmpGetNext
import org.snmp4j.Snmp; //导入方法依赖的package包/类
/**
* 获取指定OID的 getNext
*
* @param snmp
* @param target
* @param oid
* @return
* @throws IOException
*/
public static PDU snmpGetNext(Snmp snmp, Target target, String oid) throws IOException {
ScopedPDU pdu = new ScopedPDU();
pdu.setType(PDU.GETNEXT);
pdu.add(new VariableBinding(new OID(oid)));
ResponseEvent responseEvent = snmp.send(pdu, target);
return responseEvent.getResponse();
}