Code snmp

farahoo Messages postés 45 Date d'inscription vendredi 12 février 2010 Statut Membre Dernière intervention 9 mai 2010 - 20 mars 2010 à 11:37
cs_Kysic Messages postés 332 Date d'inscription mardi 12 juillet 2005 Statut Membre Dernière intervention 17 juillet 2010 - 20 mars 2010 à 14:48
est ce que quelqun peut mexpliquer les lignes de ce code quest ce qu il fait ligne par ligne
merci
import snmp.*;
import java.util.*;
import java.math.*;
import java.net.*;



public class SNMPSample
{

public static void main(String args[])
{

try
{

// create a communications interface to a remote SNMP-capable device;
// need to provide the remote host's InetAddress and the community
// name for the device; in addition, need to supply the version number
// for the SNMP messages to be sent (the value 0 corresponding to SNMP
// version 1)
InetAddress hostAddress = InetAddress.getByName("10.0.1.1");
String community = "public";
int version = 0; // SNMPv1

SNMPv1CommunicationInterface comInterface = new SNMPv1CommunicationInterface(version, hostAddress, community);



// now send an SNMP GET request to retrieve the value of the SNMP variable
// corresponding to OID 1.3.6.1.2.1.1.1.0; this is the OID corresponding to
// the device identifying string, and the type is thus SNMPOctetString
String itemID = "1.3.6.1.2.1.1.1.0";

System.out.println("Retrieving value corresponding to OID " + itemID);

// the getMIBEntry method of the communications interface returns an SNMPVarBindList
// object; this is essentially a Vector of SNMP (OID,value) pairs. In this case, the
// returned Vector has just one pair inside it.
SNMPVarBindList newVars = comInterface.getMIBEntry(itemID);

// extract the (OID,value) pair from the SNMPVarBindList; the pair is just a two-element
// SNMPSequence
SNMPSequence pair = (SNMPSequence)(newVars.getSNMPObjectAt(0));

// extract the object identifier from the pair; it's the first element in the sequence
SNMPObjectIdentifier snmpOID = (SNMPObjectIdentifier)pair.getSNMPObjectAt(0);

// extract the corresponding value from the pair; it's the second element in the sequence
SNMPObject snmpValue = pair.getSNMPObjectAt(1);

// print out the String representation of the retrieved value
System.out.println("Retrieved value: type " + snmpValue.getClass().getName() + ", value " + snmpValue.toString());

// the retrieved value can be obtained from the SNMPObject using the getValue method;
// the return type of the method is the generic base class Object, and must be cast to
// the appropriate actual Java type; in this case, for an SNMPOctetString, the underlying
// Java type is a byte array[]
byte[] javaByteArrayValue = (byte[])snmpValue.getValue();



// now send an SNMP GET request to retrieve the value of the SNMP variable
// corresponding to OID 1.3.6.1.2.1.1.3.0; this is the OID corresponding to
// the uptime of the device, and the return type is thus SNMPTimeTicks
itemID = "1.3.6.1.2.1.1.3.0";

System.out.println("Retrieving value corresponding to OID " + itemID);

// the getMIBEntry method of the communications interface returns an SNMPVarBindList
// object; this is essentially a Vector of SNMP (OID,value) pairs. In this case, the
// returned Vector has just one pair inside it.
newVars = comInterface.getMIBEntry(itemID);

// extract the (OID,value) pair from the SNMPVarBindList; the pair is just a two-element
// SNMPSequence
pair = (SNMPSequence)(newVars.getSNMPObjectAt(0));

// extract the object identifier from the pair; it's the first element in the sequence
snmpOID = (SNMPObjectIdentifier)pair.getSNMPObjectAt(0);

// extract the corresponding value from the pair; it's the second element in the sequence
snmpValue = pair.getSNMPObjectAt(1);

// print out the String representation of the retrieved value
System.out.println("Retrieved value: type " + snmpValue.getClass().getName() + ", value " + snmpValue.toString());

// the retrieved value can be obtained from the SNMPObject using the getValue method;
// the return type of the method is the generic base class Object, and must be cast to
// the appropriate actual Java type; in this case, for SNMPTimeTicks, which is a subclass
// of SNMPInteger, the actual type is BigInteger (which permits arbitrarily large values to
// be represented).
BigInteger javaIntegerValue = (BigInteger)snmpValue.getValue();



// now send an SNMP SET request to set the value of the SNMP variable
// corresponding to OID 1.3.6.1.2.1.1.1.0; this is the OID corresponding to
// the device identifying string, and the type is thus SNMPOctetString;
// to set a new value, a string is supplied
itemID = "1.3.6.1.2.1.1.1.0";

SNMPOctetString newValue = new SNMPOctetString("New device name");

System.out.println("Setting value corresponding to OID " + itemID);
System.out.println("New value: " + newValue.toString());

// the setMIBEntry method of the communications interface returns the SNMPVarBindList
// corresponding to the supplied OID and value
// This call will probably cause an SNMPSetException to be thrown, since the
// community name "public" is probably not the read/write password of the device
newVars = comInterface.setMIBEntry(itemID, newValue);



}
catch(Exception e)
{
System.out.println("Exception during SNMP operation: " + e + "\n");
}

}

4 réponses

Utilisateur anonyme
20 mars 2010 à 11:57
Traduis les commentaires, c'est déjà expliqué. Un bon informaticien est sensé avoir un niveau correct en anglais technique. Utilise Google Translate sinon.











0
cs_DARKSIDIOUS Messages postés 15814 Date d'inscription jeudi 8 août 2002 Statut Membre Dernière intervention 4 mars 2013 130
20 mars 2010 à 12:00
Toujours pas bonjour à toi... C'est si dur que cà d'être poli et montrer un minimum de respect à la communauté codes-sources ?

Comment veux-tu qu'on te réponde : il y a tout plein de classe tirée d'une bibliothèque dont la très grande majorité d'entre nous n'en a jamais entendu parlé...

Il n'y a pas une doc liée à la bibliothèque que tu as trouvé sur le net ?
______________________________________

AVANT de poster votre message, veuillez lire, comprendre, et appliquer notre réglement
0
farahoo Messages postés 45 Date d'inscription vendredi 12 février 2010 Statut Membre Dernière intervention 9 mai 2010
20 mars 2010 à 12:11
je suis daccord avec toi
mais ilnest pas la le probleme !!!! il est dans la comprehension des commandes et mots cles java utilisés dans ce code je ne les connais pas tous je ne comprends pas leur structures
0
cs_Kysic Messages postés 332 Date d'inscription mardi 12 juillet 2005 Statut Membre Dernière intervention 17 juillet 2010
20 mars 2010 à 14:48
Bonjour,
Moi je ne comprends pas bien ce que tu ne comprends pas.
Il ne me semble pas y avoir de mots clés java particulier ici, peut être est-ce les conversions de types (cast) qui te dérange ?
Si connaît tu snmp et la MIB ? Si non commence par te renseigner à ce sujet.
Cdt
 
0
Rejoignez-nous