[SNMP4J] My Hello World app !!!
Rodolfo Josÿffffe9 Castellanos Jaimes
rodolfojcj at yahoo.com
Fri Jan 28 14:27:21 CET 2005
Hi friends. I hope all you be well.
I'm just beginning to work with the SNMP4J package
from the programming point of view. I was looking for
a "hello world" like app but I didn't find it. I saw
that some people in the recent past have had the same
needing.
Yesterday that I did was to debug (running step by
step) the SnmpRequest.java example, after I read this
suggestion of Frank Fock in this link:
http://p15141779.pureserver.info/pipermail/snmp4j/2004-October/000149.html
Nice for me, the example run "perfectly". Also, I have
some doubts like why -96 in the following line ?
request.setType( -96 ); // ????
I hope I can find the justification soon.
Well, that's all.
Any suggestion, criticism, idea or question will be
welcome.
This is the source code:
/*
* HelloWorldSNMP.java
*
* Created on 27 de enero de 2005, 04:02 PM
*/
/**
*
* @author rodolfojcj
* @version 1.0
*/
/*
* This is my first app using SNMP4J package.
* My goal with it is to send a message with a GET PDU
* to my computer, which is running a snmp daemon.
* In this example, only the sysName (1.3.6.2.1.1.5.0)
object is requested
*
* I've just tested it in this scenario:
* - O.S. Linux Fedora Core 1
* - snmpd service or daemon is running
* - "public" is the community
* - Only tested for SnmpV1
* - IDE NetBeans 4.0
* - JDK 1.5.0
*
* I've just followed the SnmpRequest.java program
(which is part of
* the snmp4j package, corresponding to the tools) to
do this example
*/
package myExamples;
import org.snmp4j.*;
import org.snmp4j.smi.*;
import
org.snmp4j.transport.DefaultUdpTransportMapping;
import org.snmp4j.mp.*;
import java.io.IOException;
import java.util.Vector;
public class HelloWorldSNMP {
// class members
OctetString community = new OctetString( "public"
);
Address address = new UdpAddress( "localhost/161"
);
TransportMapping transport;
Snmp snmp;
Target target;
Vector vbs = new Vector();
/** Creates a new instance of HacerGetSNMPV1 */
public HelloWorldSNMP() {
// 1. create SNMP session
try {
transport = new DefaultUdpTransportMapping();
snmp = new Snmp( transport );
}
catch ( IOException e )
{
}
// 2. set the target which will be requested
CommunityTarget targetX = new
CommunityTarget();
targetX.setCommunity( community );
target = ( Target ) targetX;
target.setVersion( 0 );
target.setAddress( address );
target.setRetries( 1 );
target.setTimeout( 1000 );
try {
snmp.listen();
}
catch ( IOException e )
{
}
// 3. create the PDU of the message. In this
case a GET
PDU request = new PDU();
request.setType( -96 ); // honestly, I don't
know yet why -96 ???
//
vbs.add( new VariableBinding( new OID(
"1.3.6.1.2.1.1.5.0" ) ) );
//for ( int i = 0; i < vbs.size(); i++ )
request.add( ( VariableBinding ) vbs.get(
0 ) );
// prepare to get the response of the agent
PDU response = null;
try {
response = snmp.sendPDU( request, target
);
snmp.close();
}
catch ( IOException e )
{
}
// 4. print output
System.out.println( "Response received with
requestID = " + response.getRequestID() +
" , errorIndex = " +
response.getErrorIndex() +
" , errorStatus = " +
response.getErrorStatus() );
for ( int i = 0; i < response.size(); i++ )
{
VariableBinding vb = response.get( i );
System.out.println( vb.toString() );
}
}
public static void main( String args[] )
{
System.out.println( "Hello World SNMP4J !!!"
);
System.out.println( " The beginning !!! " );
HelloWorldSNMP app = new HelloWorldSNMP();
System.out.println( " Just finished !!!" );
}
}
_________________________________________________________
Do You Yahoo!?
Información de Estados Unidos y América Latina, en Yahoo! Noticias.
Visítanos en http://noticias.espanol.yahoo.com
More information about the SNMP4J
mailing list