- /*
- $Id: SetValues.src,v 1.7 2002/09/09 05:38:14 parasuraman Exp $
- */
- /*
- * @(#)SetValues.java
- * Copyright (c) 1996-2003 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;
- String userName = new String("");
- int authProtocol = USMUserEntry.NO_AUTH;
- String authPassword = new String ("");
- String privPassword = new String ("");
- String contextName = new String ("");
- String contextID = new String ("");
- public SetValues(SnmpSession session, String values[]) {
- // 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)) {
- 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)
- {
- // 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)) {
- 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;
- }
- }
- }
- }