{"id":1571,"date":"2011-02-23T22:52:06","date_gmt":"2011-02-23T17:22:06","guid":{"rendered":"http:\/\/JitendraZaa.com\/blog\/?p=1571"},"modified":"2015-03-24T14:37:39","modified_gmt":"2015-03-24T14:37:39","slug":"create-snmp-client-in-java-using-snmp4j","status":"publish","type":"post","link":"https:\/\/www.jitendrazaa.com\/blog\/java\/snmp\/create-snmp-client-in-java-using-snmp4j\/","title":{"rendered":"Create SNMP Client in JAVA Using SNMP4j"},"content":{"rendered":"<p>There are lots of open source library for SNMP is available, even java have library for the same, \u00a0But in this article I will explain a simple example of using SNMP4j in JAVA to create a simple client which will display the hardware information.<\/p>\n<p><strong>Note : <\/strong>To run this program, SNMP service must be installed. <a title=\"Install SNMP in Windows XP\" href=\"https:\/\/www.jitendrazaa.com\/blog\/java\/snmp\/install-snmp-service-in-windows-xp-and-overview-of-mib-explorer\/\" target=\"_blank\">Check this article to know that how to install SNMP in Windows XP.<\/a><\/p>\n<p>Download libraries from <a title=\"Download SNMP4j Library\" href=\"http:\/\/www.snmp4j.org\/html\/download.html\" target=\"_blank\">http:\/\/www.snmp4j.org\/html\/download.html<\/a><\/p>\n<p><strong>Create Simple NMS (Network management station \/ Client) in JAVA using SNMP4j:<!--more--><\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\n\r\npackage com.G2.SNMP.client;\r\n\r\nimport java.io.IOException;\r\n\r\nimport org.snmp4j.CommunityTarget;\r\nimport org.snmp4j.PDU;\r\nimport org.snmp4j.Snmp;\r\nimport org.snmp4j.Target;\r\nimport org.snmp4j.TransportMapping;\r\nimport org.snmp4j.event.ResponseEvent;\r\nimport org.snmp4j.mp.SnmpConstants;\r\nimport org.snmp4j.smi.Address;\r\nimport org.snmp4j.smi.GenericAddress;\r\nimport org.snmp4j.smi.OID;\r\nimport org.snmp4j.smi.OctetString;\r\nimport org.snmp4j.smi.VariableBinding;\r\nimport org.snmp4j.transport.DefaultUdpTransportMapping;\r\n\r\npublic class SNMPManager {\r\n\r\nSnmp snmp = null;\r\nString address = null;\r\n\r\n\/**\r\n* Constructor\r\n* @param add\r\n*\/\r\npublic SNMPManager(String add)\r\n{\r\naddress = add;\r\n}\r\n\r\npublic static void main(String&#x5B;] args) throws IOException {\r\n\/**\r\n* Port 161 is used for Read and Other operations\r\n* Port 162 is used for the trap generation\r\n*\/\r\nSNMPManager client = new SNMPManager(&quot;udp:127.0.0.1\/161&quot;);\r\nclient.start();\r\n\/**\r\n* OID - .1.3.6.1.2.1.1.1.0 =&gt; SysDec\r\n* OID - .1.3.6.1.2.1.1.5.0 =&gt; SysName\r\n* =&gt; MIB explorer will be usefull here, as discussed in previous article\r\n*\/\r\nString sysDescr = client.getAsString(new OID(&quot;.1.3.6.1.2.1.1.1.0&quot;));\r\nSystem.out.println(sysDescr);\r\n}\r\n\r\n\/**\r\n* Start the Snmp session. If you forget the listen() method you will not\r\n* get any answers because the communication is asynchronous\r\n* and the listen() method listens for answers.\r\n* @throws IOException\r\n*\/\r\nprivate void start() throws IOException {\r\nTransportMapping transport = new DefaultUdpTransportMapping();\r\nsnmp = new Snmp(transport);\r\n\/\/ Do not forget this line!\r\ntransport.listen();\r\n}\r\n\r\n\/**\r\n* Method which takes a single OID and returns the response from the agent as a String.\r\n* @param oid\r\n* @return\r\n* @throws IOException\r\n*\/\r\npublic String getAsString(OID oid) throws IOException {\r\nResponseEvent event = get(new OID&#x5B;] { oid });\r\nreturn event.getResponse().get(0).getVariable().toString();\r\n}\r\n\r\n\/**\r\n* This method is capable of handling multiple OIDs\r\n* @param oids\r\n* @return\r\n* @throws IOException\r\n*\/\r\npublic ResponseEvent get(OID oids&#x5B;]) throws IOException {\r\nPDU pdu = new PDU();\r\nfor (OID oid : oids) {\r\npdu.add(new VariableBinding(oid));\r\n}\r\npdu.setType(PDU.GET);\r\nResponseEvent event = snmp.send(pdu, getTarget(), null);\r\nif(event != null) {\r\nreturn event;\r\n}\r\nthrow new RuntimeException(&quot;GET timed out&quot;);\r\n}\r\n\r\n\/**\r\n* This method returns a Target, which contains information about\r\n* where the data should be fetched and how.\r\n* @return\r\n*\/\r\nprivate Target getTarget() {\r\nAddress targetAddress = GenericAddress.parse(address);\r\nCommunityTarget target = new CommunityTarget();\r\ntarget.setCommunity(new OctetString(&quot;public&quot;));\r\ntarget.setAddress(targetAddress);\r\ntarget.setRetries(2);\r\ntarget.setTimeout(1500);\r\ntarget.setVersion(SnmpConstants.version2c);\r\nreturn target;\r\n}\r\n\r\n}\r\n<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<blockquote><p>Hardware: x86 Family 6 Model 23 Stepping 10 AT\/AT COMPATIBLE &#8211; Software: Windows 2000 Version 5.1 (Build 2600 Multiprocessor Free)<\/p><\/blockquote>\n<p>Explanation of every method is documented in above program. In Next article, we will write a Java Program to create SNMP Agent.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Create SNMP Client in JAVA Using SNMP4j<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"advanced_seo_description":"","jetpack_seo_html_title":"","jetpack_seo_noindex":false,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"jz_research_post":"","_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[12],"tags":[329,183],"class_list":["post-1571","post","type-post","status-publish","format-standard","hentry","category-snmp","tag-java","tag-snmp4j"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack-related-posts":[{"id":1576,"url":"https:\/\/www.jitendrazaa.com\/blog\/java\/snmp\/creating-snmp-agent-server-in-java-using-snmp4j\/","url_meta":{"origin":1571,"position":0},"title":"Creating SNMP Agent (Server) in JAVA using SNMP4j","author":"Jitendra","date":"February 24, 2011","format":false,"excerpt":"Creating SNMP Agent (Server) in JAVA using SNMP4j","rel":"","context":"In &quot;SNMP&quot;","block_context":{"text":"SNMP","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/java\/snmp\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":1582,"url":"https:\/\/www.jitendrazaa.com\/blog\/java\/snmp\/generating-trap-in-snmp-using-snmp4j\/","url_meta":{"origin":1571,"position":1},"title":"Generating TRAP in SNMP using SNMP4j","author":"Jitendra","date":"February 24, 2011","format":false,"excerpt":"Generating TRAP in SNMP using SNMP4j","rel":"","context":"In &quot;SNMP&quot;","block_context":{"text":"SNMP","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/java\/snmp\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":1554,"url":"https:\/\/www.jitendrazaa.com\/blog\/java\/snmp\/terminologies-used-in-snmp\/","url_meta":{"origin":1571,"position":2},"title":"Terminologies used in SNMP","author":"Jitendra","date":"February 23, 2011","format":false,"excerpt":"Terminologies used in SNMP","rel":"","context":"In &quot;SNMP&quot;","block_context":{"text":"SNMP","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/java\/snmp\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":1548,"url":"https:\/\/www.jitendrazaa.com\/blog\/java\/snmp\/introduction-to-snmp\/","url_meta":{"origin":1571,"position":3},"title":"Introduction to SNMP","author":"Jitendra","date":"February 23, 2011","format":false,"excerpt":"Introduction to SNMP in JAVA","rel":"","context":"In &quot;SNMP&quot;","block_context":{"text":"SNMP","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/java\/snmp\/"},"img":{"alt_text":"Structure of MIB SNMP","src":"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2011\/02\/Structure-of-MIB-SNMP.jpg?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":1563,"url":"https:\/\/www.jitendrazaa.com\/blog\/java\/snmp\/install-snmp-service-in-windows-xp-and-overview-of-mib-explorer\/","url_meta":{"origin":1571,"position":4},"title":"Install SNMP Service in Windows XP and Overview of MIB Explorer","author":"Jitendra","date":"February 23, 2011","format":false,"excerpt":"Install SNMP Service in Windows XP and Overview of MIB Explorer","rel":"","context":"In &quot;SNMP&quot;","block_context":{"text":"SNMP","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/java\/snmp\/"},"img":{"alt_text":"Install SNMP Service in Windows XP","src":"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2011\/02\/Install-SNMP-Service-in-Windows-XP.jpg?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":3556,"url":"https:\/\/www.jitendrazaa.com\/blog\/java\/create-excel-file-in-java-using-apache-poi-library\/","url_meta":{"origin":1571,"position":5},"title":"Create Excel File in Java using Apache POI Library","author":"Jitendra","date":"October 15, 2013","format":false,"excerpt":"Recently, I came across requirement to create ExcelSheet from thin Java Client used by Salesforce. So, I though to share my experience on Creating Excel Sheet in Java. As we know that Java is product of Oracle and Excel is product of Microsoft. Off-course, There will be no standard functionality\u2026","rel":"","context":"In &quot;JAVA&quot;","block_context":{"text":"JAVA","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/java\/"},"img":{"alt_text":"Create Excel File in Java","src":"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2013\/10\/Create-Excel-File-in-Java-300x166.png?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]}],"jetpack_likes_enabled":true,"_links":{"self":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/posts\/1571","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/comments?post=1571"}],"version-history":[{"count":2,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/posts\/1571\/revisions"}],"predecessor-version":[{"id":4347,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/posts\/1571\/revisions\/4347"}],"wp:attachment":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/media?parent=1571"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/categories?post=1571"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/tags?post=1571"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}