Please follow the below mentioned steps to write a very simple SNMP4J application using Java and eclipse IDE.
First you need to download a copy of SNMP4J.jar. You can get it from http://www.snmp4j.org/html/download.html
Then you need to decide how you are going to test your application. Check whether you have access to any real network devices like Cisco routers or Juniper routers or whether you are aware of any SNMP agent running on any of your servers.There is nothing to worry if you cannot find one , you can start SNMP agent on your desktop machine as well. Here I am giving an example of how you can use your Windows machine to test your application by starting the SNMP service on the machine.
I am using a Windows XP machine and below are the steps needed to start SNMP agent on the machine.
1. Go to Start->Control Panel -> Add or Remove Programs -> Add or Remove Windows Components
Select or Check "Management and Monitoring Tools".
new community strings for additional security here.
Below code is almost self explanatory. Please refer to http://www.snmp4j.org/doc/index.html for API reference. You are welcome with questions or comments.
Please note that I have tried snmpget on 'sysDescr' MIB on my Windows machine and got the output "
Hardware: x86 Family 6 Model 23 Stepping 10 AT/AT COMPATIBLE - Software: Windows 2000 Version 5.1 (Build 2600 Multiprocessor Free)". This is Cool.....
First you need to download a copy of SNMP4J.jar. You can get it from http://www.snmp4j.org/html/download.html
Then you need to decide how you are going to test your application. Check whether you have access to any real network devices like Cisco routers or Juniper routers or whether you are aware of any SNMP agent running on any of your servers.There is nothing to worry if you cannot find one , you can start SNMP agent on your desktop machine as well. Here I am giving an example of how you can use your Windows machine to test your application by starting the SNMP service on the machine.
I am using a Windows XP machine and below are the steps needed to start SNMP agent on the machine.
1. Go to Start->Control Panel -> Add or Remove Programs -> Add or Remove Windows Components
Select or Check "Management and Monitoring Tools".
This will start the SNMP Agent running on your windows machine.
2. Verify SNMP agent running on your machine
Go to Start->Control Panel -> Administrative Tools->Services and verify the service. You can also add new community strings for additional security here.
3. Now come to Java coding. Open a Java application project in Eclipse and add the SNMP4J.jar to the
library path.
Below code is almost self explanatory. Please refer to http://www.snmp4j.org/doc/index.html for API reference. You are welcome with questions or comments.
Please note that I have tried snmpget on 'sysDescr' MIB on my Windows machine and got the output "
Hardware: x86 Family 6 Model 23 Stepping 10 AT/AT COMPATIBLE - Software: Windows 2000 Version 5.1 (Build 2600 Multiprocessor Free)". This is Cool.....
import java.io.IOException;
import java.util.Vector;
import org.snmp4j.CommunityTarget;
import org.snmp4j.PDU;
import org.snmp4j.Snmp;
import org.snmp4j.smi.*;
import org.snmp4j.mp.*;
import org.snmp4j.transport.DefaultUdpTransportMapping;
import org.snmp4j.event.ResponseEvent;
import org.snmp4j.util.*;
public class Collector
{
public static void main(String[] args) {
try {
Snmp snmp4j = new Snmp(new DefaultUdpTransportMapping());
snmp4j.listen();
Address add = new UdpAddress("192.168.1.132"+"/"+"161");
CommunityTarget target = new CommunityTarget();
target.setAddress(add);
target.setTimeout(500);
target.setRetries(3);
target.setCommunity(new OctetString("public"));
target.setVersion(SnmpConstants.version2c);
PDU request = new PDU();
request.setType(PDU.GET);
OID oid= new OID(".1.3.6.1.2.1.1.1.0");
request.add(new VariableBinding(oid));
PDU responsePDU=null;
ResponseEvent responseEvent;
responseEvent = snmp4j.send(request, target);
if (responseEvent != null)
{
responsePDU = responseEvent.getResponse();
if ( responsePDU != null)
{
Vector <VariableBinding> tmpv = responsePDU.getVariableBindings();
if(tmpv != null)
{
for(int k=0; k <tmpv.size();k++)
{
VariableBinding vb = (VariableBinding) tmpv.get(k);
String output = null;
if ( vb.isException())
{
String errorstring = vb.getVariable().getSyntaxString();
System.out.println("Error:"+errorstring);
}
else
{
String sOid = vb.getOid().toString();
Variable var = vb.getVariable();
OctetString oct = new OctetString((OctetString)var);
String sVar = oct.toString();
System.out.println("success:"+sVar);
}
}
}
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
Hi,
ReplyDeleteHow to do this in ubuntu Os
Regards
Hari
Helo
ReplyDeletereply can any one give me answer for my above qus?
Hi,
DeleteThe java program would work in Ubuntu as well but if your question is more like how to test it in Ubuntu OS then you need to install snmp on the machine.
More details can be fould on this blog: http://wmunguiam.blogspot.com/2009/07/howto-install-snmp-and-test-snmpv1.html
Hi Mathew,
ReplyDeleteThanku for response.
I have some question's regarding snmp4j ,( I am new snap and snmp4j).
1.is it possible to connect X host to Y
host.
Thanks&Regards
Hari.
2. how to convert custom erros as a snmp traps and how send it X host to Y host.
3.if possible what is the setup to do this.
Hi Mathew,
ReplyDelete1)Is it possible to send customized erros as a snmp traps from X host to Y host.. using snmp4j.?(i am new for snmp and snmp4j)
2.if possible what setup needed here.?
3.what type of information contains an snmp trap .
4.suppose i have one web application this application aim is,
The application gets the alerts(errors) data from the devices stores into local Db using web services .now if we want convert the these alerts data as an snmptrap .(is it possible to convert this using snmp4j)
and sends these data from one host to another host.?
Thanks&Regards
Hari
Hi,
ReplyDeleteCould respond my posts pls.
Hi,
ReplyDeleteThanks for the program,, can you help me..
I want to list all the machine details(as the output of the above program) which are connected in my network...
I am searching a long.. will you please give me an idea..
hello sir,
ReplyDeleteit is showing error like error: incompatible types
Vector tmpv = responsePDU.getVariableBindings();
^
required: Vector
found: Vector
where CAP#1 is a fresh type-variable:
CAP#1 extends VariableBinding from capture of ? extends VariableBinding
1 error
when compling
can anybody pls help me
change below line like this:
DeleteVector tmpv = (Vector ) responsePDU.getVariableBindings();
Please check and see whether you have SNMP4j.jar in your classpath.
ReplyDeleteI cant solve the error adding SNMP4J.jar in classpath
Deletes same error
ReplyDeleteHi how can i do set with this please
ReplyDeleteThe responseEvent.getResponse() returns null for me
ReplyDeleteI always appreciated your work, your creation is definitely unique. Great job
ReplyDeleteapk editor pro|kahoot hack|www.myaarpmedicare.com|
elf bar
ReplyDeletebinance hesap aƧma
sms onay
A6RRS