/*$Id: snmpgetbulk.src,v 1.3.2.5 2009/10/12 11:56:26 prathika Exp $*/ /* * @(#)snmpgetbulk.java * Copyright (c) 1996-2009 ZOHO Corp. All Rights Reserved. * Please read the associated COPYRIGHTS file for more details. */ /** * Do an SNMP Getbulk based on command line arguments. Loads MIBs * as specified, and converts to/from names for loaded MIB data. * * [-d] - Debug output. By default off. * [-c] - community String. By default "public". * [-p] - remote port no. By default 161. * [-t] - Timeout. By default 5000ms. * [-r] - Retries. By default 0. * [-m] - The mibs to be loaded. * - non-repeaters. * - max-repetitions. * [-u] - The v3 principal/userName * [-a] - The authProtocol(MD5/SHA). Mandatory if authPassword is specified * [-w] - The authentication password. * [-s] - The privacy protocol password. Must be accompanied with auth password and authProtocol fields. * [-n] - The snmpv3 contextName to be used. * [-i] - The snmpv3 contextID to be used. * [-pp] - The privacy protocol . Must be accompanied with auth,priv password and authProtocol fields. * Mandatory - The RemoteHost (agent).Format (string without double qoutes/IpAddress). * Mandatory - Give only one Object Identifier. */ import com.adventnet.snmp.beans.*; import com.adventnet.snmp.snmp2.*; public class snmpgetbulk { private static final int VERSION = 0; private static final int COMMUNITY = 1; private static final int PORT = 2; private static final int RETRIES = 3; private static final int TIMEOUT = 4; private static final int DEBUG = 5; private static final int MIBS = 6; private static final int NON_REPETERS = 7; private static final int MAX_REPETITIONS = 8; private static final int USER_NAME = 9; private static final int AUTH_PROTOCOL = 10; private static final int AUTH_PASSWORD = 11; private static final int PRIV_PASSWORD = 12; private static final int CONTEXT_NAME = 13; private static final int CONTEXT_ID = 14; private static final int PRIV_PROTOCOL = 15; public static void main(String args[]) { // Take care of getting options String usage = "snmpgetbulk [-v version(v2,v3)] [-m MIB_files] [-c community] [-p port] [-t timeout] [-r retries] [-d debug] [-nr non-repeaters] [-mr max-repetitions] [-u user] [-a auth_protocol(MD5/SHA)] [-w auth_password] [-s priv_password] [-n contextName] [-i contextID] [ -pp priv_protocol (DES/AES-128/AES-192/AES-256/3DES) ] host OID [OID] ..."; String options[] = { "-v", "-c", "-p", "-r", "-t", "-d", "-m", "-nr", "-mr", "-u", "-a", "-w", "-s", "-n", "-i", "-pp" }; String values[] = { null, null, null, null, null, "None", null, null, null, null, null, null, null, null, null, null}; String authProtocol = new String("NO_AUTH"); ParseOptions opt = new ParseOptions(args,options,values, usage); // check for at least hostname and one OID in remaining arguments if (opt.remArgs.length<2) opt.usage_error(); if(values[VERSION] != null) { if ((values[VERSION].equals("v3")) && (values[USER_NAME] == null)) opt.usage_error(); } // Use an SNMP target bean to perform SNMP operations SnmpTarget target = new SnmpTarget(); //To load MIBs from compiled file target.setLoadFromCompiledMibs(true); if(values[DEBUG].equals("Set")) target.setDebug(true); target.setSnmpVersion( SnmpTarget.VERSION2C ) ; int nonRepeaters = 0; // we need this for printing if(values[VERSION] != null) { // if SNMP version is specified, set it if(values[VERSION].equals("v2")) target.setSnmpVersion( SnmpTarget.VERSION2C ) ; else if(values[VERSION].equals("v3")) target.setSnmpVersion( SnmpTarget.VERSION3 ) ; else { System.out.println("Invalid Version Number"); System.exit(1); } } else target.setSnmpVersion( SnmpTarget.VERSION2C ) ; target.setTargetHost( opt.remArgs[0] ); // set the agent hostname if (values[COMMUNITY] != null) // set the community if specified target.setCommunity( values[COMMUNITY] ); try { // set the timeout/retries/port parameters, if specified if (values[PORT] != null) target.setTargetPort( Integer.parseInt(values[PORT]) ); if (values[RETRIES] != null) target.setRetries( Integer.parseInt(values[RETRIES]) ); if (values[TIMEOUT] != null) target.setTimeout( Integer.parseInt(values[TIMEOUT]) ); if (values[NON_REPETERS] != null) nonRepeaters = Integer.parseInt(values[NON_REPETERS]); target.setNonRepeaters( nonRepeaters ); if (values[MAX_REPETITIONS] != null) target.setMaxRepetitions( Integer.parseInt(values[MAX_REPETITIONS]) ); } catch (NumberFormatException ex) { System.err.println("Invalid Integer Argument "+ex); } if(target.getSnmpVersion() == target.VERSION3) { if (values[USER_NAME] != null) target.setPrincipal(values[USER_NAME]); if (values[AUTH_PROTOCOL] != null) { //System.out.println("authProtocol = " + authProtocol); authProtocol = values[AUTH_PROTOCOL]; } if(authProtocol.equals("SHA")) target.setAuthProtocol(target.SHA_AUTH); else if(authProtocol.equals("MD5")) target.setAuthProtocol(target.MD5_AUTH); else target.setAuthProtocol(target.NO_AUTH); if (values[AUTH_PASSWORD] != null) target.setAuthPassword(values[AUTH_PASSWORD]); if (values[PRIV_PASSWORD] != null) { target.setPrivPassword(values[PRIV_PASSWORD]); if(values[PRIV_PROTOCOL] != null) { if(values[PRIV_PROTOCOL].equals("AES-128")) { target.setPrivProtocol(target.CFB_AES_128); } else if(values[PRIV_PROTOCOL].equals("AES-192")) { target.setPrivProtocol(target.CFB_AES_192); } else if(values[PRIV_PROTOCOL].equals("AES-256")) { target.setPrivProtocol(target.CFB_AES_256); } else if(values[PRIV_PROTOCOL].equals("3DES")) { target.setPrivProtocol(target.CBC_3DES); } else if(values[PRIV_PROTOCOL].equals("DES")) { target.setPrivProtocol(target.CBC_DES); } else { System.out.println(" Invalid PrivProtocol "+values[PRIV_PROTOCOL]); opt.usage_error(); } } } if(values[CONTEXT_NAME]!= null) target.setContextName(values[CONTEXT_NAME]); if(values[CONTEXT_ID]!= null) target.setContextID(values[CONTEXT_ID]); } if (values[MIBS] != null) try { // Load the MIB files System.err.println("Loading MIBs: "+values[MIBS]); target.loadMibs(values[MIBS]); } catch (Exception ex) { System.err.println("Error loading MIBs: "+ex); } if(target.getSnmpVersion() == target.VERSION3){ target.create_v3_tables(); } // create OID array from command line arguments String oids[] = new String[opt.remArgs.length-1]; for (int i=1;i