<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>SNMP4J &#8211; Jitendra Zaa</title>
	<atom:link href="https://www.jitendrazaa.com/blog/tag/snmp4j/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.jitendrazaa.com/blog</link>
	<description>AI, Salesforce, ServiceNow &#38; Enterprise Tech Guides</description>
	<lastBuildDate>Tue, 24 Mar 2015 17:18:07 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.4</generator>
<site xmlns="com-wordpress:feed-additions:1">87744916</site><atom:link rel="search" type="application/opensearchdescription+xml" title="Search Jitendra Zaa" href="https://www.jitendrazaa.com/blog/wp-json/opensearch/1.1/document" />	<item>
		<title>Generating TRAP in SNMP using SNMP4j</title>
		<link>https://www.jitendrazaa.com/blog/java/snmp/generating-trap-in-snmp-using-snmp4j/</link>
					<comments>https://www.jitendrazaa.com/blog/java/snmp/generating-trap-in-snmp-using-snmp4j/#comments</comments>
		
		<dc:creator><![CDATA[Jitendra]]></dc:creator>
		<pubDate>Wed, 23 Feb 2011 19:37:23 +0000</pubDate>
				<category><![CDATA[SNMP]]></category>
		<category><![CDATA[JAVA]]></category>
		<category><![CDATA[SNMP4J]]></category>
		<guid isPermaLink="false">http://JitendraZaa.com/blog/?p=1582</guid>

					<description><![CDATA[Generating TRAP in SNMP using SNMP4j]]></description>
										<content:encoded><![CDATA[<p>As we have discussed, Traps are like events. This is a way the SNMP agent pushes the information to client. Asynchronously, events can be fired to client from server (agent).</p>
<p><span id="more-1582"></span> Example:</p>
<p><strong>SNMP Trap Receiver:</strong></p>
<pre class="brush: java; title: ; notranslate">
package com.G2.SNMP.Trap.receiver;

import java.io.IOException;

import org.snmp4j.CommandResponder;
import org.snmp4j.CommandResponderEvent;
import org.snmp4j.CommunityTarget;
import org.snmp4j.MessageDispatcher;
import org.snmp4j.MessageDispatcherImpl;
import org.snmp4j.MessageException;
import org.snmp4j.PDU;
import org.snmp4j.Snmp;
import org.snmp4j.mp.MPv1;
import org.snmp4j.mp.MPv2c;
import org.snmp4j.mp.StateReference;
import org.snmp4j.mp.StatusInformation;
import org.snmp4j.security.Priv3DES;
import org.snmp4j.security.SecurityProtocols;
import org.snmp4j.smi.OctetString;
import org.snmp4j.smi.TcpAddress;
import org.snmp4j.smi.TransportIpAddress;
import org.snmp4j.smi.UdpAddress;
import org.snmp4j.transport.AbstractTransportMapping;
import org.snmp4j.transport.DefaultTcpTransportMapping;
import org.snmp4j.transport.DefaultUdpTransportMapping;
import org.snmp4j.util.MultiThreadedMessageDispatcher;
import org.snmp4j.util.ThreadPool;

public class TrapReceiver implements CommandResponder {
	public static void main(String&#x5B;] args) {
		TrapReceiver snmp4jTrapReceiver = new TrapReceiver();
		try {
			snmp4jTrapReceiver.listen(new UdpAddress(&quot;localhost/162&quot;));
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * Trap Listner
	 */
	public synchronized void listen(TransportIpAddress address)
			throws IOException {
		AbstractTransportMapping transport;
		if (address instanceof TcpAddress) {
			transport = new DefaultTcpTransportMapping((TcpAddress) address);
		} else {
			transport = new DefaultUdpTransportMapping((UdpAddress) address);
		}

		ThreadPool threadPool = ThreadPool.create(&quot;DispatcherPool&quot;, 10);
		MessageDispatcher mDispathcher = new MultiThreadedMessageDispatcher(
				threadPool, new MessageDispatcherImpl());

		// add message processing models
		mDispathcher.addMessageProcessingModel(new MPv1());
		mDispathcher.addMessageProcessingModel(new MPv2c());

		// add all security protocols
		SecurityProtocols.getInstance().addDefaultProtocols();
		SecurityProtocols.getInstance().addPrivacyProtocol(new Priv3DES());

		// Create Target
		CommunityTarget target = new CommunityTarget();
		target.setCommunity(new OctetString(&quot;public&quot;));

		Snmp snmp = new Snmp(mDispathcher, transport);
		snmp.addCommandResponder(this);

		transport.listen();
		System.out.println(&quot;Listening on &quot; + address);

		try {
			this.wait();
		} catch (InterruptedException ex) {
			Thread.currentThread().interrupt();
		}
	}

	/**
	 * This method will be called whenever a pdu is received on the given port
	 * specified in the listen() method
	 */
	public synchronized void processPdu(CommandResponderEvent cmdRespEvent) {
		System.out.println(&quot;Received PDU...&quot;);
		PDU pdu = cmdRespEvent.getPDU();
		if (pdu != null) {
			System.out.println(&quot;Trap Type = &quot; + pdu.getType());
			System.out.println(&quot;Variables = &quot; + pdu.getVariableBindings());
			}
		}
	}
</pre>
<p><strong>Version 1 Trap Sender:</strong></p>
<pre class="brush: java; title: ; notranslate">
package com.G2.SNMP.Trap.Sender;

import org.snmp4j.CommunityTarget;
import org.snmp4j.PDU;
import org.snmp4j.PDUv1;
import org.snmp4j.Snmp;
import org.snmp4j.TransportMapping;
import org.snmp4j.mp.SnmpConstants;
import org.snmp4j.smi.IpAddress;
import org.snmp4j.smi.OID;
import org.snmp4j.smi.OctetString;
import org.snmp4j.smi.UdpAddress;
import org.snmp4j.transport.DefaultUdpTransportMapping;

public class TrapSenderVersion1 {

	public static final String community = &quot;public&quot;;

	// Sending Trap for sysLocation of RFC1213
	public static final String Oid = &quot;.1.3.6.1.2.1.1.8&quot;;

	//IP of Local Host
	public static final String ipAddress = &quot;127.0.0.1&quot;;

	//Ideally Port 162 should be used to send receive Trap, any other available Port can be used
	public static final int port = 162;

	public static void main(String&#x5B;] args) {
		TrapSenderVersion1 trapV1 = new TrapSenderVersion1();
		trapV1.sendTrap_Version1();
	}
	/**
	 * This methods sends the V1 trap to the Localhost in port 162
	 */
	public void sendTrap_Version1() {
		try {
			// Create Transport Mapping
			TransportMapping transport = new DefaultUdpTransportMapping();
			transport.listen();

			// Create Target
			CommunityTarget cTarget = new CommunityTarget();
			cTarget.setCommunity(new OctetString(community));
			cTarget.setVersion(SnmpConstants.version1);
			cTarget.setAddress(new UdpAddress(ipAddress + &quot;/&quot; + port));
			cTarget.setTimeout(5000);
			cTarget.setRetries(2);

			PDUv1 pdu = new PDUv1();
			pdu.setType(PDU.V1TRAP);
			pdu.setEnterprise(new OID(Oid));
			pdu.setGenericTrap(PDUv1.ENTERPRISE_SPECIFIC);
			pdu.setSpecificTrap(1);
			pdu.setAgentAddress(new IpAddress(ipAddress));

			// Send the PDU
			Snmp snmp = new Snmp(transport);
			System.out.println(&quot;Sending V1 Trap... Check Wheather NMS is Listening or not? &quot;);
			snmp.send(pdu, cTarget);
			snmp.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
</pre>
<p><strong>Output:</strong></p>
<blockquote><p>Listening on 127.0.0.1/162<br />
Received PDU&#8230;<br />
Trap Type = -92<br />
Variables = []</p></blockquote>
<p><strong>Version 2 Trap Sender:</strong></p>
<pre class="brush: java; title: ; notranslate">
package com.G2.SNMP.Trap.Sender;

import java.util.Date;

import org.snmp4j.CommunityTarget;
import org.snmp4j.PDU;
import org.snmp4j.Snmp;
import org.snmp4j.TransportMapping;
import org.snmp4j.mp.SnmpConstants;
import org.snmp4j.smi.IpAddress;
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;

public class TrapSenderVersion2 {

	public static final String community = &quot;public&quot;;

	// Sending Trap for sysLocation of RFC1213
	public static final String Oid = &quot;.1.3.6.1.2.1.1.8&quot;;

	//IP of Local Host
	public static final String ipAddress = &quot;127.0.0.1&quot;;

	//Ideally Port 162 should be used to send receive Trap, any other available Port can be used
	public static final int port = 162;

	public static void main(String&#x5B;] args) {
		TrapSenderVersion2 trapV2 = new TrapSenderVersion2();
		trapV2.sendTrap_Version2();
	}
	/**
	 * This methods sends the V1 trap to the Localhost in port 162
	 */
	public void sendTrap_Version2() {
		try {
			// Create Transport Mapping
			TransportMapping transport = new DefaultUdpTransportMapping();
			transport.listen();

			// Create Target
			CommunityTarget cTarget = new CommunityTarget();
			cTarget.setCommunity(new OctetString(community));
			cTarget.setVersion(SnmpConstants.version2c);
			cTarget.setAddress(new UdpAddress(ipAddress + &quot;/&quot; + port));
			cTarget.setRetries(2);
			cTarget.setTimeout(5000);

			// Create PDU for V2
			PDU pdu = new PDU();

			// need to specify the system up time
			pdu.add(new VariableBinding(SnmpConstants.sysUpTime,
					new OctetString(new Date().toString())));
			pdu.add(new VariableBinding(SnmpConstants.snmpTrapOID, new OID(
					Oid)));
			pdu.add(new VariableBinding(SnmpConstants.snmpTrapAddress,
					new IpAddress(ipAddress)));

			pdu.add(new VariableBinding(new OID(Oid), new OctetString(
					&quot;Major&quot;)));
			pdu.setType(PDU.NOTIFICATION);

			// Send the PDU
			Snmp snmp = new Snmp(transport);
			System.out.println(&quot;Sending V2 Trap... Check Wheather NMS is Listening or not? &quot;);
			snmp.send(pdu, cTarget);
			snmp.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
</pre>
<p><strong>Output:</strong></p>
<blockquote><p>Listening on 127.0.0.1/162<br />
Received PDU&#8230;<br />
Trap Type = -89<br />
Variables = [1.3.6.1.2.1.1.3.0 = Wed Feb 16 01:17:02 IST 2011, 1.3.6.1.6.3.1.1.4.1.0 = 1.3.6.1.2.1.1.6, 1.3.6.1.6.3.18.1.3.0 = 127.0.0.1, 1.3.6.1.2.1.1.6 = Major]</p></blockquote>
]]></content:encoded>
					
					<wfw:commentRss>https://www.jitendrazaa.com/blog/java/snmp/generating-trap-in-snmp-using-snmp4j/feed/</wfw:commentRss>
			<slash:comments>22</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">1582</post-id>	</item>
		<item>
		<title>Creating SNMP Agent (Server) in JAVA using SNMP4j</title>
		<link>https://www.jitendrazaa.com/blog/java/snmp/creating-snmp-agent-server-in-java-using-snmp4j/</link>
					<comments>https://www.jitendrazaa.com/blog/java/snmp/creating-snmp-agent-server-in-java-using-snmp4j/#comments</comments>
		
		<dc:creator><![CDATA[Jitendra]]></dc:creator>
		<pubDate>Wed, 23 Feb 2011 18:44:18 +0000</pubDate>
				<category><![CDATA[SNMP]]></category>
		<category><![CDATA[JAVA]]></category>
		<category><![CDATA[SNMP4J]]></category>
		<guid isPermaLink="false">http://JitendraZaa.com/blog/?p=1576</guid>

					<description><![CDATA[Creating SNMP Agent (Server) in JAVA using SNMP4j]]></description>
										<content:encoded><![CDATA[<p>In Previous article, we have seen that how to <a title="Create SNMP Client in JAVA using SNMP4j" href="https://jitendrazaa.com/blog/java/snmp/create-snmp-client-in-java-using-snmp4j/" target="_blank">create SNMP client in JAVA using SNMP4j</a>.</p>
<p style="text-align: justify;">To create the Agent for SNMP which listens for the request should extend the abstract class <strong>BaseAgent</strong>.<br />
The BaseAgent abstract class defines a framework for writing SNMP agents using the <strong>SNMP4J-Agent API</strong>. To implement your own SNMP agent, extend this class and implement the abstract methods defined by BaseAgent. The hook methods do not need any specific implementation. They only provide a defined mechanism to customize your agent.</p>
<p><strong><span id="more-1576"></span>Below Class is used to create the SNMP Agent:</strong></p>
<pre class="brush: java; title: ; notranslate">
package com.G2.SNMP.Server;

import java.io.File;
import java.io.IOException;

import org.snmp4j.TransportMapping;
import org.snmp4j.agent.BaseAgent;
import org.snmp4j.agent.CommandProcessor;
import org.snmp4j.agent.DuplicateRegistrationException;
import org.snmp4j.agent.MOGroup;
import org.snmp4j.agent.ManagedObject;
import org.snmp4j.agent.mo.MOTableRow;
import org.snmp4j.agent.mo.snmp.RowStatus;
import org.snmp4j.agent.mo.snmp.SnmpCommunityMIB;
import org.snmp4j.agent.mo.snmp.SnmpNotificationMIB;
import org.snmp4j.agent.mo.snmp.SnmpTargetMIB;
import org.snmp4j.agent.mo.snmp.StorageType;
import org.snmp4j.agent.mo.snmp.VacmMIB;
import org.snmp4j.agent.security.MutableVACM;
import org.snmp4j.mp.MPv3;
import org.snmp4j.security.SecurityLevel;
import org.snmp4j.security.SecurityModel;
import org.snmp4j.security.USM;
import org.snmp4j.smi.Address;
import org.snmp4j.smi.GenericAddress;
import org.snmp4j.smi.Integer32;
import org.snmp4j.smi.OID;
import org.snmp4j.smi.OctetString;
import org.snmp4j.smi.Variable;
import org.snmp4j.transport.TransportMappings;

public class SNMPAgent extends BaseAgent {

	private String address;

	/**
	 *
	 * @param address
	 * @throws IOException
	 */
	public SNMPAgent(String address) throws IOException {

		/**
		 * Creates a base agent with boot-counter, config file, and a
		 * CommandProcessor for processing SNMP requests. Parameters:
		 * &quot;bootCounterFile&quot; - a file with serialized boot-counter information
		 * (read/write). If the file does not exist it is created on shutdown of
		 * the agent. &quot;configFile&quot; - a file with serialized configuration
		 * information (read/write). If the file does not exist it is created on
		 * shutdown of the agent. &quot;commandProcessor&quot; - the CommandProcessor
		 * instance that handles the SNMP requests.
		 */
		super(new File(&quot;conf.agent&quot;), new File(&quot;bootCounter.agent&quot;),
				new CommandProcessor(
						new OctetString(MPv3.createLocalEngineID())));
		this.address = address;
	}

	/**
	 * Adds community to security name mappings needed for SNMPv1 and SNMPv2c.
	 */
	@Override
	protected void addCommunities(SnmpCommunityMIB communityMIB) {
		Variable&#x5B;] com2sec = new Variable&#x5B;] { new OctetString(&quot;public&quot;),
				new OctetString(&quot;cpublic&quot;), // security name
				getAgent().getContextEngineID(), // local engine ID
				new OctetString(&quot;public&quot;), // default context name
				new OctetString(), // transport tag
				new Integer32(StorageType.nonVolatile), // storage type
				new Integer32(RowStatus.active) // row status
		};
		MOTableRow row = communityMIB.getSnmpCommunityEntry().createRow(
				new OctetString(&quot;public2public&quot;).toSubIndex(true), com2sec);
		communityMIB.getSnmpCommunityEntry().addRow(row);

	}

	/**
	 * Adds initial notification targets and filters.
	 */
	@Override
	protected void addNotificationTargets(SnmpTargetMIB arg0,
			SnmpNotificationMIB arg1) {
		// TODO Auto-generated method stub

	}

	/**
	 * Adds all the necessary initial users to the USM.
	 */
	@Override
	protected void addUsmUser(USM arg0) {
		// TODO Auto-generated method stub

	}

	/**
	 * Adds initial VACM configuration.
	 */
	@Override
	protected void addViews(VacmMIB vacm) {
		vacm.addGroup(SecurityModel.SECURITY_MODEL_SNMPv2c, new OctetString(
				&quot;cpublic&quot;), new OctetString(&quot;v1v2group&quot;),
				StorageType.nonVolatile);

		vacm.addAccess(new OctetString(&quot;v1v2group&quot;), new OctetString(&quot;public&quot;),
				SecurityModel.SECURITY_MODEL_ANY, SecurityLevel.NOAUTH_NOPRIV,
				MutableVACM.VACM_MATCH_EXACT, new OctetString(&quot;fullReadView&quot;),
				new OctetString(&quot;fullWriteView&quot;), new OctetString(
						&quot;fullNotifyView&quot;), StorageType.nonVolatile);

		vacm.addViewTreeFamily(new OctetString(&quot;fullReadView&quot;), new OID(&quot;1.3&quot;),
				new OctetString(), VacmMIB.vacmViewIncluded,
				StorageType.nonVolatile);

	}

	/**
	 * Unregister the basic MIB modules from the agent's MOServer.
	 */
	@Override
	protected void unregisterManagedObjects() {
		// TODO Auto-generated method stub

	}

	/**
	 * Register additional managed objects at the agent's server.
	 */
	@Override
	protected void registerManagedObjects() {
		// TODO Auto-generated method stub

	}

	protected void initTransportMappings() throws IOException {
		transportMappings = new TransportMapping&#x5B;1];
		Address addr = GenericAddress.parse(address);
		TransportMapping tm = TransportMappings.getInstance()
				.createTransportMapping(addr);
		transportMappings&#x5B;0] = tm;
	}

	/**
	 * Start method invokes some initialization methods needed to start the
	 * agent
	 *
	 * @throws IOException
	 */
	public void start() throws IOException {

		init();
		// This method reads some old config from a file and causes
		// unexpected behavior.
		// loadConfig(ImportModes.REPLACE_CREATE);
		addShutdownHook();
		getServer().addContext(new OctetString(&quot;public&quot;));
		finishInit();
		run();
		sendColdStartNotification();
	}

	/**
	 * Clients can register the MO they need
	 */
	public void registerManagedObject(ManagedObject mo) {
		try {
			server.register(mo, null);
		} catch (DuplicateRegistrationException ex) {
			throw new RuntimeException(ex);
		}
	}

	public void unregisterManagedObject(MOGroup moGroup) {
		moGroup.unregisterMOs(server, getContext(moGroup));
	}

}
</pre>
<p><strong> This class is used to create ManagedObject which is used by the SNMP agent Class created above:</strong></p>
<pre class="brush: java; title: ; notranslate">
package com.G2.SNMP.Server;

import org.snmp4j.agent.mo.MOAccessImpl;
import org.snmp4j.agent.mo.MOScalar;
import org.snmp4j.smi.OID;
import org.snmp4j.smi.OctetString;
import org.snmp4j.smi.Variable;

/**
 * This class creates and returns ManagedObjects
 * @author Shiva
 *
 */
public class MOCreator {
	public static MOScalar createReadOnly(OID oid,Object value ){
		return new MOScalar(oid,
				MOAccessImpl.ACCESS_READ_ONLY,
				getVariable(value));
	}

	private static Variable getVariable(Object value) {
		if(value instanceof String) {
			return new OctetString((String)value);
		}
		throw new IllegalArgumentException(&quot;Unmanaged Type: &quot; + value.getClass());
	}

}
</pre>
<p><strong> Below class tests that agent is working properly or not :</strong></p>
<pre class="brush: java; title: ; notranslate">
package com.G2.SNMP.Server;

import java.io.IOException;

import org.snmp4j.smi.OID;

import com.G2.SNMP.client.SNMPManager;

public class TestSNMPAgent {

	static final OID sysDescr = new OID(&quot;.1.3.6.1.2.1.1.1.0&quot;);

	public static void main(String&#x5B;] args) throws IOException {
		TestSNMPAgent client = new TestSNMPAgent(&quot;udp:127.0.0.1/161&quot;);
		client.init();
	}

	SNMPAgent agent = null;
	/**
	 * This is the client which we have created earlier
	 */
	SNMPManager client = null;

	String address = null;

	/**
	 * Constructor
	 *
	 * @param add
	 */
	public TestSNMPAgent(String add) {
		address = add;
	}

	private void init() throws IOException {
		agent = new SNMPAgent(&quot;0.0.0.0/2001&quot;);
		agent.start();

		// Since BaseAgent registers some MIBs by default we need to unregister
		// one before we register our own sysDescr. Normally you would
		// override that method and register the MIBs that you need
		agent.unregisterManagedObject(agent.getSnmpv2MIB());

		// Register a system description, use one from you product environment
		// to test with
		agent.registerManagedObject(MOCreator.createReadOnly(sysDescr,
				&quot;This Description is set By ShivaSoft&quot;));

		// Setup the client to use our newly started agent
		client = new SNMPManager(&quot;udp:127.0.0.1/2001&quot;);
		client.start();
		// Get back Value which is set
		System.out.println(client.getAsString(sysDescr));
	}

}
</pre>
<p><strong> Output :</strong></p>
<blockquote><p>This Description is set By ShivaSoft</p></blockquote>
<p><strong>Possible run time errors :</strong><br />
Few of you may receive error like</p>
<blockquote><p>&#8220;Exception in thread &#8220;main&#8221; java.lang.RuntimeException: java.net.BindException: Address already in use: Cannot bind&#8221;.</p></blockquote>
<p><strong>Solution:</strong> You are trying to listen on a local IP and port which is already in use by some other process (for example the operating system &#8211; if you use port 161 this is rather likely).</p>
<p>Try to use a different port (or IP address &#8211; but most services listen on all local IP addresses) or stop the process that is using it.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.jitendrazaa.com/blog/java/snmp/creating-snmp-agent-server-in-java-using-snmp4j/feed/</wfw:commentRss>
			<slash:comments>34</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">1576</post-id>	</item>
		<item>
		<title>Create SNMP Client in JAVA Using SNMP4j</title>
		<link>https://www.jitendrazaa.com/blog/java/snmp/create-snmp-client-in-java-using-snmp4j/</link>
					<comments>https://www.jitendrazaa.com/blog/java/snmp/create-snmp-client-in-java-using-snmp4j/#comments</comments>
		
		<dc:creator><![CDATA[Jitendra]]></dc:creator>
		<pubDate>Wed, 23 Feb 2011 17:22:06 +0000</pubDate>
				<category><![CDATA[SNMP]]></category>
		<category><![CDATA[JAVA]]></category>
		<category><![CDATA[SNMP4J]]></category>
		<guid isPermaLink="false">http://JitendraZaa.com/blog/?p=1571</guid>

					<description><![CDATA[Create SNMP Client in JAVA Using SNMP4j]]></description>
										<content:encoded><![CDATA[<p>There are lots of open source library for SNMP is available, even java have library for the same,  But 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>
<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>
<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>
<p><strong>Create Simple NMS (Network management station / Client) in JAVA using SNMP4j:<span id="more-1571"></span></strong></p>
<pre class="brush: java; title: ; notranslate">

package com.G2.SNMP.client;

import java.io.IOException;

import org.snmp4j.CommunityTarget;
import org.snmp4j.PDU;
import org.snmp4j.Snmp;
import org.snmp4j.Target;
import org.snmp4j.TransportMapping;
import org.snmp4j.event.ResponseEvent;
import org.snmp4j.mp.SnmpConstants;
import org.snmp4j.smi.Address;
import org.snmp4j.smi.GenericAddress;
import org.snmp4j.smi.OID;
import org.snmp4j.smi.OctetString;
import org.snmp4j.smi.VariableBinding;
import org.snmp4j.transport.DefaultUdpTransportMapping;

public class SNMPManager {

Snmp snmp = null;
String address = null;

/**
* Constructor
* @param add
*/
public SNMPManager(String add)
{
address = add;
}

public static void main(String&#x5B;] args) throws IOException {
/**
* Port 161 is used for Read and Other operations
* Port 162 is used for the trap generation
*/
SNMPManager client = new SNMPManager(&quot;udp:127.0.0.1/161&quot;);
client.start();
/**
* OID - .1.3.6.1.2.1.1.1.0 =&gt; SysDec
* OID - .1.3.6.1.2.1.1.5.0 =&gt; SysName
* =&gt; MIB explorer will be usefull here, as discussed in previous article
*/
String sysDescr = client.getAsString(new OID(&quot;.1.3.6.1.2.1.1.1.0&quot;));
System.out.println(sysDescr);
}

/**
* Start the Snmp session. If you forget the listen() method you will not
* get any answers because the communication is asynchronous
* and the listen() method listens for answers.
* @throws IOException
*/
private void start() throws IOException {
TransportMapping transport = new DefaultUdpTransportMapping();
snmp = new Snmp(transport);
// Do not forget this line!
transport.listen();
}

/**
* Method which takes a single OID and returns the response from the agent as a String.
* @param oid
* @return
* @throws IOException
*/
public String getAsString(OID oid) throws IOException {
ResponseEvent event = get(new OID&#x5B;] { oid });
return event.getResponse().get(0).getVariable().toString();
}

/**
* This method is capable of handling multiple OIDs
* @param oids
* @return
* @throws IOException
*/
public ResponseEvent get(OID oids&#x5B;]) throws IOException {
PDU pdu = new PDU();
for (OID oid : oids) {
pdu.add(new VariableBinding(oid));
}
pdu.setType(PDU.GET);
ResponseEvent event = snmp.send(pdu, getTarget(), null);
if(event != null) {
return event;
}
throw new RuntimeException(&quot;GET timed out&quot;);
}

/**
* This method returns a Target, which contains information about
* where the data should be fetched and how.
* @return
*/
private Target getTarget() {
Address targetAddress = GenericAddress.parse(address);
CommunityTarget target = new CommunityTarget();
target.setCommunity(new OctetString(&quot;public&quot;));
target.setAddress(targetAddress);
target.setRetries(2);
target.setTimeout(1500);
target.setVersion(SnmpConstants.version2c);
return target;
}

}
</pre>
<p><strong>Output:</strong></p>
<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>
<p>Explanation of every method is documented in above program. In Next article, we will write a Java Program to create SNMP Agent.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.jitendrazaa.com/blog/java/snmp/create-snmp-client-in-java-using-snmp4j/feed/</wfw:commentRss>
			<slash:comments>78</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">1571</post-id>	</item>
	</channel>
</rss>

<!--
Performance optimized by W3 Total Cache. Learn more: https://www.boldgrid.com/w3-total-cache/?utm_source=w3tc&utm_medium=footer_comment&utm_campaign=free_plugin

Page Caching using Disk: Enhanced 
Minified using Disk

Served from: www.jitendrazaa.com @ 2026-04-21 08:43:08 by W3 Total Cache
-->