/* $Id: snmpset.src,v 1.6.2.8 2009/10/27 07:07:11 prathika Exp $ */ /* * @(#)snmpset.java * Copyright (c) 1996-2009 ZOHO Corp. All Rights Reserved. * Please read the associated COPYRIGHTS file for more details. */ /** * This is an example program to explain how to write an application to do * the basic SNMP operation SET using com.adventnet.snmp.snmp2 package of * WebNMS SNMP2 api. * The user could run this application by giving any one of the following usage. * * java snmpset [options] hostname oid value [oid value] ... * * v1 request: * java snmpset [-d] [-c community] [-wc writeCommunity] [-p port] [-t timeout] [-r retries] host [OID {INTEGER | STRING | GAUGE | TIMETICKS | OPAQUE | IPADDRESS | COUNTER | OID } value] ... * e.g. * java snmpset -p 161 -c public adventnet 1.1.0 advent-machine 1.4.0 contact-advent * * v2c request: * java snmpset [-d] [-v version(v1,v2)] [-c community] [-wc writeCommunity] [-p port] [-t timeout] [-r retries] host [OID {INTEGER | STRING | GAUGE | TIMETICKS | OPAQUE | IPADDRESS | COUNTER | OID } value] ... * e.g. For v1 request give -v v1 or drop the option -v . * java snmpset -p 161 -v v2 -c public adventnet 1.7.0 76 * * v3 request: * java snmpset [-d] [-v version(v1,v2,v3)] [-c community] [-p port] [-r retries] [-t timeout] [-u user] [-a auth_protocol] [-w auth_password] [-s priv_password] [-i context_id] [-pp privProtocol(DES/AES-128/AES-192/AES-256/3DES)] host [OID {INTEGER | STRING | GAUGE | TIMETICKS | OPAQUE | IPADDRESS | COUNTER | OID } value] ... * e.g. * java snmpset -v v3 -u initial2 -w initial2Pass -a MD5 10.3.2.120 1.5.0 STRING whatever * * If the oid is not starting with a dot (.) it will be prefixed by .1.3.6.1.2.1 . * So the entire OID of 1.1.0 will become .1.3.6.1.2.1.1.1.0 . You can also * give the entire OID . * * Options: * [-d] - Debug output. By default off. * [-c] - community String. By default "public". * [-wc] - write community String. By default "public". * [-p] - remote port no. By default 161. * [-t] - Timeout. By default 5000ms. * [-r] - Retries. By default 0. * [-v] - version(v1 / v2 / v3). By default v1. * [-u] - The v3 principal/userName * [-a] - The authProtocol(MD5/SHA). Mandatory if authPassword is specified * [-pp] - The authentication password. * [-s] - The privacy protocol password. Must be accompanied with auth password and authProtocol fields. * [-n] - The contextName to be used for the v3 pdu. * [-i] - The contextID to be used for the v3 pdu. * Mandatory - The RemoteHost (agent).Format (string without double qoutes/IpAddress). * Mandatory - Give multiple no. of Object Identifiers with type and value. * Mandatory - object type * Mandatory - The object instance value to be set . */ import java.lang.*; import java.util.*; import java.net.*; import com.adventnet.snmp.snmp2.*; import com.adventnet.snmp.snmp2.usm.*; public class snmpset { public static void main(String args[]) { // Take care of getting options String usage = "\nsnmpset [-d] [-v version(v1,v2,v3)] [-c community] \n" + "[-wc writeCommunity] [-p port] [-r retries] \n" + "[-t timeout] [-u user] [-a auth_protocol] \n" + "[-w auth_password] [-s priv_password] \n" + "[-n contextName] [-i contextID] \n" + "[-DB_driver database_driver]\n" + "[-DB_url database_url]\n" + "[-DB_username database_username]\n" + "[-DB_password database_password]\n" + "[-pp privProtocol(DES/AES-128/AES-192/AES-256/3DES)]]\n" + "host [OID {INTEGER | STRING | GAUGE | TIMETICKS | \n" + "OPAQUE | IPADDRESS | COUNTER | OID } value] ...\n"; String options[] = { "-d", "-c", "-wc", "-p", "-r", "-t", "-m", "-v", "-u", "-a", "-w", "-s", "-n", "-i", "-DB_driver", "-DB_url", "-DB_username", "-DB_password","-pp" }; String values[] = { "None", null, null, null, null, null, "None", null, null, null, null, null, null, null, null, null, null, null,null }; ParseOptions opt = new ParseOptions(args,options,values,usage); if (opt.remArgs.length<1) { opt.usage_error(); } // Start SNMP API SnmpAPI api; api = new SnmpAPI(); if (values[0].equals("Set")) api.setDebug( true ); // Open session SnmpSession session = new SnmpSession(api); int PORT = 3; SnmpPDU pdu = new SnmpPDU(); UDPProtocolOptions ses_opt = new UDPProtocolOptions(); ses_opt.setRemoteHost(opt.remArgs[0]); if(values[PORT] != null) { try { ses_opt.setRemotePort(Integer.parseInt(values[PORT])); } catch(Exception exp) { System.out.println("Invalid port: " + values[PORT]); System.exit(1); } } pdu.setProtocolOptions(ses_opt); // set values SetValues setVal = new SetValues(session, values); if(setVal.usage_error) { opt.usage_error(); } String driver = values[14]; String url = values[15]; String username = values[16]; String password = values[17]; if(driver != null || url != null || username != null || password != null) { if(session.getVersion() != 3) { System.out.println( "Database option can be used only for SNMPv3."); System.exit(1); } if(driver == null) { System.out.println( "The Database driver name should be given."); System.exit(1); } if(url == null) { System.out.println("The Database URL should be given."); System.exit(1); } try { api.setV3DatabaseFlag(true); api.initJdbcParams(driver, url, username, password); } catch(Exception exp) { System.out.println("Unable to Establish Database Connection."); System.out.println("Please check the driverName and url."); System.exit(1); } } // Build set request PDU // SnmpPDU pdu = new SnmpPDU(); pdu.setCommand( api.SET_REQ_MSG ); // add Variable Bindings for (int i=1;i