Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.lang.*;
- import java.util.Vector;
- import com.adventnet.snmp.snmp2.SnmpAPI;
- import com.adventnet.snmp.snmp2.SnmpException;
- import com.adventnet.snmp.snmp2.SnmpOID;
- import com.adventnet.snmp.snmp2.SnmpPDU;
- import com.adventnet.snmp.snmp2.SnmpSession;
- import com.adventnet.snmp.snmp2.SnmpVarBind;
- public class tcpConMgr {
- public static void main(String args[]) {
- if( args.length < 2)
- {
- System.out.println("Usage : java SnmpGetNext hostname OID ");
- System.exit(0);
- }
- // Take care of getting the hostname and the OID
- String remoteHost = args[0];
- String OID = args[1];
- String community= "imm";
- int port=2161;
- int oidLength = OID.length();
- //System.out.println(oidLength);
- // Start SNMP API
- SnmpAPI api;
- api = new SnmpAPI();
- api.start();
- // Open session
- SnmpSession session = new SnmpSession(api);
- try {
- session.open();
- } catch (SnmpException e ) {
- System.err.println("Error opening socket: "+e);
- }
- // set remote Host
- session.setPeername(remoteHost);
- session.setRemotePort(port);
- session.setCommunity(community);
- // Build GetNext request PDU
- SnmpPDU pdu = new SnmpPDU();
- pdu.setCommand( SnmpAPI.GETNEXT_REQ_MSG );
- // add OIDs
- SnmpOID oid = new SnmpOID(OID);
- pdu.addNull(oid);
- String checkId = OID;
- do{
- try {
- pdu.setCommand(SnmpAPI.GETNEXT_REQ_MSG);
- // Send PDU and receive response PDU
- pdu = session.syncSend(pdu);
- } catch (SnmpException e) {
- System.err.println("Error sending SNMP request: "+e);
- }
- // System.out.println(pdu.getData());
- SnmpVarBind snmpVar = pdu.getVariableBinding(0);
- //convert oid to string to manipulate later
- String objId = snmpVar.getObjectID().toString();
- //extract the oid ta later check when exit the table
- checkId = objId.substring(0,oidLength);
- //extract local host, port....
- objId = objId.substring(oidLength+1, objId.length());
- if (OID.equals(checkId))
- {
- System.out.println( objId);
- }
- else
- break;
- }while(OID.equals(checkId));
- // close session
- session.close();
- // stop api thread
- api.close();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement