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
| package com.example.salleho;
import java.io.IOException;
import org.snmp4j.CommunityTarget;
import org.snmp4j.PDU;
import org.snmp4j.Snmp;
import org.snmp4j.TransportMapping;
import org.snmp4j.event.ResponseEvent;
import org.snmp4j.mp.SnmpConstants;
import org.snmp4j.smi.Integer32;
import org.snmp4j.smi.OID;
import org.snmp4j.smi.OctetString;
import org.snmp4j.smi.UdpAddress;
import org.snmp4j.smi.VariableBinding;
import org.snmp4j.transport.DefaultUdpTransportMapping;
import com.example.salleho.R;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity {
private static String ipAddress = "192.168.50.42";
private static String port = "161";
//cmd to request from Server
private static String oidValue = ".1.3.6.1.4.1.318.1.1.12.3.3.1.1.4.3";
private static int snmpVersion = SnmpConstants.version2c;
private static String community = "supelec";
public static Snmp snmp;
public static CommunityTarget comtarget;
static PDU pdu;
static OID oid;
static VariableBinding req;
Button button;
private static final String tag = "SNMP CLIENT";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.i(tag,"bouton");
button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
try {
Log.i(tag ,"Appelle la fonction");
sendSnmpRequest(oidValue);
} catch (IOException e) {
// TODO Auto-generated catch block
Log.i(tag ,"Erreur");
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
Log.i(tag ,"Erreur");
e.printStackTrace();
}
}
});
}
private void sendSnmpRequest(String cmd) throws Exception {
// Create TransportMapping and Listen
Log.i(tag ,"Creation du trasport");
TransportMapping transport = new DefaultUdpTransportMapping();
transport.listen();
Log.i(tag ,"fin du trasport");
// Create Target Address object
Log.i(tag ,"Fixe l'IP etc....");
CommunityTarget comtarget = new CommunityTarget();
comtarget.setCommunity(new OctetString(community));
comtarget.setVersion(snmpVersion);
comtarget.setAddress(new UdpAddress(ipAddress + "/" + port));
comtarget.setRetries(2);
comtarget.setTimeout(1000);
// Prepare PDU
Log.i(tag ,"Creation du PDU");
req = new VariableBinding();
oid = new OID(cmd);
req.setOid(oid);
pdu = new PDU();
pdu.add(new VariableBinding(oid, new Integer32(2)));
pdu.setType(PDU.SET);
snmp = new Snmp(transport);
Log.i(tag ,"Envoie de la requete au Switch");
ResponseEvent response = snmp.send(pdu, comtarget, null);
if (response != null) {
Log.i(tag,"Got Response from Agent");
PDU responsePDU = response.getResponse();
if (responsePDU != null) {
int errorStatus = responsePDU.getErrorStatus();
int errorIndex = responsePDU.getErrorIndex();
String errorStatusText = responsePDU.getErrorStatusText();
if (errorStatus == PDU.noError) {
Log.i(tag,"Snmp Get Response = "
+ responsePDU.getVariableBindings());
Toast.makeText(getApplicationContext(),
"Snmp Get Response = " + responsePDU.getVariableBindings(), Toast.LENGTH_LONG).show();
} else {
Log.i((String) tag,"Error: Request Failed");
Log.i(tag,"Error Status = " + errorStatus);
Log.i(tag,"Error Index = " + errorIndex);
Log.i(tag,"Error Status Text = " + errorStatusText);
}
} else {
Log.i(tag,"Error: Response PDU is null");
}
} else {
Log.i(tag,"Error: Agent Timeout... ");
}
snmp.close();
}
} |
Partager