[SNMP4J] Problem with Table Utils
Ryan Acord
ryan.acord at ibbsonline.com
Wed Nov 2 16:00:58 CET 2005
Working on trying to use tableutils, but when i run the code....I get
nothing back. It returns a list of size 0. It should have 2 entries in it
import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.List;
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.Address;
import org.snmp4j.smi.GenericAddress;
import org.snmp4j.smi.OID;
import org.snmp4j.smi.OctetString;
import org.snmp4j.transport.DefaultUdpTransportMapping;
import org.snmp4j.util.DefaultPDUFactory;
import org.snmp4j.util.PDUFactory;
import org.snmp4j.util.TableUtils;
import org.snmp4j.smi.VariableBinding;
public class snmpTableTest {
public static void main(String[] args) throws IOException {
Snmp snmp = null;
String community = "public";
String host = "10.201.120.177";
InetAddress hostAddress = null;
TransportMapping transport;
int maxRepetitions = 100;
// Create the SNMP Session
try {
transport = new DefaultUdpTransportMapping();
snmp = new Snmp( transport );
} catch ( IOException ioe ) {
System.out.println( "Can not create transport " + ioe );
}
try {
hostAddress = InetAddress.getByName( host );
} catch ( UnknownHostException ue ) {
System.out.println( "Can not get the host name, input: " +
host + ue );
}
// Setting up the target
CommunityTarget target = new CommunityTarget();
target.setCommunity(new OctetString(community));
Address targetAddress = GenericAddress.parse(hostAddress+"/161");
System.out.println ("TargetAddress = " + targetAddress);
System.out.println ("Community = " + community);
target.setAddress(targetAddress);
target.setRetries(10);
target.setTimeout(5000);
target.setVersion(SnmpConstants.version2c);
try {
snmp.listen();
System.out.println("Connect to snmp");
} catch ( IOException ioe ){
System.out.println( "Can not connect to the host " + ioe );
}
// table options
PDUFactory pF = new DefaultPDUFactory (PDU.GETNEXT);
TableUtils tableUtils = new TableUtils(snmp, pF);
tableUtils.setMaxNumRowsPerPDU(maxRepetitions);
OID[] columns = new OID[1];
columns[0] = new VariableBinding (new OID(
"1.3.6.1.2.1.17.4.3.1.1" )).getOid() ;
OID lowerBoundIndex = new OID("1.3.6.1.2.1.17.4.3.1" ) ;
OID upperBoundIndex = new OID( "1.3.6.1.2.1.17.4.3.2") ;
System.out.println( "Vector Bulk SNMP oid= " + columns[0]);
System.out.println( "Vector Bulk SNMP lower= " + lowerBoundIndex);
System.out.println( "Vector Bulk SNMP upper= " + upperBoundIndex);
List snmpList = tableUtils.getTable(target, columns,
lowerBoundIndex, upperBoundIndex);
System.out.println("snmpList size : " + snmpList.size());
for ( int j = 0; j < snmpList.size();j++){
System.out.println("snmpList : " + snmpList.get(j));
}
try{
snmp.close();
} catch ( IOException ioe ) {
System.out.println( "Can not close the snmp connection " +
ioe );
}
}
}
More information about the SNMP4J
mailing list