geronimo-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From snmpost <aoun.fa...@gmail.com>
Subject snmp protocol
Date Sat, 20 Mar 2010 11:29:36 GMT

hi,
iam trying to understand this codev but i could not
is there someone who can help me please
import org.snmp4j.CommunityTarget;
import org.snmp4j.PDU;
import org.snmp4j.ScopedPDU;
import org.snmp4j.Snmp;
import org.snmp4j.TransportMapping;
import org.snmp4j.UserTarget;
import org.snmp4j.event.ResponseEvent;
import org.snmp4j.event.ResponseListener;
import org.snmp4j.mp.MPv3;
import org.snmp4j.mp.SnmpConstants;
import org.snmp4j.security.AuthMD5;
import org.snmp4j.security.PrivDES;
import org.snmp4j.security.SecurityLevel;
import org.snmp4j.security.SecurityModels;
import org.snmp4j.security.SecurityProtocols;
import org.snmp4j.security.USM;
import org.snmp4j.security.UsmUser;
import org.snmp4j.smi.*;
import org.snmp4j.transport.DefaultUdpTransportMapping;

public class snmp2
{
 public static final String READ_COMMUNITY = "public";
 public static final String WRITE_COMMUNITY= "private";

 private static final String NameMach = "1.3.6.1.2.1.1.5.0";
 private static final String IpTTL="1.3.6.1.2.1.4.2.0";
 private static final String RAM="1.3.6.1.2.1.25.2.2.0";
 private static final String NbProcess="1.3.6.1.2.1.25.1.6.0";
 private static final String NbMaxProcess="1.3.6.1.2.1.25.1.7.0";
 private static final String Ip="1.3.6.1.2.1.4.20.1.1";
 private static final String StorageDesc="1.3.6.1.2.1.25.2.3.1.3.2";

 private String msg="";

 public snmp2()
 {
   try
    {
        String strIPAddress = "127.0.0.1";
        String val1="EXPLORATION";
        String val2="Exp";

        //snmpSet(strIPAddress, WRITE_COMMUNITY,NameMach, val1);

        msg =snmpGet(strIPAddress,READ_COMMUNITY,NameMach);
        //msg =snmpGet(strIPAddress,READ_COMMUNITY,IpTTL);
        //msg =snmpGet(strIPAddress,READ_COMMUNITY,RAM);
        //msg =snmpGet(strIPAddress,READ_COMMUNITY,NbProcess);
        //msg =snmpGet(strIPAddress,READ_COMMUNITY,NbMaxProcess);
        //msg =snmpGet(strIPAddress,READ_COMMUNITY,Ip);
        //msg =snmpGet(strIPAddress,READ_COMMUNITY,StorageDesc);

        //msg =snmpv3Get(strIPAddress,READ_COMMUNITY,NameMach);

     }
    catch (Exception e)
    {
          e.printStackTrace();
    }
 }
 private int SNMP_PORT=161;

 public void snmpSet(String strAddress, String community, String strOID,
String Value)
 {
   strAddress= strAddress+"/"+SNMP_PORT;
   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.add(new VariableBinding(new OID(strOID), new OctetString(Value)));

    pdu.setType(PDU.SET);

    ResponseListener listener = new ResponseListener()
     {
       public void onResponse(ResponseEvent event)
        {
          ((Snmp)event.getSource()).cancel(event.getRequest(), this);
          msg="Set Status is:"+event.getResponse().getErrorStatusText();
          //System.out.println(msg);
        }
     };
    snmp.send(pdu, target, null, listener);
    snmp.close();
  }
  catch (Exception e)
  {
      e.printStackTrace();
  }
 }

 public String snmpGet(String strAddress, String communit, String strOID)
  {
   String str="";
   try
   {
     OctetString community = new OctetString(communit);
     strAddress= strAddress+"/"+SNMP_PORT;
     Address targetaddress = new UdpAddress(strAddress);
     TransportMapping transport = new DefaultUdpTransportMapping();
     Snmp snmp = new Snmp(transport);
     transport.listen();

     CommunityTarget comtarget = new CommunityTarget();
     comtarget.setCommunity(community);
     comtarget.setVersion(SnmpConstants.version1);
     comtarget.setAddress(targetaddress);
     comtarget.setRetries(2);
     comtarget.setTimeout(5000);

     PDU pdu = new PDU();
     ResponseEvent response;

     pdu.add(new VariableBinding(new OID(strOID)));
     pdu.setType(PDU.GET);

     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 str+="echec1";
       }
     else
      {
         msg="Feeling like a TimeOut occured";
         //System.out.println(msg);
      }
    snmp.close();
   }
   catch(Exception e)
   {
       //e.printStackTrace();
       str+="echec2";
   }
    //System.out.println("Response="+str);
    return str;
  }

 public String snmpv3Get(String strAddress, String communit, String strOID)
  {
   String str="";
   try
   {
     strAddress= strAddress+"/"+SNMP_PORT;
     Address targetaddress = new UdpAddress(strAddress);
     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.getListenAddress();

     byte a[]=new byte[]{1,2};
     transport.sendMessage(targetaddress, a);
     transport.listen();


     snmp.getUSM().addUser(new OctetString("MD5DES"), new UsmUser(new
OctetString("MD5DES"),AuthMD5.ID,new
OctetString("MD5DESUserAuthPassword"),PrivDES.ID,new
OctetString("MD5DESUserPrivPassword")));

     UserTarget comtarget = new UserTarget();
     comtarget.setAddress(targetaddress);
     comtarget.setRetries(1);
     comtarget.setTimeout(5000);
     comtarget.setVersion(SnmpConstants.version3);
     comtarget.setSecurityLevel(SecurityLevel.AUTH_PRIV);
     comtarget.setSecurityName(new OctetString("MD5DES"));

     PDU pdu = new ScopedPDU();
     ResponseEvent response;

     pdu.add(new VariableBinding(new OID(strOID)));
     pdu.setType(PDU.GET);

     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 str+="echec1";
       }
     else
      {
         msg="Feeling like a TimeOut occured";
         //System.out.println(msg);
      }
    snmp.close();
   }
   catch(Exception e)
   {
       //e.printStackTrace();
       str+=e.getMessage();
   }
    //System.out.println("Response="+str);
    return str;
  }

 public String Aff()
 {
     return msg;
 }
}
---Fin de la citation---


		
	
	


-- 
View this message in context: http://n3.nabble.com/snmp-protocol-tp464057p464057.html
Sent from the Development mailing list archive at Nabble.com.

Mime
View raw message