Come posso inviare trap snmpv2 dall'applicazione Java. Ho cercato di fare un esempio su snmp4j, ma non ha funzionato.Invia trap v2 in Java
risposta
Io uso SNMP4J per questo.
This javadoc potrebbe aiutarti a scrivere il tuo codice. È possibile utilizzare il metodo Snmp.trap()
Edit:
Beh, io non sono il codice della mia, in questo momento, ma si può fare riferimento this uno. È necessario utilizzare Snmp.notify() per l'invio di trap V2 anziché Snmp.trap() poiché trap() supporta solo l'invio di trap V1.
hai un semplice esempio, come farlo nel modo giusto? grazie! –
modificato la mia risposta per fornire un collegamento al codice – Gopi
Vorrei andare per la libreria 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);
hai un semplice esempio, come farlo nel modo giusto? grazie! –
Mi c'è voluto un po 'di tempo, ma alla fine ho capito come utilizzare SNMP4J per inviare una trappola: La speranza che aiuta ..
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);
}
Grazie! 'SysUpTime' e' sysDescr' sono obbligatori? – Matthieu
Un buon esempio che trasmette trappola V1 e V2 notifica. Ho solo bisogno di modificare la porta a 162 e il modo di impostare sysUpTime per entrambe le versioni V1 e V2.
Testato con snmp4j-1.10.1 e 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 Che cosa succede se il collegamento muore? Incollare il codice è una buona idea. –
Un ottimo esempio può essere trovato qui:
http://shivasoft.in/blog/java/snmp/create-snmp-client-in-java-using-snmp4j/
Ma non un esempio di invio di una trappola. – EJP
- 1. Definisci MIB e invia trap SNMP sotto .net
- 2. Invia posta tramite Gmail con Send-MailMessage di PowerShell V2
- 3. Jquery trap form submit()
- 4. Interruzione trap 6 errore in C
- 5. In che modo Java invia KeyEvents?
- 6. Invia Mail a più destinatari in java
- 7. Funzione trap passando gli argomenti?
- 8. Java String analisi - {k1 = v1, v2 = k2, ...}
- 9. Genera trap SNMP con PHP
- 10. È necessario specificare trap diversi da EXIT?
- 11. Invia dati a Matlab da Android/Java
- 12. Che cos'è "Trace/BPT trap: 5"?
- 13. Spiegare: trap 'sudo kill -9 - - $$' EXIT
- 14. come inviare trap net-snmp da "C"
- 15. Invia notifica push da server a dispositivo Android in Java
- 16. Invia dati in tempo reale da Java all'applicazione Android
- 17. Contenitore servlet per glassfish v2
- 18. Boost.Log vs. Boost.Log v2
- 19. Invia applicazione non possibile
- 20. WindowsAzure.Storage v2 StorageException
- 21. Mappe V2 con viewPager
- 22. API vocale Google V2
- 23. Importa KML in Maps API V2
- 24. Mappa non in emulatore android api v2
- 25. API di Google Maps v2 in Dialog
- 26. fullCalendar V2 eventi in background titolo
- 27. invia posta all'account Gmail
- 28. I simulatori 68k diversi hanno compiti TRAP diversi?
- 29. iOS UIWebView - trap eccezioni caricamento formato file non valido
- 30. Dove si trova lo stato di uscita dopo trap/return?
duplicati http://stackoverflow.com/questions/3329378/sending-traps-from-a- java-application –