IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Entrée/Sortie Java Discussion :

SNMP Avec Java


Sujet :

Entrée/Sortie Java

  1. #1
    Membre actif
    Homme Profil pro
    Développeur Java
    Inscrit en
    Avril 2012
    Messages
    48
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Tunisie

    Informations professionnelles :
    Activité : Développeur Java
    Secteur : Enseignement

    Informations forums :
    Inscription : Avril 2012
    Messages : 48
    Par défaut SNMP Avec Java
    salut toute le monde je veut lister un oid existant avec java ,j'ai le teste avec snmputil est coorect
    voila le code java
    Code java : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    import snmp.*; 
    import java.util.*; 
    import java.math.*; 
    import java.net.*; 
     
     
     
    public class SNMPSample2 
    {
     
        public static void main(String args[]) 
        {
     
            try 
            {
     
                InetAddress hostAddress = InetAddress.getByName("127.0.0.1");
                String community = "public"; 
                int version = 0;    // SNMPv1
     
                SNMPv1CommunicationInterface comInterface = new SNMPv1CommunicationInterface(version, hostAddress, community); 
     
     
                String itemID = ".1.3.6.1.2.1.1.1";
     
                System.out.println("Retrieving value corresponding to OID " + itemID);
     
               SNMPVarBindList newVars = comInterface.getMIBEntry(itemID);
     
     
                SNMPSequence pair = (SNMPSequence)(newVars.getSNMPObjectAt(0));
     
     
                SNMPObjectIdentifier snmpOID = (SNMPObjectIdentifier)pair.getSNMPObjectAt(0);
     
     
                SNMPObject snmpValue = pair.getSNMPObjectAt(1);
     
     
                System.out.println("Retrieved value: type " + snmpValue.getClass().getName() + ", value " + snmpValue.toString());
     
     
                byte[] javaByteArrayValue = (byte[])snmpValue.getValue(); 
     
     
                itemID = ".1.3.6.1.2.1.1.1";
     
                System.out.println("Retrieving value corresponding to OID " + itemID);
     
     
                newVars = comInterface.getMIBEntry(itemID);
     
     
                pair = (SNMPSequence)(newVars.getSNMPObjectAt(0));
     
     
                snmpOID = (SNMPObjectIdentifier)pair.getSNMPObjectAt(0);
     
     
                snmpValue = pair.getSNMPObjectAt(1);
     
     
                System.out.println("Retrieved value: type " + snmpValue.getClass().getName() + ", value " + snmpValue.toString());
            } 
            catch(Exception e) 
            { 
                System.out.println("Exception during SNMP operation:  " + e + "\n"); 
            }
     
        }
     
    }
    ---------------------------------------------------------
    Retrieving value corresponding to OID .1.3.6.1.2.1.1.1
    Exception during SNMP operation:  snmp.SNMPGetException: OID .1.3.6.1.2.1.1.1 not available for retrieval

  2. #2
    Membre actif
    Homme Profil pro
    Développeur Java
    Inscrit en
    Avril 2012
    Messages
    48
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Tunisie

    Informations professionnelles :
    Activité : Développeur Java
    Secteur : Enseignement

    Informations forums :
    Inscription : Avril 2012
    Messages : 48

  3. #3
    Membre actif
    Homme Profil pro
    Développeur Java
    Inscrit en
    Avril 2012
    Messages
    48
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Tunisie

    Informations professionnelles :
    Activité : Développeur Java
    Secteur : Enseignement

    Informations forums :
    Inscription : Avril 2012
    Messages : 48

  4. #4
    Membre actif
    Homme Profil pro
    Développeur Java
    Inscrit en
    Avril 2012
    Messages
    48
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Tunisie

    Informations professionnelles :
    Activité : Développeur Java
    Secteur : Enseignement

    Informations forums :
    Inscription : Avril 2012
    Messages : 48

  5. #5
    Membre actif
    Homme Profil pro
    Développeur Java
    Inscrit en
    Avril 2012
    Messages
    48
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Tunisie

    Informations professionnelles :
    Activité : Développeur Java
    Secteur : Enseignement

    Informations forums :
    Inscription : Avril 2012
    Messages : 48
    Par défaut
    Code java : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    package sNMP4JHelper;
     
    import org.snmp4j.CommunityTarget;
     
    import org.snmp4j.PDU;
     
    import org.snmp4j.Snmp;
     
    import org.snmp4j.TransportMapping;
     
    import org.snmp4j.event.ResponseEvent;
     
    import org.snmp4j.event.ResponseListener;
     
    import org.snmp4j.mp.SnmpConstants;
     
    import org.snmp4j.smi.*;
     
    import org.snmp4j.transport.DefaultUdpTransportMapping;
     
    public class SNMP4JHelper {
     
          public static final String READ_COMMUNITY = "public";
     
          public static final String WRITE_COMMUNITY= "private";
     
          public static final int mSNMPVersion =0; // 0 represents SNMP version=1
     
          public static final String OID_UPS_OUTLET_GROUP1 =
     
          "1.3.6.1.4.1.318.1.1.1.12.3.2.1.3.1";
     
          public static final String OID_SYS_DESCR="1.3.6.1.2.1.1.1.0";
     
          public static void main(String[] args)
     
          {
     
          try
     
          {
     
          String strIPAddress = "127.0.0.1";
     
          SNMP4JHelper objSNMP = new SNMP4JHelper();
     
          //objSNMP.snmpSet();
     
         ////////////////////////////////////////////
     
          //Set Value=2 to trun OFF UPS OUTLET Group1
     
          //Value=1 to trun ON UPS OUTLET Group1
     
          //////////////////////////////////////////
     
          int Value = 2;
     
          //objSNMP.snmpSet(strIPAddress, WRITE_COMMUNITY,OID_UPS_OUTLET_GROUP1, Value);
     
          //////////////////////////////////////////////////////////
     
          //Get Basic state of UPS
     
          /////////////////////////////////////////////////////////
     
          String batteryCap =objSNMP.snmpGet(strIPAddress,READ_COMMUNITY,OID_SYS_DESCR);
     
          }
     
          catch (Exception e)
     
          {
     
          e.printStackTrace();
     
          }
     
    }
     
    /*
     
    * The following code valid only SNMP version1. This
     
    * method is very useful to set a parameter on remote device.
     
    */
     
    public void snmpSet(String strAddress, String community, String strOID, int Value)
     
    {
     
    strAddress= strAddress+"/"+161;
     
    Address targetAddress = GenericAddress.parse(strAddress);
     
    Snmp snmp;
     
    try
     
    {
     
    TransportMapping transport = new DefaultUdpTransportMapping();
     
    snmp = new Snmp(transport);
     
    transport.listen();
     
    CommunityTarget target = new CommunityTarget();
     
    target.setCommunity(new OctetString(community));
     
    target.setAddress(targetAddress);
     
    target.setRetries(2);
     
    target.setTimeout(5000);
     
    target.setVersion(SnmpConstants.version1);
     
    PDU pdu = new PDU();
     
    pdu.add(new VariableBinding(new OID(strOID), new Integer32(Value)));
     
    pdu.setType(PDU.SET);
     
    ResponseListener listener = new ResponseListener() {
     
          public void onResponse(ResponseEvent event) {
     
                // Always cancel async request when response has been received
     
                // otherwise a memory leak is created! Not canceling a request
     
                // immediately can be useful when sending a request to a broadcast
     
                // address.
     
                ((Snmp)event.getSource()).cancel(event.getRequest(), this);
     
                System.out.println("Set Status is:"+event.getResponse().getErrorStatusText());
     
                }
     
                };
     
                snmp.send(pdu, target, null, listener);
     
                snmp.close();
     
                }
     
                catch (Exception e)
     
                {
     
                e.printStackTrace();
     
                }
     
                }
     
     /*
    * The code is valid only SNMP version1. SnmpGet method
     
    * return Response for given OID from the Device.
     
    */
     
    public String snmpGet(String strAddress, String community, String strOID)
     
    {
     
    String str="";
     
    try
     
    {
     
    OctetString community1 = new OctetString(community);
     
    strAddress= strAddress+"/" + 161;
     
    Address targetaddress = new UdpAddress(strAddress);
     
    TransportMapping transport = new DefaultUdpTransportMapping();
     
    transport.listen();
     
    CommunityTarget comtarget = new CommunityTarget();
     
    comtarget.setCommunity(community1);
     
    comtarget.setVersion(SnmpConstants.version1);
     
    comtarget.setAddress(targetaddress);
     
    comtarget.setRetries(2);
     
    comtarget.setTimeout(5000);
     
    PDU pdu = new PDU();
     
    ResponseEvent response;
     
    Snmp snmp;
     
    pdu.add(new VariableBinding(new OID(strOID)));
     
    pdu.setType(PDU.GET);
     
    snmp = new Snmp(transport);
     
    response = snmp.get(pdu,comtarget);
     
    if(response != null)
     
    {
     
    if(response.getResponse().getErrorStatusText().equalsIgnoreCase("Success"))
     
    {
     
    PDU pduresponse=response.getResponse();
     
    str=pduresponse.getVariableBindings().firstElement().toString();
     
    if(str.contains("="))
     
    {
     
    int len = str.indexOf("=");
     
    str=str.substring(len+1, str.length());
     
    }
     
    }
     
    }
     
    else
     
    {
     
    System.out.println("Feeling like a TimeOut occured ");
     
    }
     
    snmp.close();
     
    } catch(Exception e) { e.printStackTrace(); }
     
    System.out.println("Response="+str);
     
    return str;
     
    }
     
    }








+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. détection d'etat de port switch avec java snmp
    Par hichem tunis dans le forum Général Java
    Réponses: 0
    Dernier message: 31/03/2011, 11h36
  2. meilleure base de données avec java
    Par mial dans le forum JDBC
    Réponses: 11
    Dernier message: 10/11/2010, 11h49
  3. [CR][Java] imprimer un état CR avec java
    Par wassimb dans le forum SDK
    Réponses: 2
    Dernier message: 24/05/2004, 16h40
  4. Réponses: 3
    Dernier message: 27/01/2004, 16h15
  5. [JNDI] Equivalent avec Java Web Start ?
    Par cameleon2002 dans le forum Java EE
    Réponses: 8
    Dernier message: 18/09/2003, 18h55

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo