/* $Id: snmpv3trap.src,v 1.4.2.13 2009/10/27 07:07:11 prathika Exp $ */ /* * @(#)snmpv3trap.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 send a * v3 Trap message 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 snmpv3trap [-d] [-p port] [-e engineID(0x....)] [-a auth_protocol] [-w auth_password] [-s priv_password] [-i context_id] userName[-pp privProtocol(DES/AES-128/AES-192/AES-256/3DES)] host TimeTicksvalue OIDvalue [OID {INTEGER | STRING | GAUGE | TIMETICKS | OPAQUE | IPADDRESS | COUNTER | COUNTER64 | UNSIGNED32} value] ... * e.g. * java snmpv3trap -e 0x000012141516171819202121 -a MD5 -w initial2Pass -i initial initial2 10.3.2.120 16352 .1.3.6.1.4.1.2162.1000.2 .1.3.6.1.4.1.2162.1001.21.0 STRING TrapTest * * 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. * [-p] - remote port no. By default 162. * [-e] - Engine ID. * [-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 contextName to be used for the v3 pdu. * [-i] - The contextID to be used for the v3 pdu. * - The v3 principal/userName. Mandatory. * Mandatory - the value of object sysUpTime when the event occurred * Mandatory - Object Identifier * Mandatory - The RemoteHost (agent).Format (string without double qoutes/IpAddress). * Mandatory - Object Identifier. * 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 snmpv3trap { private static final int DEBUG = 0; private static final int PORT = 1; private static final int AUTH_PROTOCOL = 2; private static final int AUTH_PASSWORD = 3; private static final int PRIV_PASSWORD = 4; private static final int CONTEXT_NAME = 5; private static final int CONTEXT_ID = 6; private static final int ENGINEID = 7; static final int USM_SECURITY_MODEL = 3; private static final int PRIV_PROTOCOL=9; public static void main(String args[]) { // Take care of getting options String usage = "snmpv3trap [-d] [-p port][-g agent-address][-e engineID(0x....)] [-a auth_protocol] [-w auth_password] [-s priv_password] [-n contextName] [-i contextID]\n[-pp privProtocol(DES/AES-128/AES-192/AES-256/3DES)] userName host TimeTicksvalue OIDvalue [OID {INTEGER | STRING | GAUGE | TIMETICKS | OPAQUE | IPADDRESS | COUNTER | COUNTER64 | UNSIGNED32} value] ..."; String options[] = { "-d", "-p", "-a", "-w", "-s", "-n", "-i", "-e" ,"-g", "-pp"}; String values[] = { "None", null, null, null, null, null, null, null,null, null }; String id = new String(""); 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 (""); ParseOptions opt = new ParseOptions(args,options,values, usage); if (opt.remArgs.length<4) opt.usage_error(); // Start SNMP API SnmpAPI api; api = new SnmpAPI(); if (values[0].equals("Set")) api.setDebug( true ); SnmpPDU pdu = new SnmpPDU(); Snmp3Message msg = (Snmp3Message)(pdu.getMsg()); pdu.setCommand( api.TRP2_REQ_MSG ); // Open session SnmpSession session = new SnmpSession(api); // set remoteHost UDPProtocolOptions ses_opt = new UDPProtocolOptions(opt.remArgs[1]); // set version session.setVersion( SnmpAPI.SNMP_VERSION_3 ) ; try { if(values[PORT] != null) ses_opt.setRemotePort( Integer.parseInt(values[PORT]) ); else ses_opt.setRemotePort(162); if (values[ENGINEID]!=null) { id = values[ENGINEID]; if(id.startsWith("0x") || id.startsWith("0X")) id = new String(gethexValue(values[ENGINEID])); } } catch (NumberFormatException ex) { System.err.println("Invalid Integer Arg"); } catch (StringIndexOutOfBoundsException sie){ System.err.println("Invalid engineID. Please specify proper" + " hex value. Exception = " + sie); opt.usage_error(); } session.setProtocolOptions(ses_opt); userName = opt.remArgs[0]; if ((values[AUTH_PROTOCOL] != null) && (values[AUTH_PASSWORD] != null)) { if(values[AUTH_PROTOCOL].equals("SHA")) authProtocol = USMUserEntry.SHA_AUTH; else authProtocol = USMUserEntry.MD5_AUTH; if(authProtocol==USMUserEntry.NO_AUTH){ System.err.println("Enter authentication protocol"); opt.usage_error(); } authPassword = values[AUTH_PASSWORD]; if (values[PRIV_PASSWORD] != null) { privPassword = values[PRIV_PASSWORD]; if(values[PRIV_PROTOCOL] !=null) { if(values[PRIV_PROTOCOL].equals("DES")) { privProtocol=USMUserEntry.CBC_DES; } else 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 { System.out.println(" Invalid privProtocol "); opt.usage_error(); } } else { System.out.println(" Please specify the privProtocol value "); opt.usage_error(); } } } else if ((values[AUTH_PROTOCOL] != null) || (values[AUTH_PASSWORD] != null) || (values[PRIV_PASSWORD] != null)) { opt.usage_error(); } if (values[CONTEXT_NAME] != null) contextName = values[CONTEXT_NAME]; if (values[CONTEXT_ID] != null) contextID = values[CONTEXT_ID]; createUSMTable(userName.getBytes(), id.getBytes(), authProtocol, authPassword, privPassword, api,privProtocol); pdu.setUserName(userName.getBytes()); // Build trap request PDU // Adding the sysUpTime variable binding SnmpOID oid = new SnmpOID(".1.3.6.1.2.1.1.3.0"); if (oid.toValue() == null) System.err.println("Invalid OID argument: .1.3.6.1.2.1.1.3.0"); else { SnmpVar var = null ; try { var = SnmpVar.createVariable(opt.remArgs[2], SnmpAPI.TIMETICKS); } catch (SnmpException e) { System.err.println("Cannot create variable: " + oid +" with value: "+opt.remArgs[1]); } SnmpVarBind varbind = new SnmpVarBind(oid, var); pdu.addVariableBinding(varbind); } // Adding the snmpTrapOID variable binding oid = new SnmpOID(".1.3.6.1.6.3.1.1.4.1.0"); if (oid.toValue() == null) System.err.println("Invalid OID argument: " + ".1.3.6.1.6.3.1.1.4.1.0"); else { SnmpVar var = null ; try { var = SnmpVar.createVariable(opt.remArgs[3], SnmpAPI.OBJID); } catch (SnmpException e) { System.err.println("Cannot create variable: " + oid +" with value: "+opt.remArgs[2]); } SnmpVarBind varbind = new SnmpVarBind(oid, var); pdu.addVariableBinding(varbind); } String agentAddress=""; int otherVarBinds=0; for (int i=4;i0)) { byte[] tempKey = USMUtils.password_to_key(authProtocol, privPassword.getBytes(), privPassword.getBytes().length, engineID,privProtocol); entry.setPrivProtocol(privProtocol); byte privKey[]=null; if(privProtocol==USMUserEntry.CFB_AES_192) { privKey=new byte[24]; System.arraycopy(tempKey,0,privKey,0,24); } else if(privProtocol==USMUserEntry.CFB_AES_256) { privKey =new byte[32]; System.arraycopy(tempKey,0,privKey,0,32); } else if(privProtocol==USMUserEntry.CBC_3DES) { privKey =new byte[32]; System.arraycopy(tempKey,0,privKey,0,32); } else { privKey=new byte[16]; System.arraycopy(tempKey,0,privKey,0,16); } entry.setPrivKey(privKey); level |= 2; } } entry.setSecurityLevel(level); uut.addEntry(entry); api.setSnmpEngineID(engineID); } }