如何从Java应用程序发送snmpv2陷阱。我试图做snmp4j的例子,但它没有奏效。在Java中发送陷阱v2
回答
我为此使用SNMP4J。
This javadoc可能会帮助您编写代码。您可以使用Snmp.trap()方法
编辑:
好了,我不就在这时有我自己的代码,但你可以参考this之一。您必须使用Snmp.notify()发送V2陷阱而不是Snmp.trap(),因为trap()仅支持发送V1陷阱。
你有一个简单的例子,怎么做对不对? 谢谢! – 2010-07-28 17:16:11
编辑我的答案,提供一个链接到代码 – Gopi 2010-07-29 17:40:20
我会去snmp4j库http://www.snmp4j.org/。
import org.snmp4j.*;
import org.snmp4j.event.*;
...
CommunityTarget target = new CommunityTarget();
target.setCommunity(new OctetString("public"));
target.setAddress(targetAddress);
target.setVersion(SnmpConstants.version2c);
PDU request = new PDU();
request.setType(PDU.V2TRAP);
request.setGenericTrap(PDUv2.COLDSTART);
Snmp snmp = new Snmp(new DefaultUdpTransportMapping());
snmp.listen();
snmp.sendPDU(request, target, null, listener);
你有一个简单的例子,该怎么做对不对? 谢谢! – 2010-07-28 17:16:49
我花了一些时间,但我终于想通了如何使用SNMP4J发送陷阱:希望帮助..
public static void main(String[] args) throws Exception {
// Create PDU
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))); // put your uptime here
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("10.101.21.32/162");
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);
}
谢谢! “sysUpTime”和“sysDescr”是强制性的吗? – Matthieu 2014-12-31 03:59:34
发送陷阱V1和V2通知一个很好的例子。我只需要将端口修改为162以及为版本V1和V2设置sysUpTime的方式。
使用snmp4j-1.10.1和snmp4j-2.1.0进行测试。
http://www.techdive.in/snmp/snmp4j-trap-sender
package snippet;
/**
* Copyright 2010 TechDive.in
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* <a href="http://www.apache.org/licenses/LICENSE-2.0" title="http://www.apache.org/licenses/LICENSE-2.0">http://www.apache.org/licenses/LICENSE-2.0</a>
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
/**
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS''
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
import java.util.Date;
import org.snmp4j.CommunityTarget;
import org.snmp4j.PDU;
import org.snmp4j.PDUv1;
import org.snmp4j.Snmp;
import org.snmp4j.TransportMapping;
import org.snmp4j.mp.SnmpConstants;
import org.snmp4j.smi.IpAddress;
import org.snmp4j.smi.OID;
import org.snmp4j.smi.OctetString;
import org.snmp4j.smi.UdpAddress;
import org.snmp4j.smi.TimeTicks;
import org.snmp4j.smi.VariableBinding;
import org.snmp4j.transport.DefaultUdpTransportMapping;
public class TrapSender {
public static final String community = "public";
// Sending Trap for sysLocation of RFC1213
public static final String trapOid = ".1.3.6.1.2.1.1.6";
public static final String ipAddress = "192.168.1.52";
public static final int port = 162;
public TrapSender() {
}
public static void main(String[] args) {
TrapSender snmp4JTrap = new TrapSender();
/* Sending V1 Trap */
snmp4JTrap.sendSnmpV1Trap();
/* Sending V2 Trap */
snmp4JTrap.sendSnmpV2Trap();
}
/**
* 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));
long sysUpTime = 111111;
pdu.setTimestamp(sysUpTime);
// 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());
}
}
/**
* 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
long sysUpTime = 111111;
pdu.add(new VariableBinding(SnmpConstants.sysUpTime, new TimeTicks(sysUpTime)));
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());
}
}
}
@ durron597那么,如果链接死亡会发生什么?粘贴代码是一个好主意。 – 2014-10-20 13:16:39
但不是一个发送陷阱的例子。 – EJP 2015-10-15 04:04:58
- 1. 从Java应用程序发送陷阱
- 2. Java JIT陷阱
- 3. 如何从“C”发送net-snmp陷阱
- 4. 如何使用C#发送SNMP陷阱?
- 5. 定义MIB并发送SNMP陷阱.net
- 6. Datastax Opscenter 6.0不发送SNMP陷阱
- 7. 如何使用PowerShell发送SNMP陷阱?
- 8. 如何发送和陷阱发送到PID的信号,用C
- 9. 陷阱
- 10. C++程序员有哪些常见的Java陷阱/陷阱?
- 11. 是否可以在SNMP陷阱中发送表?
- 12. pysnmp:在陷阱中发送十六进制字符串
- 13. 从MS Access的空陷阱的陷阱
- 14. 软件陷阱vs硬件陷阱
- 15. 如何在陷阱代码中再次设置bash陷阱?
- 16. 陷阱os.Exit在golang
- 17. 春季陷阱
- 18. SetTimer()陷阱
- 19. 形式 - 陷阱
- 20. PyThreadState_SetAsyncExc陷阱?
- 21. .NET中ClickOnce /智能客户端部署的陷阱/陷阱
- 22. 如何在发送陷阱之前在SNMP4J库中设置对等地址?
- 23. ValueConstraintError在pysnmp陷阱接收
- 24. 我被困在UpdatePanel陷阱
- 25. 在Windows上调度陷阱
- 26. 陷阱错误在httpwebresponse
- 27. 中止陷阱6用C
- 28. 中止陷阱6的malloc
- 29. 如何发送SNMP陷阱到不同的IP
- 30. 不发送所有SNMP陷阱的应用程序
重复http://stackoverflow.com/questions/3329378/sending-traps-from-a- java-application – 2010-07-29 16:48:06