Android Implementation SNMP Protocol (i)

Source: Internet
Author: User
Tags sendmsg snmp


early January 2015. I got a call from a teacher in  and asked me to help with the app they used at the Beijing show, which can show  network equipment running state and set up some simple parameters, including AP, LSW, AP, AR and so on.
The  teacher told me that they were managed using the V2C version of the SNMP protocol, which meant that I had to implement the Protocol on my Android device and interact with their network devices.
Go back to study carefully and consult some of the more familiar with the small partner, finally to SNMP has some superficial understanding.

SNMP refers to the Simple Network Device Management protocol, as the name implies is a common standard protocol for network equipment management, belongs to the application layer of TCP/IP, SNMP server side occupies 161, the client occupies 162 (based on UDP protocol).
Turn on the SNMP protocol on Windows to refer to http://blog.csdn.net/zougangx/article/details/6977936. It is important to note that the SNMP service properties in the Security tab are set to accept SNMP packets from any host so that we can debug.

The protocol uses the OID (object identifier) as the content of the query between the two devices that have already established the connection, and the OID content can refer to http://www.cnblogs.com/aspx-net/p/3554044.html. The OID is part of the protocol definition, and some device vendors can define it themselves.
Complete the above steps, and familiar with the basic OID instructions, online have to write a friend said you can use Paessler SNMP tester debugging, but I did not in the actual operation so smooth, Paessler SNMP tester has been shown noresponse, Use Snmputil instead. (Paessler SNMP Tester and Snmputil are both tools of the Windows-side test SNMP protocol, Paessler SNMP tester has a graphical interface, Snmputil No, operation on Snmputil can refer to http:/ /blog.chinaunix.net/uid-21857285-id-3340217.html)
Error on SnmpMgrRequest 40 errors when using Snmputil, refer to the following URL to be resolved: http://blog.csdn.net/wqjsir/article/details/ 8472006, the Traps tab is configured in this article. At this point, my snmputil and Paessler SNMP tester to run normally!
in the list of services for your computer, you can see:

Where the trap message needs to be manually opened, and the service is turned on automatically.As for how the SNMP trap service is used, the settings for the Trap tab of the SNMP service I am not very clear, but also want someone to know the words of the liberal enlighten, temporarily do not affect my project also did not go into the study.
When all two services are turned on, you can use NETSTAT-AN|FINDSTR "162" or netstat-an|findstr "161" to see if the port is being developed, and 161 will be ready for local testing after it has been opened.


SNMP protocol is the TCP/IP protocol, is completed in C-system language, I have previously transplanted uip1.0 is written in C language. And Android must use Java to achieve, for this, I first used the snmp4j this jar package, built a Java project, imitation Official document example, coding as follows (need to introduce snmp4j two jar package):
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;            /** * Construction Method * @param version */public snmpmanager (int version) {this. Version = version;                 try {//Set to UDP protocol transportmapping = new defaultudptransportmapping ();                 SNMP = new SNMP (transportmapping); if (Version = = Version3) {//set security Information USM = New USM (Securityprotocols.getinsta                     NCE (), 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 * Send Message method */Public void Sendmsg (Boolean sync, Final boolean bro, PDU PDU, String addr) {Address targetaddres = genericaddress. pa           RSE (addr);            Target target = null; if (this. Version = = Version3) {SNMP.GETUSM (). AddUser (New octetstring ("Md5des"), New Usmuser (New OCTE Tstring ("Md5des"), AuthMD5.                ID, New OctetString ("Md5desuserauthpassword"), Privdes.id, New OctetString ("Md5desuserprivpassword"));                 target = new Usertarget (); Set 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 ("public"));                     } else {target.setversion (VERSION2C);                ((communitytarget) target). Setcommunity (New octetstring ("public"));           }} target.setaddress (Targetaddres);           Target.setretries (2);            Target.settimeout (1000);                 if (sync) {//sends a message and accepts a response responseevent response = null;                try {response = Snmp.send (PDU, target);                      } catch (IOException e) {//TODO auto-generated catch block E.printstacktrace (); System.                Out.println (e); }//Processing 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);                           }//Processing 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 the message PDU PDU = new PDU (); PDU PDU = new SCOPEDPDU (); Version3 use//Set the object ID oid OIDs = new oid ("1.3.6.1.2.1.1.1.0") to get,//oid OIDs = new OID (New I           NT [] {1, 3, 6, 1, 2, 1, 1, 1, 0});            Pdu.add (New variablebinding (OIDs)); Set the message type Pdu.settype (PDU.             GET);//((SCOPEDPDU) PDU). Setcontextname (New octetstring ("Priv"));                Send a message where the last one is the destination address that you want to send Manager.sendmsg (true, True, PDU, "udp:127.0.0.1/161"); }}

The results of the operation are as follows:

By the time I ported this Java code to Android, it didn't work. I Baidu, Google, StackOverflow and other websites have seen, in StackOverflow a friend is also encountered and I have the same problem, someone reply snmp4j can not be used on Android, why, I can not explain to everyone listen, You need someone more powerful!
Because it is  Teacher's project, I can not so kicks, continue to find relevant information, I think someone should have made a similar snmp4j version of the Android, Kung Fu. Https://www.webnms.com/snmpapi-android/index.html This site's closed source package provides this feature, as a developer of SNMP, it may be useful to study, I am using this site's development package, the SNMP protocol was completed.


Android Implementation SNMP Protocol (i)


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.