Advertisement
Guest User

Untitled

a guest
May 26th, 2016
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.50 KB | None | 0 0
  1. import java.lang.*;
  2. import java.util.Vector;
  3.  
  4. import com.adventnet.snmp.snmp2.SnmpAPI;
  5. import com.adventnet.snmp.snmp2.SnmpException;
  6. import com.adventnet.snmp.snmp2.SnmpOID;
  7. import com.adventnet.snmp.snmp2.SnmpPDU;
  8. import com.adventnet.snmp.snmp2.SnmpSession;
  9. import com.adventnet.snmp.snmp2.SnmpVarBind;
  10.  
  11. public class tcpConMgr {
  12. public static void main(String args[]) {
  13.  
  14. if( args.length < 2)
  15. {
  16. System.out.println("Usage : java SnmpGetNext hostname OID ");
  17. System.exit(0);
  18. }
  19.  
  20.  
  21.  
  22. // Take care of getting the hostname and the OID
  23. String remoteHost = args[0];
  24. String OID = args[1];
  25. String community= "imm";
  26. int port=2161;
  27.  
  28. int oidLength = OID.length();
  29. //System.out.println(oidLength);
  30.  
  31. // Start SNMP API
  32. SnmpAPI api;
  33. api = new SnmpAPI();
  34. api.start();
  35.  
  36. // Open session
  37. SnmpSession session = new SnmpSession(api);
  38.  
  39. try {
  40. session.open();
  41. } catch (SnmpException e ) {
  42. System.err.println("Error opening socket: "+e);
  43. }
  44.  
  45. // set remote Host
  46. session.setPeername(remoteHost);
  47. session.setRemotePort(port);
  48. session.setCommunity(community);
  49.  
  50. // Build GetNext request PDU
  51. SnmpPDU pdu = new SnmpPDU();
  52.  
  53. pdu.setCommand( SnmpAPI.GETNEXT_REQ_MSG );
  54.  
  55. // add OIDs
  56.  
  57.  
  58. SnmpOID oid = new SnmpOID(OID);
  59. pdu.addNull(oid);
  60.  
  61.  
  62. String checkId = OID;
  63. do{
  64. try {
  65. pdu.setCommand(SnmpAPI.GETNEXT_REQ_MSG);
  66. // Send PDU and receive response PDU
  67. pdu = session.syncSend(pdu);
  68. } catch (SnmpException e) {
  69. System.err.println("Error sending SNMP request: "+e);
  70. }
  71.  
  72.  
  73. // System.out.println(pdu.getData());
  74.  
  75.  
  76. SnmpVarBind snmpVar = pdu.getVariableBinding(0);
  77. //convert oid to string to manipulate later
  78. String objId = snmpVar.getObjectID().toString();
  79. //extract the oid ta later check when exit the table
  80. checkId = objId.substring(0,oidLength);
  81. //extract local host, port....
  82. objId = objId.substring(oidLength+1, objId.length());
  83.  
  84. if (OID.equals(checkId))
  85. {
  86.  
  87.  
  88. System.out.println( objId);
  89.  
  90. }
  91. else
  92. break;
  93.  
  94. }while(OID.equals(checkId));
  95.  
  96. // close session
  97. session.close();
  98.  
  99. // stop api thread
  100. api.close();
  101.  
  102. }
  103.  
  104.  
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement