SetValues.java
上传用户:aonuowh
上传日期:2021-05-23
资源大小:35390k
文件大小:10k
- /*
- $Id: SetValues.src,v 1.7.6.3 2008/11/05 18:37:15 prathika Exp $
- */
- /*
- * @(#)SetValues.java
- * Copyright (c) 1996-2002 AdventNet, Inc. All Rights Reserved.
- * Please read the associated COPYRIGHTS file for more details.
- */
- import java.lang.*;
- import com.adventnet.snmp.snmp2.*;
- import com.adventnet.snmp.snmp2.usm.*;
- public class SetValues {
-
- boolean usage_error = false;
-
- private static final int COMMUNITY = 1;
- private static final int WRITE_COMMUNITY = 2;
- private static final int PORT = 3;
- private static final int RETRIES = 4;
- private static final int TIMEOUT = 5;
- //MIBS = 6 is for MIB option );
- private static final int VERSION = 7;
- private static final int USER_NAME = 8;
- private static final int AUTH_PROTOCOL = 9;
- private static final int AUTH_PASSWORD = 10;
- private static final int PRIV_PASSWORD = 11;
- private static final int CONTEXT_NAME = 12;
- private static final int CONTEXT_ID = 13;
- private static int PRIV_PROTOCOL;
- String userName = new String("");
- int authProtocol = USMUserEntry.NO_AUTH;
- int privProtocol = USMUserEntry.NO_PRIV;
- String authPassword = new String ("");
- String privPassword = new String ("");
- String contextName = new String ("");
- String contextID = new String ("");
- String[] val;
-
- public SetValues(SnmpSession session, String values[]) {
- val =new String [19];
- if(values.length==19){
- PRIV_PROTOCOL=18;
- val=values ;
- if(values[PRIV_PROTOCOL] == null ){
- values[PRIV_PROTOCOL] = "DES";
- }
- }
- else
- {
- System.arraycopy(values,0,val,0,values.length);
- PRIV_PROTOCOL= values.length-1;
-
- }
-
- // set community & writecommunity
- if (values[COMMUNITY] != null)
- session.setCommunity( values[COMMUNITY] );
- if (values[WRITE_COMMUNITY] != null)
- session.setWriteCommunity( values[WRITE_COMMUNITY] );
-
- //set remote Port, timeout,retries if needed.
- try {
- if (values[PORT] != null)
- session.setRemotePort( Integer.parseInt(values[PORT]) );
- if (values[RETRIES] != null)
- session.setRetries( Integer.parseInt(values[RETRIES]) );
- if (values[TIMEOUT] != null)
- session.setTimeout( Integer.parseInt(values[TIMEOUT]) );
- }
- catch (NumberFormatException ex) {
- System.err.println("Invalid Integer Arg : " + ex.getMessage());
- System.exit(1);
- }
- if(values[VERSION] != null) {
- if(values[VERSION].equals("v2"))
- session.setVersion( SnmpAPI.SNMP_VERSION_2C ) ;
- else if(values[VERSION].equals("v1"))
- session.setVersion( SnmpAPI.SNMP_VERSION_1 );
- else if(values[VERSION].equals("v3"))
- session.setVersion( SnmpAPI.SNMP_VERSION_3 );
- else {
- System.out.println("Invalid Version Number");
- usage_error = true;
- }
- }
-
- if(session.getVersion()==SnmpAPI.SNMP_VERSION_3) {
- if (values[USER_NAME] != null) {
- userName = values[USER_NAME];
- if ((values[AUTH_PROTOCOL] != null) && (values[AUTH_PASSWORD] != null) && (val[PRIV_PROTOCOL] != null)) {
- if(values[AUTH_PROTOCOL].equals("SHA"))
- {
- authProtocol = USMUserEntry.SHA_AUTH;
- }
- else if(values[AUTH_PROTOCOL].equals("MD5"))
- {
- authProtocol = USMUserEntry.MD5_AUTH;
- }
- else
- {
- System.out.println("The AuthProtocol should be " +
- "either MD5 or SHA");
- usage_error = true;
- }
- if(values[PRIV_PROTOCOL].equals("AES-128"))
- {
- privProtocol = USMUserEntry.CFB_AES_128;
- }
- else if(values[PRIV_PROTOCOL].equals("AES-192"))
- {
- privProtocol = USMUserEntry.CFB_AES_192;
- }
- else if(values[PRIV_PROTOCOL].equals("AES-256"))
- {
- privProtocol = USMUserEntry.CFB_AES_256;
- }
- else if(values[PRIV_PROTOCOL].equals("3DES"))
- {
- privProtocol = USMUserEntry.CBC_3DES;
- }
- else if(values[PRIV_PROTOCOL].equals("DES"))
- {
- privProtocol = USMUserEntry.CBC_DES;
- }
- else
- {
- System.out.println("The PrivProtocol should be " +
- "either AES or DES");
- usage_error = true;
- }
- if(authProtocol==USMUserEntry.NO_AUTH)
- {
- System.err.println("Enter authentication protocol");
- usage_error = true;
- }
- authPassword = values[AUTH_PASSWORD];
- if (values[PRIV_PASSWORD] != null)
- {
- privPassword = values[PRIV_PASSWORD];
- }
- }
- else if ((values[AUTH_PROTOCOL] != null) && (values[AUTH_PASSWORD] != null)) {
- if(values[AUTH_PROTOCOL].equals("SHA"))
- {
- authProtocol = USMUserEntry.SHA_AUTH;
- }
- else if(values[AUTH_PROTOCOL].equals("MD5"))
- {
- authProtocol = USMUserEntry.MD5_AUTH;
- }
- else
- {
- System.out.println("The AuthProtocol should be " +
- "either MD5 or SHA");
- usage_error = true;
- }
- if(authProtocol==USMUserEntry.NO_AUTH)
- {
- System.err.println("Enter authentication protocol");
- usage_error = true;
- }
- authPassword = values[AUTH_PASSWORD];
- if (values[PRIV_PASSWORD] != null)
- {
- privPassword = values[PRIV_PASSWORD];
- }
- }
- else if ((values[AUTH_PROTOCOL] != null) || (values[AUTH_PASSWORD] != null) || (values[PRIV_PASSWORD] != null)) {
- usage_error = true;
- }
- if (values[CONTEXT_NAME] != null) {
- contextName = values[CONTEXT_NAME];
- }
- if (values[CONTEXT_ID] != null) {
- contextID = values[CONTEXT_ID];
- }
- }
- else {
- System.err.println("UserName Missing");
- usage_error = true;
- }
- }
- }
- public SetValues(SnmpPDU pdu, String[] values)
- {
- val =new String [19];
- if(values.length==19){
- PRIV_PROTOCOL=18;
- val=values ;
- if(values[PRIV_PROTOCOL] == null ){
- values[PRIV_PROTOCOL] = "DES";
- }
- }
- else
- {
- System.arraycopy(values,0,val,0,values.length);
- PRIV_PROTOCOL=values.length-1;
-
- }
- // set community & writecommunity
- if (values[COMMUNITY] != null)
- pdu.setCommunity( values[COMMUNITY] );
- if (values[WRITE_COMMUNITY] != null)
- pdu.setWriteCommunity( values[WRITE_COMMUNITY] );
-
- //set remote Port, timeout,retries if needed.
- try {
- if (values[RETRIES] != null)
- pdu.setRetries( Integer.parseInt(values[RETRIES]) );
- if (values[TIMEOUT] != null)
- pdu.setTimeout( Integer.parseInt(values[TIMEOUT]) );
- }
- catch (NumberFormatException ex) {
- System.err.println("Invalid Integer Arg : " + ex.getMessage());
- System.exit(1);
- }
- if(values[VERSION] != null) {
- if(values[VERSION].equals("v2"))
- pdu.setVersion( SnmpAPI.SNMP_VERSION_2C ) ;
- else if(values[VERSION].equals("v1"))
- pdu.setVersion( SnmpAPI.SNMP_VERSION_1 );
- else if(values[VERSION].equals("v3"))
- pdu.setVersion( SnmpAPI.SNMP_VERSION_3 );
- else {
- System.out.println("Invalid Version Number");
- usage_error = true;
- }
- }
-
- if(pdu.getVersion()==SnmpAPI.SNMP_VERSION_3) {
- if (values[USER_NAME] != null) {
- userName = values[USER_NAME];
- if ((values[AUTH_PROTOCOL] != null) && (values[AUTH_PASSWORD] != null) && (val[PRIV_PROTOCOL] != null)) {
- if(values[AUTH_PROTOCOL].equals("SHA"))
- {
- authProtocol = USMUserEntry.SHA_AUTH;
- }
- else if(values[AUTH_PROTOCOL].equals("MD5"))
- {
- authProtocol = USMUserEntry.MD5_AUTH;
- }
- else
- {
- System.out.println("The AuthProtocol should be " +
- "either MD5 or SHA");
- usage_error = true;
- }
- if(values[PRIV_PROTOCOL].equals("AES-128"))
- {
- privProtocol = USMUserEntry.CFB_AES_128;
- }
- else if(values[PRIV_PROTOCOL].equals("AES-192"))
- {
- privProtocol = USMUserEntry.CFB_AES_192;
- }
- else if(values[PRIV_PROTOCOL].equals("AES-256"))
- {
- privProtocol = USMUserEntry.CFB_AES_256;
- }
- else if(values[PRIV_PROTOCOL].equals("3DES"))
- {
- privProtocol = USMUserEntry.CBC_3DES;
- }
- else if(values[PRIV_PROTOCOL].equals("DES"))
- {
- privProtocol = USMUserEntry.CBC_DES;
- }
- else
- {
- System.out.println("The PrivProtocol should be " +
- "either AES or DES");
- usage_error = true;
- }
- if(authProtocol==USMUserEntry.NO_AUTH)
- {
- System.err.println("Enter authentication protocol");
- usage_error = true;
- }
- authPassword = values[AUTH_PASSWORD];
- if (values[PRIV_PASSWORD] != null)
- {
- privPassword = values[PRIV_PASSWORD];
- }
- }
- else if ((values[AUTH_PROTOCOL] != null) && (values[AUTH_PASSWORD] != null)) {
- if(values[AUTH_PROTOCOL].equals("SHA"))
- {
- authProtocol = USMUserEntry.SHA_AUTH;
- }
- else if(values[AUTH_PROTOCOL].equals("MD5"))
- {
- authProtocol = USMUserEntry.MD5_AUTH;
- }
- else
- {
- System.out.println("The AuthProtocol should be " +
- "either MD5 or SHA");
- usage_error = true;
- }
- if(authProtocol==USMUserEntry.NO_AUTH)
- {
- System.err.println("Enter authentication protocol");
- usage_error = true;
- }
- authPassword = values[AUTH_PASSWORD];
- if (values[PRIV_PASSWORD] != null)
- {
- privPassword = values[PRIV_PASSWORD];
- }
- }
- else if ((values[AUTH_PROTOCOL] != null) || (values[AUTH_PASSWORD] != null) || (values[PRIV_PASSWORD] != null)) {
- usage_error = true;
- }
- if (values[CONTEXT_NAME] != null) {
- contextName = values[CONTEXT_NAME];
- }
- if (values[CONTEXT_ID] != null) {
- contextID = values[CONTEXT_ID];
- }
- }
- else {
- System.err.println("UserName Missing");
- usage_error = true;
- }
- }
- }
- }