Snmp protocol for Android (1)
Early January 2015. I received a call from a Huawei instructor and asked me to help me develop an App they would use at the Beijing Exhibition, this App can display the running status of Huawei's network devices and set some simple parameters, including AP, LSW, AP and AR.
The Huawei instructor told me that they managed the protocol using the snmp protocol v2c, which means that I had to implement the Protocol on Android devices and interact with their network devices.
Go back and study it carefully and ask some friends who are familiar with it. Finally, I have a rough understanding of snmp.
Snmp refers to a simple network device management protocol. as its name implies, it is a general standard protocol for managing network devices. It belongs to the TCP/IP application layer. The snmp server occupies port 161, the client uses 162 (based on UDP ).
To enable the snmp protocol on windows, refer to http://blog.csdn.net/zougangx/article/details/6977936. Note that the Security tab of the SNMP Service attribute allows us to Accept snmp Packets from any host for debugging.
The OID (object identifier) is used as the query content for the connection between two devices. For details about the OID content, see http://www.cnblogs.com/aspx-net/p/3554044.html. Some of the OID protocols are well defined, and some device vendors can define it themselves.
After completing the above steps and familiarizing myself with the basic OID commands, some online users say they can use Paessler SNMP Tester for debugging. However, I did not perform this smoothly in actual operations, paessler SNMP Tester always displays noresponse and uses snmputil instead. (Paessler SNMP Tester and snmputil are both tools for testing the snmp protocol on windows, Paessler SNMP Tester has a graphical interface, snmputil is not, the operation of snmputil can refer to the http://blog.chinaunix.net/uid-21857285-id-3340217.html)
When using snmputil, the error on SnmpMgrRequest 40 occurs. refer to the following URL to solve the problem. So far, my snmputil and Paessler SNMP Tester are running normally!
In the service list of the computer, you can see:
The Trap message must be manually enabled, while the service is automatically enabled. As for how to use the snmp trap service, I am not very clear about the reasons for setting the trap tab of the snmp service. I also hope that some people will not be able to give me any further instructions, and I will not conduct any further research on the project.
After both services are enabled, you can use netstat-an "findstr" 162 "or netstat-an | findstr" 161 "to check whether the port is developed, 161 you can perform a local test after enabling the tool.
The snmp protocol is a TCP/IP protocol and is completed in c language. I have previously transplanted uip1.0 and it is also written in c language. Android must be implemented using Java. To this end, I first used the snmp4j jar package, established a Java project, and counterfeited examples of official documents, coding is as follows (two jar packages of snmp4j need to be introduced ):
Class SnmpManager {private TransportMapping transportMapping = null; private Snmp snmp = null; private int version; public final static int version1 = SnmpConstants. version1; public final static int version2c = SnmpConstants. version2c; public final static int version3 = SnmpConstants. version3;/*** constructor * @ param version */public SnmpManager (int version) {this. version = version; try {// set Udp transportMapping = new DefaultUdpTransportMapping (); snmp = new Snmp (transportMapping); if (version = version3) {// set the Security Information USM usm = new USM (SecurityProtocols. getInstance (), new OctetString (MPv3.createLocalEngineID (), 0); SecurityModels. getInstance (). addSecurityModel (usm);} transportMapping. listen ();} catch (Exception e) {e. printStackTrace (); System. out. println (e) ;}}/*** @ param Sync * @ param bro * @ param pdu * @ param addr * message sending Method */public void sendMsg (boolean sync, final boolean bro, PDU pdu, String addr) {Address targetAddres = GenericAddress. parse (addr); Target target = null; if (this. version = version3) {snmp. getUSM (). addUser (new OctetString ("MD5DES"), new UsmUser (new OctetString ("MD5DES"), AuthMD5. ID, new OctetString ("MD5DESUserAuthPassword"), PrivDES. I D, new OctetString ("MD5DESUserPrivPassword"); target = new UserTarget (); // sets the security level target. setSecurityLevel (SecurityLevel. AUTH_PRIV); target. setSecurityName (new OctetString ("MD5DES"); target. setVersion (SnmpConstants. version3);} else {target = new CommunityTarget (); if (this. version = version1) {target. setVersion (version1); (CommunityTarget) target ). setCommunity (new OctetString ("publi C ");} else {target. setVersion (version2c); (CommunityTarget) target ). setCommunity (new OctetString ("public") ;}} target. setAddress (targetAddres); target. setRetries (2); target. setTimeout (1000); if (sync) {// send the message and receive the response ResponseEvent response = null; try {response = snmp. send (pdu, target);} catch (IOException e) {// TODO Auto-generated catch block e. printStackTrace (); System. out. println (E);} // process the response System. out. println ("Synchronize message from" + response. getPeerAddress () + "/nrequest:" + response. getRequest () + "/nresponse:" + response. getResponse ();} else {ResponseListener listener = new ResponseListener () {@ Override public void onResponse (ResponseEvent event) {if (! Bro) {(Snmp) event. getSource ()). cancel (event. getRequest (), this);} // process the response PDU request = event. getRequest (); PDU response = event. getResponse (); System. out. println ("Asynchronise message from" + event. getPeerAddress () + "/nrequest:" + request + "/nresponse:" + response) ;}; try {snmp. send (pdu, target, null, listener);} catch (IOException e) {e. printStackTrace (); System. out. println (e) ;}}} public class SnmpTest {public static String myVersion = ""; static boolean sync = false; static boolean bro = false; /*** main function * @ param args */public static void main (String [] args) {SnmpManager manager = new SnmpManager (SnmpConstants. version2c); // construct a message PDU pdu = new PDU (); // PDU pdu = new ScopedPDU (); version3 use // to set the id oid oids = new OID ("1.3.6.1.2.1.1.1.0"); // OID oids = new OID (new int [] {1, 3, 6, 1, 2, 1, 1, 1, 0}); pdu. add (new VariableBinding (oids); // sets the Message Type pdu. setType (PDU. GET); // (ScopedPDU) pdu ). setContextName (new OctetString ("priv"); // The last one is the target manager address to be sent. sendMsg (true, true, pdu, "udp: 127.0.0.1/161 ");}}
The running result is as follows:
It does not work when I port this Java code to the Android project. I have seen some websites such as Baidu, google, and stackoverflow. A friend on stackoverflow also encountered the same problem as me. Someone replied that snmp4j could not be used on Android. Why, I can't explain it to anyone. I need more powerful people!
Because it is a project of a Huawei instructor, I can't keep searching for related information like this. I think someone should have made an Android version similar to snmp4j, and I am very sorry. Bytes.
Private void buttonFun () {noNull (); new Thread (new Runnable () {@ Overridepublic void run () {// TODO Auto-generated method stubapi = new SnmpAPI (); session = new SnmpSession (api); SnmpPDU pdu = new SnmpPDU (); UDPProtocolOptions protocol = new UDPProtocolOptions (); protocol. setRemoteHost (getText (target); protocol. setRemotePort (Integer. parseInt (getText (port); pdu. setProtocolOptions (protocol); // Build Get request PDUpdu. setCommand (SnmpAPI. GET_REQ_MSG); pdu. setCommunity (getText (community); if (version. getSelectedItemPosition () = 0) {pdu. setVersion (SnmpAPI. SNMP_VERSION_1);} else if (version. getSelectedItemPosition () = 1) {pdu. setVersion (SnmpAPI. SNMP_VERSION_2C);} else if (version. getSelectedItemPosition () = 2) {// you also need to set the MD5 SHA authentication password and other pdu. setVersion (SnmpAPI. SNMP_VERSION_3);} pdu. setTimeout (Integer. parseInt (getText (timeout); pdu. setRetries (Integer. parseInt (getText (retries); // SnmpOID oid = new SnmpOID ("1.3.6.1.2.1.1.1.0"); // SnmpOID oid = new SnmpOID (SnmpOID) (getText (oid); // pdu. addNull (oid); String oidstr = getText (oid); String str [] = oidstr. split ("\\. "); int a [] = new int [9]; for (int I = 0; I
To do this, I contacted Huawei's teachers. They told me that they were busy a year ago and that they would not be able to discuss the demand business in detail until the year later. I also temporarily shelved this project to do other things. Maybe there is a new situation. I will write the second article, hoping to help you.