snmpvacmconfigure.java
上传用户:aonuowh
上传日期:2021-05-23
资源大小:35390k
文件大小:34k
源码类别:

SNMP编程

开发平台:

C/C++

  1. /* $Id: snmpvacmconfigure.src,v 1.3.2.3 2009/01/28 13:32:31 tmanoj Exp $ */
  2. /*
  3.  * @(#)snmpvacmconfigure.java
  4.  * Copyright (c) 1996-2009 AdventNet, Inc. All Rights Reserved.
  5.  * Please read the associated COPYRIGHTS file for more details.
  6.  */
  7. /**
  8.  * This is an example program used for adding entries to
  9.  * the VACM tables, namely "vacmSecurityToGroupTable",
  10.  * "vacmAccessTable" and "vacmViewTreeFamilyTable".
  11.  */
  12. import java.util.StringTokenizer;
  13. import com.adventnet.snmp.snmp2.*;
  14. import com.adventnet.snmp.snmp2.vacm.*;
  15. import com.adventnet.snmp.snmp2.usm.*;
  16. public class snmpvacmconfigure
  17. {
  18.     static private final int CREATEACCESS       = 1;
  19.     static private final int DELETEACCESS       = 2;
  20.     static private final int CREATESEC2GROUP    = 3;
  21.     static private final int DELETESEC2GROUP    = 4;
  22.     static private final int CREATEVIEW         = 5;
  23.     static private final int DELETEVIEW         = 6;
  24.     static private final int HELP               = 7;
  25.     static private final int QUIT               = 8;
  26.     private final int CREATE_AND_GO         = 4;
  27.     private final String securityToGroupTableEntry = ".1.3.6.1.6.3.16.1.2.1.";
  28.     private final String vacmAccessEntry = ".1.3.6.1.6.3.16.1.4.1.";
  29.     private final String vacmFamilyEntry = ".1.3.6.1.6.3.16.1.5.2.1.";
  30.     private String params;
  31.     static private final String accessHelp =
  32. "nn" +
  33. "------------------------------------------------------------------------n" +
  34. "                           "vacmAccessTable"n" +
  35. "------------------------------------------------------------------------n" +
  36. "S.No ColumnName               Data Type     Size      Possible valuesn" +
  37. "------------------------------------------------------------------------n" +
  38. "1.   vacmGroupName            OCTET STRING  1 .. 32          -n" +
  39. "2.   vacmAccessContextPrefix  OCTET STRING  0 .. 32          -n" +
  40. "3.   vacmAccessSecurityModel  INTEGER         -       0 .. 2147483647n" +
  41. "4.   vacmAccessSecurityLevel  INTEGER         -       1(noAuthNoPriv)n" +
  42. "                                              -       2(authNoPriv)n" +
  43. "                                              -       3(authPriv)n" +
  44. "5.   vacmAccessContextMatch   INTEGER         -       1(exact)n" +
  45. "                                              -       2(prefix)n" +
  46. "6.   vacmAccessReadViewName   OCTET STRING  0 .. 32          -n" +
  47. "7.   vacmAccessWriteViewName  OCTET STRING  0 .. 32          -n" +
  48. "8.   vacmAccessNotifyViewName OCTET STRING  0 .. 32          -n" +
  49. "------------------------------------------------------------------------n";
  50.     
  51.     static private final String securityToGroupHelp =
  52. "nn" +
  53. "------------------------------------------------------------------------n" +
  54. "                           "vacmSecurityToGroupTable"n" +
  55. "------------------------------------------------------------------------n" +
  56. "S.No ColumnName                     Data Type     Size      Possible valuesn" +
  57. "------------------------------------------------------------------------n" +
  58. "1.   vacmSecurityModel              INTEGER         -       0 .. 2147483647n"+
  59. "2.   vacmSecurityName               OCTET STRING  1 .. 32          -n" +
  60. "3.   vacmGroupName                  OCTET STRING  1 .. 32          -n" +
  61. "4.   vacmSecurityToGroupStorageType INTEGER         -       1(other)n" +
  62. "                                                            2(volatile)n" +
  63. "                                                            3(nonVolatile)n" +
  64. "                                                            4(permanent)n" +
  65. "                                                            5(readOnly)n" +
  66. "------------------------------------------------------------------------n";
  67.     static private final String familyTableHelp =
  68. "nn" +
  69. "------------------------------------------------------------------------------n" +
  70. "                           "vacmViewTreeFamilyTable"n" +
  71. "------------------------------------------------------------------------------n" +
  72. "S.No ColumnName                  Data Type          Size      Possible valuesn" +
  73. "------------------------------------------------------------------------------n" +
  74. "1.   vacmViewTreeFamilyViewName  OCTET STRING       1 .. 32          -n" +
  75. "2.   vacmViewTreeFamilySubtree   OBJECT IDENTIFIER    -              -n" +
  76. "3.   vacmViewTreeFamilyMask      OCTET STRING       0 .. 16          -n" +
  77. "------------------------------------------------------------------------------n" ;
  78.     private SnmpAPI api;
  79.     private SnmpSession session;
  80.     
  81.     private int timeout;
  82.     private int retries;
  83.     private String remoteHost;
  84.     private int remotePort;
  85.     private int version;
  86.     private String contextName;
  87.     private String userName;
  88.     private String authPassword;
  89.     private int authProtocol;
  90.     private String privPassword;
  91.     private String optionString;
  92.     private int action;
  93.     private String helpNumber;
  94.     public snmpvacmconfigure() throws SnmpException
  95.     {
  96.         api = new SnmpAPI();
  97.         session = new SnmpSession(api);
  98.         session.open();
  99.         timeout = 5000;
  100.         retries = 0;
  101.         remoteHost = null;
  102.         remotePort = 161;
  103.         version = 0;
  104.         contextName = null;
  105.         userName = null;
  106.         authPassword = null;
  107.         authProtocol = USMUserEntry.NO_AUTH;
  108.         privPassword = null;
  109.         params = null;
  110.         optionString = 
  111.         "nn---------------------------------------n" +
  112.         "1. createAccessn" +
  113.         "2. deleteAccessn" +
  114.         "3. createSec2Groupn" +
  115.         "4. deleteSec2Groupn" +
  116.         "5. createViewn" +
  117.         "6. deleteViewn" +
  118.         "7. Helpn" +
  119.         "8. Quitn" +
  120.         "Enter a Choice :";
  121.         helpNumber = "";
  122.     }
  123.     public void setRemoteHost(String host)
  124.     {
  125.         this.remoteHost = host;
  126.     }
  127.     public void setRemotePort(int remotePort)
  128.     {
  129.         this.remotePort = remotePort;
  130.     }
  131.     
  132.     public void setTimeout(int timeout)
  133.     {
  134.         this.timeout = timeout;
  135.     }
  136.     public void setRetries(int retries)
  137.     {
  138.         this.retries = retries;
  139.     }
  140.     public void setVersion(int version)
  141.     {
  142.         this.version = version;
  143.     }
  144.     public void setContextName(String contextName)
  145.     {
  146.         this.contextName = contextName;
  147.     }
  148.     public void setUserName(String userName)
  149.     {
  150.         this.userName = userName;
  151.     }
  152.     public void setAuthPassword(String authPassword)
  153.     {
  154.         this.authPassword = authPassword;
  155.     }
  156.     public void setAuthProtocol(int authProtocol)
  157.     {
  158.         this.authProtocol = authProtocol;
  159.     }
  160.     public void setPrivPassword(String privPassword)
  161.     {
  162.         this.privPassword = privPassword;
  163.     }
  164.     public void initDatabase(String driver, String url,
  165.     String user, String password)throws SnmpException
  166.     {
  167.         try
  168.         {
  169.             api.initJdbcParams(driver, url, user, password);
  170.         }
  171.         catch(Exception exp)
  172.         {
  173.             throw new SnmpException(exp.toString());
  174.         }
  175.     }
  176.     public void createUSMEntry() throws SnmpException
  177.     {
  178.         if(userName == null || userName.length() == 0)
  179.         {
  180.             throw new SnmpException("Invalid UserName.");
  181.         }
  182.         if(authPassword != null && authProtocol == USMUserEntry.NO_AUTH)
  183.         {
  184.             throw new SnmpException(
  185.             "Specify the authProtocol if authPassword is specified.");
  186.         }
  187.         if(privPassword != null && authPassword == null)
  188.         {
  189.             throw new SnmpException(
  190.             "An user cannot be "noAuth,Priv", " +
  191.             "hence specify the authPassword");
  192.         }
  193.         USMUtils.init_v3_params(userName, authProtocol, authPassword,
  194.             privPassword, remoteHost, remotePort, session);
  195.         System.out.println("nnSuccessfully created the USM entry for " +
  196.             remoteHost + ":" + remotePort + ":" + userName + ".");
  197.     }
  198.     public void close()
  199.     {
  200.         session.close();
  201.         api.close();
  202.     }
  203.     public void processConfiguration()
  204.     {
  205.         int action = -1;
  206.         String line = "";
  207.         do
  208.         {
  209.             try
  210.             {
  211.                 helpNumber = "";
  212.                 System.out.print(optionString);
  213.                 StringTokenizer st = new StringTokenizer(readLine());
  214.                 int tokens = st.countTokens();
  215.                 line = st.nextToken();
  216.                 if(tokens > 1)
  217.                 {
  218.                     helpNumber = st.nextToken();
  219.                 }
  220.                 try
  221.                 {
  222.                     action = Integer.parseInt(line);
  223.                     if(!isValidOption(action))
  224.                     {
  225.                         System.out.println("nInvalid option. " + action);
  226.                         continue;
  227.                     }
  228.                     performAction(action);
  229.                 }
  230.                 catch(NumberFormatException exp)
  231.                 {
  232.                     System.out.println("Invalid Option. " + line);
  233.                     continue;
  234.                 }
  235.             }
  236.             catch(Exception exp)
  237.             {
  238.             }
  239.         }while(action != QUIT);
  240.     }
  241.     private void performAction(int action)
  242.     {
  243.         switch(action)
  244.         {
  245.             case CREATEACCESS:
  246.             {
  247.                 createAccess();
  248.                 break;
  249.             }
  250.             case DELETEACCESS:
  251.             {
  252.                 deleteAccess();
  253.                 break;
  254.             }
  255.             case CREATESEC2GROUP:
  256.             {
  257.                 createSecurityToGroup();
  258.                 break;
  259.             }
  260.             case DELETESEC2GROUP:
  261.             {
  262.                 deleteSecurityToGroup();
  263.                 break;
  264.             }
  265.             case CREATEVIEW:
  266.             {
  267.                 createView();
  268.                 break;
  269.             }
  270.             case DELETEVIEW:
  271.             {
  272.                 deleteView();
  273.                 break;
  274.             }
  275.             case HELP:
  276.             {
  277.                 help();
  278.                 break;
  279.             }
  280.         }
  281.     }
  282.     private SnmpPDU getSnmpPDU()
  283.     {
  284.         SnmpPDU pdu = new SnmpPDU();
  285.         UDPProtocolOptions pdu_opt = new UDPProtocolOptions(remoteHost, remotePort);
  286.         pdu.setProtocolOptions(pdu_opt);
  287.         pdu.setVersion(version);
  288.         pdu.setCommand(api.SET_REQ_MSG);
  289.         if(version == SnmpAPI.SNMP_VERSION_3)
  290.         {
  291.             if(userName != null)
  292.             {
  293.                 pdu.setUserName(userName.getBytes());
  294.             }
  295.             if(contextName != null)
  296.             {
  297.                 pdu.setContextName(contextName.getBytes());
  298.             }
  299.         }
  300.         return pdu;
  301.     }
  302.     private void createAccess()
  303.     {
  304.         try
  305.         {
  306.             if(params == null)
  307.             {
  308.                 System.out.println(
  309.                 "Please enter the following in a single line " +
  310.                 "seperated by space:n" +
  311.                 "GROUPNAME PREFIX SECURITYMODEL SECURITYLEVEL MATCH " +
  312.                 "READ WRITE NOTIFY ");
  313.             //  + "STORAGE_TYPE");
  314.             }
  315.             String s = (params == null)? readLine() : params;
  316.             StringTokenizer st = new StringTokenizer(s, " ");
  317.             String groupName = st.nextToken();
  318.             String prefix = st.nextToken();
  319.             int model = Integer.parseInt(st.nextToken());
  320.             int level = Integer.parseInt(st.nextToken());
  321.             int match = Integer.parseInt(st.nextToken());
  322.             String readView = st.nextToken();
  323.             String writeView = st.nextToken();
  324.             String notifyView = st.nextToken();
  325.         //  int storageType = Integer.parseInt(st.nextToken());
  326.             String index = getStringIndex(groupName) + "." +
  327.                 getStringIndex(prefix) + "." +
  328.                 model + "." + level;
  329.             SnmpPDU pdu = getSnmpPDU();
  330.             
  331.             SnmpVarBind varbind = new SnmpVarBind(
  332.                 new SnmpOID(vacmAccessEntry + "9." + index),
  333.                 new SnmpInt(4));
  334.             pdu.addVariableBinding(varbind);
  335.             varbind = new SnmpVarBind(
  336.                 new SnmpOID(vacmAccessEntry + "4." + index),
  337.                 new SnmpInt(match));
  338.             pdu.addVariableBinding(varbind);
  339.             varbind = new SnmpVarBind(
  340.                 new SnmpOID(vacmAccessEntry + "5." + index),
  341.                 new SnmpString(readView));
  342.             pdu.addVariableBinding(varbind);
  343.             varbind = new SnmpVarBind(
  344.                 new SnmpOID(vacmAccessEntry + "6." + index),
  345.                 new SnmpString(writeView));
  346.             pdu.addVariableBinding(varbind);
  347.             varbind = new SnmpVarBind(
  348.                 new SnmpOID(vacmAccessEntry + "7." + index),
  349.                 new SnmpString(notifyView));
  350.             pdu.addVariableBinding(varbind);
  351. /*          varbind = new SnmpVarBind(
  352.                 new SnmpOID(vacmAccessEntry + "8." + index),
  353.                 new SnmpInt(storageType));
  354.             pdu.addVariableBinding(varbind);*/
  355.             send(pdu);
  356.             System.out.println(
  357.                 "Successfully added an entry to the VacmAccessTable.");
  358.         }
  359.         catch(Exception exp)
  360.         {
  361.             System.out.println(
  362.             "Unable to create an entry in VacmAccessTable. " + exp);
  363.         }
  364.     }
  365.     private void deleteAccess()
  366.     {
  367.         try
  368.         {
  369.             if(params == null)
  370.             {
  371.                 System.out.println(
  372.                 "Please enter the following in a single line " +
  373.                 "seperated by space:n" +
  374.                 "GROUPNAME CONTEXTPREFIX SECURITYMODEL SECURITYLEVEL");
  375.             }
  376.             String s = (params == null)? readLine() : params;
  377.             StringTokenizer st = new StringTokenizer(s, " ");
  378.             String groupName = st.nextToken();
  379.             String contextPrefix = st.nextToken();
  380.             int securityModel = Integer.parseInt(st.nextToken());
  381.             int securityLevel = Integer.parseInt(st.nextToken());
  382.             String index = vacmAccessEntry + "9." +
  383.                 getStringIndex(groupName) + "." +
  384.                 getStringIndex(contextPrefix) + "." + securityModel + "." +
  385.                 securityLevel;
  386.             SnmpOID indexOID = new SnmpOID(index);
  387.             SnmpInt indexValue = new SnmpInt(6);
  388.             SnmpPDU pdu = getSnmpPDU();
  389.             SnmpVarBind varbind = new SnmpVarBind(indexOID, indexValue);
  390.             pdu.addVariableBinding(varbind);
  391.             send(pdu);
  392.             System.out.println("Successfully deleted the entry " +
  393.                 "from the VacmAccessTable.");
  394.         }
  395.         catch(Exception exp)
  396.         {
  397.             System.out.println("Problem encountered while deleting access: " +
  398.                 exp.toString());
  399.         }
  400.     }
  401.     private void createSecurityToGroup()
  402.     {
  403.         if(params == null)
  404.         {
  405.             System.out.println(
  406.             "Please enter the following in a single line " +
  407.             "seperated by space:n" +
  408.             "MODEL SECURITYNAME  GROUPNAME");
  409.         //  + "STORAGE_TYPE");
  410.         }
  411.         String s = (params == null)? readLine() : params;
  412.         StringTokenizer st = new StringTokenizer(s, " ");
  413.         try
  414.         {
  415.             int model = Integer.parseInt(st.nextToken());
  416.             String securityName = st.nextToken();
  417.             String groupName = st.nextToken();
  418.         //  int storageType = Integer.parseInt(st.nextToken());
  419.             String index = model + "." + getStringIndex(securityName);
  420.             SnmpPDU pdu = getSnmpPDU();
  421.             String rowStatusOID = securityToGroupTableEntry + "5." + index;
  422.             SnmpOID oid = new SnmpOID(rowStatusOID);
  423.             SnmpInt rowStatusValue = new SnmpInt(4);
  424.             SnmpVarBind varbind = new SnmpVarBind(oid, rowStatusValue);
  425.             pdu.addVariableBinding(varbind);
  426.             String groupNameOID = securityToGroupTableEntry + "3." + index;
  427.             oid = new SnmpOID(groupNameOID);
  428.             SnmpString str = new SnmpString(groupName);
  429.             varbind = new SnmpVarBind(oid, str);
  430.             pdu.addVariableBinding(varbind);
  431. /*
  432.             String storageTypeOID = securityToGroupTableEntry + "4." + index;
  433.             oid = new SnmpOID(groupNameOID);
  434.             SnmpInt storageTypeValue = new SnmpInt(storageType);
  435.             varbind = new SnmpVarBind(oid, storageTypeValue);
  436.             pdu.addVariableBinding(varbind);*/
  437.             send(pdu);
  438.             System.out.println("Successfully added an entry " +
  439.                 "to the SecurityToGroupTable.");
  440.         }
  441.         catch(Exception exp)
  442.         {
  443.             System.out.println(
  444.             "Unable to create an entry in the securityToGroupTable. " + exp);
  445.         }
  446.     }
  447.     private void deleteSecurityToGroup()
  448.     {
  449.         if(params == null)
  450.         {
  451.             System.out.println(
  452.             "Please enter the following in a single line " +
  453.             "seperated by space:n" +
  454.             "MODEL SECURITYNAME");
  455.         }
  456.         String s = (params == null)? readLine() : params;
  457.         StringTokenizer st = new StringTokenizer(s, " ");
  458.         try
  459.         {
  460.             int model = Integer.parseInt(st.nextToken());
  461.             String securityName = st.nextToken();
  462.             String index = model + "." + getStringIndex(securityName);
  463.             String rowStatusOID = securityToGroupTableEntry + "5." + index;
  464.             SnmpPDU pdu = getSnmpPDU();
  465.             SnmpOID rowStatus = new SnmpOID(rowStatusOID);
  466.             SnmpInt rowStatusValue = new SnmpInt(6);
  467.             SnmpVarBind varbind = new SnmpVarBind(rowStatus, rowStatusValue);
  468.             pdu.addVariableBinding(varbind);
  469.             send(pdu);
  470.             System.out.println("Successfully deleted an entry from the " +
  471.                 "SecurityToGroypTable.");
  472.         }
  473.         catch(Exception exp)
  474.         {
  475.             System.out.println(
  476.             "Unable to delete an entry from securityToGroupTable. " +
  477.             exp.toString());
  478.         }
  479.     }
  480.     private void createView()
  481.     {
  482.         if(params == null)
  483.         {
  484.             System.out.println(
  485.             "Please enter the following in a single line " +
  486.             "seperated by space:n" +
  487.             "NAME SUBTREE MASK");
  488.         //  + "STORAGE_TYPE");
  489.         }
  490.         try
  491.         {
  492.             String s = (params == null) ? readLine() : params;
  493.             StringTokenizer st = new StringTokenizer(s, " ");
  494.             String viewName = st.nextToken();
  495.             String subtree = st.nextToken();
  496.             SnmpOID subtreeOID = new SnmpOID(subtree);
  497.             int[] subids = (int[])subtreeOID.toValue();
  498.             if(subids != null)
  499.             {
  500.                 subtree = subids.length + subtreeOID.toString();
  501.             }
  502.             else
  503.             {
  504.                 throw new Exception("Invalid SUBTREE: " + subtree);
  505.             }
  506.             String mask = st.nextToken();
  507.         //  int storageType = Integer.parseInt(st.nextToken());
  508.             SnmpPDU pdu = getSnmpPDU();
  509.             String index = getStringIndex(viewName) + "." + subtree;
  510.             SnmpOID rowStatusOID = new SnmpOID(vacmFamilyEntry+"6."+index);
  511.             SnmpInt rowStatus = new SnmpInt(4);
  512.             SnmpVarBind varbind = new SnmpVarBind(rowStatusOID, rowStatus);
  513.             pdu.addVariableBinding(varbind);
  514.             SnmpOID maskOID = new SnmpOID(vacmFamilyEntry + "3." + index);
  515.             SnmpString maskValue = new SnmpString(mask);
  516.             varbind = new SnmpVarBind(maskOID, maskValue);
  517.             pdu.addVariableBinding(varbind);
  518.             SnmpOID familyTypeOID = new SnmpOID(vacmFamilyEntry+"4."+index);
  519.             SnmpInt familyTypeValue = new SnmpInt(1);
  520.             varbind = new SnmpVarBind(familyTypeOID, familyTypeValue);
  521.             pdu.addVariableBinding(varbind);
  522. /*
  523.             SnmpOID storageTypeOID = new SnmpOID(vacmFamilyEntry+"5."+index);
  524.             SnmpInt storageTypeValue = new SnmpInt(storageType);
  525.             varbind = new SnmpVarBind(storageTypeOID, storageTypeValue);
  526.             pdu.addVariableBinding(varbind);*/
  527.             send(pdu);
  528.             System.out.println("Successfully added an entry to the " +
  529.                 "VacmViewTreeFamilyTable.");
  530.         }
  531.         catch(Exception exp)
  532.         {
  533.             System.out.println(
  534.             "Unable to create an entry in VacmViewTreeFamilyTable. " +
  535.             exp.toString());
  536.         }
  537.     }
  538.     private void deleteView()
  539.     {
  540.         if(params == null)
  541.         {
  542.             System.out.println(
  543.             "Please enter the following in a single line " +
  544.             "seperated by space:n" +
  545.             "NAME SUBTREE");
  546.         }
  547.         try
  548.         {
  549.             String s = (params == null) ? readLine() : params ;
  550.             StringTokenizer st = new StringTokenizer(s, " ");
  551.             String viewName = st.nextToken();
  552.             String subtree = st.nextToken();
  553.             SnmpOID subtreeOID = new SnmpOID(subtree);
  554.             int[] subids = (int[])subtreeOID.toValue();
  555.             if(subids != null)
  556.             {
  557.                 subtree = subids.length + subtreeOID.toString();
  558.             }
  559.             else
  560.             {
  561.                 System.out.println("Invalid SUBTREE: " + subtree);
  562.             }
  563.             String index = getStringIndex(viewName) + "." + subtree ;
  564.             SnmpPDU pdu = getSnmpPDU();
  565.             SnmpOID rowStatusOID = new SnmpOID(vacmFamilyEntry+"6."+index);
  566.             SnmpInt rowStatusValue = new SnmpInt(6);
  567.             SnmpVarBind varbind = new SnmpVarBind(rowStatusOID,rowStatusValue);
  568.             pdu.addVariableBinding(varbind);
  569.             send(pdu);
  570.             System.out.println("Successfully deleted an entry from the " +
  571.                 "VacmViewTreeFamilyTable.");
  572.         }
  573.         catch(Exception exp)
  574.         {
  575.             System.out.println(
  576.             "Unable to delete an entry from the VacmViewTreeFamilyTable. " +
  577.             exp.toString());
  578.         }
  579.     }
  580.     private void help()
  581.     {
  582.         int num = 0;
  583.         try
  584.         {
  585.             if(!helpNumber.equals(""))
  586.             {
  587.                 num = Integer.parseInt(helpNumber);
  588.             }
  589.         }
  590.         catch(Exception exp)
  591.         {
  592.         }
  593.         if(num == 0 || num == CREATEACCESS || num == DELETEACCESS)
  594.         {
  595.             System.out.print(accessHelp);
  596.         }
  597.         if(num == 0 || num == CREATESEC2GROUP || num == DELETESEC2GROUP)
  598.         {
  599.             System.out.print(securityToGroupHelp);
  600.         }
  601.         if(num == 0 || num == CREATEVIEW || num == DELETEVIEW)
  602.         {
  603.             System.out.print(familyTableHelp);
  604.         }
  605.     }
  606.     private void send(SnmpPDU pdu) throws Exception
  607.     {
  608.         SnmpPDU res_pdu = session.syncSend(pdu);
  609.         if(res_pdu != null)
  610.         {
  611.             if(res_pdu.getErrindex() != 0 || res_pdu.getErrstat() != 0)
  612.             {
  613.                 throw new Exception(res_pdu.getError());
  614.             }
  615.         }
  616.         else
  617.         {
  618.             throw new Exception("SNMP SET Request has timed out.");
  619.         }
  620.     }
  621.     private String getStringIndex(String stringIndexValue)
  622.     {
  623.         int len = stringIndexValue.length();
  624.         StringBuffer sb = new StringBuffer();
  625.         sb.append(len);
  626.         for(int i=0;i<len;i++)
  627.         {
  628.             sb.append(".");
  629.             sb.append( (int)(stringIndexValue.charAt(i) & 0xff) );
  630.         }
  631.         return sb.toString();
  632.     }
  633.     private boolean isValidOption(int option)
  634.     {
  635.         return (option == CREATEACCESS || option == DELETEACCESS ||
  636.             option == CREATESEC2GROUP || option == DELETESEC2GROUP ||
  637.             option == DELETESEC2GROUP || option == CREATEVIEW ||
  638.             option == DELETEVIEW || option == HELP || option == QUIT);
  639.     }
  640.     private String readLine()
  641.     {
  642.         char newLine = 'n';
  643.         char ch;
  644.         char[] array = new char[10];
  645.         int i=0;
  646.         String line = "";
  647.         try
  648.         {
  649.             do
  650.             {
  651.                 if(i == array.length)
  652.                 {
  653.                     char[] dummy = new char[array.length * 2];
  654.                     System.arraycopy(array, 0, dummy, 0, array.length);
  655.                     array = dummy;
  656.                 }
  657.                 ch = (char)(System.in.read() & 0xff);
  658.                 array[i++] = ch;
  659.             }
  660.             while(ch != newLine);
  661.             line = new String(array, 0, i-1);
  662.         }
  663.         catch(Exception exp)
  664.         {
  665.         }
  666.         return line;
  667.     }
  668.     public void setDebug(boolean bool)
  669.     {
  670.         api.setDebug(bool);
  671.     }
  672.     public static void main(String[] args)
  673.     {
  674.         int DEBUG           = 0;
  675.         int PORT            = 1;
  676.         int RETRIES         = 2;
  677.         int TIMEOUT         = 3;
  678.     //  int STORAGE_TYPE    = 4;
  679.         int VERSION         = 4;
  680.         int USERNAME        = 5;
  681.         int AUTHPROTOCOL    = 6;
  682.         int AUTHPASSWORD    = 7;
  683.         int PRIVPASSWORD    = 8;
  684.         int CONTEXTNAME     = 9;
  685.         int CONTEXTID       = 10;
  686.         int DB_DRIVER       = 11;
  687.         int DB_URL          = 12;
  688.         int DB_USER         = 13;
  689.         int DB_PASSWORD     = 14;
  690.         int HOST            = 0;
  691.         String usage =
  692.             "nsnmpget [-d] [-p port] [-r retries] [-t timeout]n" +
  693.         //  "[-st storage_type]n" +
  694.             "[-v version(v1/v2/v3)]" + 
  695.             "[-u userName]n" +
  696.             "[-a auth_protocol] [-w auth_password] [-s priv_password]n" +
  697.             "[-n contextName] [-i contextID]n" +
  698.             "[-DB_driver database_driver]n" +
  699.             "[-DB_url database_url]n" +
  700.             "[-DB_username database_username]n" +
  701.             "[-DB_password database_password]n" +
  702.             "host [command]nn" +
  703.             "COMMANDn" +
  704.             "createAccess GROUPNAME PREFIX SECURITYMODEL SECURITYLEVEL MATCH READ WRITE NOTIFY n" +
  705.         //  "[-st storage_type]n" +
  706.             "deleteAccess GROUPNAME CONTEXTPREFIX SECURITYMODEL SECURITYLEVELn" +
  707.             "createSecurityToGroup MODEL SECURITYNAME GROUPNAME n" +
  708.         //  "[-st storage_type]n" +
  709.             "deleteSecurityToGroup MODEL SECURITYNAMEn" +
  710.             "createView NAME SUBTREE MASK [-type familyType]n" +
  711.         //  "[-st storage_type]n" + 
  712.             "deleteView NAME SUBTREEn";
  713.         String options[] =
  714.         {
  715.             "-d", "-p", "-r", "-t"
  716.         //  "-st"
  717.             ,"-v"
  718.             ,"-u", "-a", "-w", "-s", "-n", "-i",
  719.             "-DB_driver", "-DB_url", "-DB_username", "-DB_password"
  720.         };
  721.         String values[] =
  722.         {
  723.             "None", null, null, null
  724.         //  null
  725.             ,null
  726.             ,null, null, null, null, null, null,
  727.             null, null, null, null
  728.         };
  729.         ParseOptions opt = new ParseOptions(args, options, values, usage);
  730.         if(opt.remArgs.length == 0)
  731.         {
  732.             System.out.println("Host field is Mandatory.");
  733.             System.out.println(usage);
  734.             System.exit(1);
  735.         }
  736.         snmpvacmconfigure vacmConfig = null;
  737.         try
  738.         {
  739.             vacmConfig = new snmpvacmconfigure();
  740.         }
  741.         catch(SnmpException exp)
  742.         {
  743.             System.out.println(exp.toString());
  744.             System.exit(1);
  745.         }
  746.         vacmConfig.setRemoteHost(opt.remArgs[HOST]);
  747.         if(values[VERSION] != null)
  748.         {
  749.             if(values[VERSION].equals("v1"))
  750.             {
  751.                 vacmConfig.setVersion(SnmpAPI.SNMP_VERSION_1);
  752.             }
  753.             else if(values[VERSION].equals("v2"))
  754.             {
  755.                 vacmConfig.setVersion(SnmpAPI.SNMP_VERSION_2);
  756.             }
  757.             else if(values[VERSION].equals("v3"))
  758.             {
  759.                 vacmConfig.setVersion(SnmpAPI.SNMP_VERSION_3);
  760.             }
  761.         }
  762.         if(vacmConfig.version == SnmpAPI.SNMP_VERSION_3)
  763.         {
  764.             if(values[USERNAME] == null)
  765.             {
  766.                 System.out.println(
  767.                 "UserName should be specified in case of SNMP_3.");
  768.                 System.exit(1);
  769.             }
  770.             vacmConfig.setUserName(values[USERNAME]);
  771.             vacmConfig.setAuthPassword(values[AUTHPASSWORD]);
  772.             if(values[AUTHPROTOCOL] != null)
  773.             {
  774.                 if(values[AUTHPROTOCOL].equals("MD5"))
  775.                 {
  776.                     vacmConfig.setAuthProtocol(USMUserEntry.MD5_AUTH);
  777.                 }
  778.                 else if(values[AUTHPROTOCOL].equals("SHA"))
  779.                 {
  780.                     vacmConfig.setAuthProtocol(USMUserEntry.SHA_AUTH);
  781.                 }
  782.                 else
  783.                 {
  784.                     System.out.println("Invalid AuthProtocol. " +
  785.                     "It can be either MD5 or SHA: " + values[AUTHPROTOCOL]);
  786.                     System.exit(1);
  787.                 }
  788.             }
  789.             vacmConfig.setPrivPassword(values[PRIVPASSWORD]);
  790.             try
  791.             {
  792.                 vacmConfig.createUSMEntry();
  793.             }
  794.             catch(Exception exp)
  795.             {
  796.                 System.out.println("Could not create USM Entry.n" +
  797.                 "Please check the parameters: " + exp.toString());
  798.                 System.exit(1);
  799.             }
  800.             vacmConfig.setContextName(values[CONTEXTNAME]);
  801.         }
  802.         if(values[TIMEOUT] != null)
  803.         {
  804.             try
  805.             {
  806.                 vacmConfig.setTimeout(Integer.parseInt(values[TIMEOUT]));
  807.             }
  808.             catch(NumberFormatException nfe)
  809.             {
  810.                 System.out.println("Invalid timeout value:"+values[TIMEOUT] );
  811.                 System.exit(1);
  812.             }
  813.         }
  814.         if(values[RETRIES] != null)
  815.         {
  816.             try
  817.             {
  818.                 vacmConfig.setRetries(Integer.parseInt(values[RETRIES]));
  819.             }
  820.             catch(NumberFormatException nfe)
  821.             {
  822.                 System.out.println("Invalid retries value:"+values[RETRIES] );
  823.                 System.exit(1);
  824.             }
  825.         }
  826.         if(values[PORT] != null)
  827.         {
  828.             try
  829.             {
  830.                 vacmConfig.setRemotePort(Integer.parseInt(values[PORT]));
  831.             }
  832.             catch(NumberFormatException nfe)
  833.             {
  834.                 System.out.println("Invalid port:" + values[PORT]);
  835.                 System.exit(1);
  836.             }
  837.         }
  838.         if(values[DEBUG].equals("Set"))
  839.         {
  840.             vacmConfig.setDebug(true);
  841.         }
  842.         if(opt.remArgs.length > 1)
  843.         {
  844.             String s = opt.remArgs[1];
  845.             if(s.equals("createAccess"))
  846.             {
  847.                 if(opt.remArgs.length == 10)
  848.                 {
  849.                     StringBuffer sb = new StringBuffer();
  850.                     for(int i=2;i<opt.remArgs.length;i++)
  851.                     {
  852.                         sb.append(opt.remArgs[i] + " ");
  853.                     }
  854.                 /*  if(values[STORAGE_TYPE] != null)
  855.                     {
  856.                         sb.append(values[STORAGE_TYPE]);
  857.                     }
  858.                     else
  859.                     {
  860.                         sb.append("3");
  861.                     }*/
  862.                     vacmConfig.params = sb.toString();
  863.                     vacmConfig.createAccess();
  864.                 }
  865.                 else
  866.                 {
  867.                     System.out.println(usage);
  868.                 }
  869.             }
  870.             else if(s.equals("deleteAccess"))
  871.             {
  872.                 if(opt.remArgs.length == 6)
  873.                 {
  874.                     StringBuffer sb = new StringBuffer();
  875.                     for(int i=2;i<opt.remArgs.length;i++)
  876.                     {
  877.                         sb.append(opt.remArgs[i] + " ");
  878.                     }
  879.                     vacmConfig.params = sb.toString();
  880.                     vacmConfig.deleteAccess();
  881.                 }
  882.                 else
  883.                 {
  884.                     System.out.println(usage);
  885.                 }
  886.             }
  887.             else if(s.equals("createSecurityToGroup"))
  888.             {
  889.                 if(opt.remArgs.length == 5)
  890.                 {
  891.                     StringBuffer sb = new StringBuffer();
  892.                     for(int i=2;i<opt.remArgs.length;i++)
  893.                     {
  894.                         sb.append(opt.remArgs[i] + " ");
  895.                     }
  896.                 /*  if(values[STORAGE_TYPE] != null)
  897.                     {
  898.                         sb.append(values[STORAGE_TYPE]);
  899.                     }
  900.                     else
  901.                     {
  902.                         sb.append("3");
  903.                     }*/
  904.                     vacmConfig.params = sb.toString();
  905.                     vacmConfig.createSecurityToGroup();
  906.                 }
  907.                 else
  908.                 {
  909.                     System.out.println(usage);
  910.                 }
  911.             }
  912.             else if(s.equals("deleteSecurityToGroup"))
  913.             {
  914.                 if(opt.remArgs.length == 4)
  915.                 {
  916.                     StringBuffer sb = new StringBuffer();
  917.                     for(int i=2;i<opt.remArgs.length;i++)
  918.                     {
  919.                         sb.append(opt.remArgs[i] + " ");
  920.                     }
  921.                     vacmConfig.params = sb.toString();
  922.                     vacmConfig.deleteSecurityToGroup();
  923.                 }
  924.                 else
  925.                 {
  926.                     System.out.println(usage);
  927.                 }
  928.             }
  929.             else if(s.equals("createView"))
  930.             {
  931.                 if(opt.remArgs.length == 5)
  932.                 {
  933.                     StringBuffer sb = new StringBuffer();
  934.                     for(int i=2;i<opt.remArgs.length;i++)
  935.                     {
  936.                         sb.append(opt.remArgs[i] + " ");
  937.                     }
  938.                 /*  if(values[STORAGE_TYPE] != null)
  939.                     {
  940.                         sb.append(values[STORAGE_TYPE]);
  941.                     }
  942.                     else
  943.                     {
  944.                         sb.append("3");
  945.                     }*/
  946.                     vacmConfig.params = sb.toString();
  947.                     vacmConfig.createView();
  948.                 }
  949.                 else
  950.                 {
  951.                     System.out.println(usage);
  952.                 }
  953.             }
  954.             else if(s.equals("deleteView"))
  955.             {
  956.                 if(opt.remArgs.length == 4)
  957.                 {
  958.                     StringBuffer sb = new StringBuffer();
  959.                     for(int i=2;i<opt.remArgs.length;i++)
  960.                     {
  961.                         sb.append(opt.remArgs[i] + " ");
  962.                     }
  963.                     vacmConfig.params = sb.toString();
  964.                     vacmConfig.deleteView();
  965.                 }
  966.                 else
  967.                 {
  968.                     System.out.println(usage);
  969.                 }
  970.             }
  971.             else
  972.             {
  973.                 System.out.println(
  974.                 "COMMAND should be any one of the following:");
  975.                 System.out.println(
  976.                 "createAccess deleteAccess createSecurityToGroup");
  977.                 System.out.println(
  978.                 "deleteSecurityToGroup createView deleteView");
  979.             }
  980.             System.exit(1);
  981.         }
  982.         vacmConfig.processConfiguration();
  983.         vacmConfig.close();
  984.         System.exit(1);
  985.     }
  986. }