when using snmp4j simulate snmpv3's pdu, found although add not-existed oid variable binding list, still valid response.
but when protocol version v1, i got error code 2 , means 'no such name'.
when protocol version v2c, i didn't error, , returned vb list size 1, , list(0) variable binding 's syntax exception ,which means "no such object".
can figure out whether exists or not when using v1 or v2c, when it's v3, got following response:
snmp-user-based-sm-mib::usmstatsunknownengineids.0 = counter32: xx
**xx number.
the code :
import org.snmp4j.*; import org.snmp4j.event.responseevent; import org.snmp4j.mp.snmpconstants; import org.snmp4j.security.*; import org.snmp4j.smi.*; import org.snmp4j.transport.defaultudptransportmapping; import static org.snmp4j.mp.mpv3.createlocalengineid; /** * * following syntax may helpfull: smiconstants * public static final int exception_no_such_object = ber.nosuchobject; * public static final int exception_no_such_instance = ber.nosuchinstance; * public static final int exception_end_of_mib_view = ber.endofmibview; * * give string , test whether exists or not * * conclusions: * (1)when op get, * v1: * errorcode @ pdu, , 2, means "no such name" * v2c: * not error, , returned vb list size 1, , list(0) null.isexceptionsyntax(vb.getsyntax()) == true // means "no such object" * v3: * not error, : snmp-user-based-sm-mib::usmstatsunknownengineids.0 = counter32: 23 * returned varbingds : 1.3.6.1.6.3.15.1.1.3.0 = xxx * (2)when op getnext: * v1: * errorcode @ pdu, , 2, means "no such name" * v2c: * not error, , returned vb list size 1, , list(0) null.isexceptionsyntax(vb.getsyntax()) == true // means "endofmibview" * v3: * not error, : snmp-user-based-sm-mib::usmstatsunknownengineids.0 = counter32: 23 * (3)when op getbulk: * * created edward.gao on 8/9/16. */ public class testoidexists { private static final string _v1_or_v2_communityname= "public"; private static final string _v3_username = "edward"; /** * authentication protocol md5 or sha */ private static final string _v3_authentication_passphrase = "auth12345678"; /** * privacy protocol des or aes */ private static final string _v3_privacy_passphrase = "encry12345678"; public static void main(string[] args) throws exception{ final string addrstring= "udp:192.168.170.150/161"; byte [] localengineid = createlocalengineid(); //version3 need usm usm usm = new usm(securityprotocols.getinstance(),new octetstring(localengineid), 0); securitymodels.getinstance().addsecuritymodel(usm); snmp snmp = new snmp(new defaultudptransportmapping()); snmp.listen(); string [] oids = new string[] {/*".1.3.6.1.2.1.1.5.0", ".1.3.6.1.2.1.25.6.3.1.2", ".1.3.6.1.2.1.1.5",*/ ".1.3.999"}; string [] types = new string[]{ "get","getnext", "getbulk"}; (string oid : oids) { (string type : types) { system.out.println(string.format("version 3, oid:%s result: %d", oid, typeofoid(oid, snmp, snmpconstants.version3, type))); system.out.println(string.format("version 2, oid:%s result: %d", oid, typeofoid(oid, snmp, snmpconstants.version2c, type))); system.out.println(string.format("version 1, oid:%s result: %d", oid, typeofoid(oid, snmp, snmpconstants.version1, type))); } } } /** * type of matched oid string * @param oid * @param snmp * @return -1 : not exist , 0, leaf, 1, tree */ public static int typeofoid(string oid , snmp snmp, int version, string typestring) throws exception { boolean isv3 = version == snmpconstants.version3; //build client address addr = genericaddress.parse("192.168.170.150/161"); target target = null; if (isv3) { usmuser usmuser = new usmuser(new octetstring(_v3_username), authmd5.id, new octetstring(_v3_authentication_passphrase), privdes.id, new octetstring(_v3_privacy_passphrase) ); snmp.getusm().adduser(usmuser); target = new usertarget(); } else { target = new communitytarget(); //the default version 3 if (version == snmpconstants.version2c) { target.setversion(snmpconstants.version2c); } else { target.setversion(snmpconstants.version1); } ((communitytarget)target).setcommunity(new octetstring("public")); } target.setaddress(addr); target.setretries(2); target.settimeout(30000); int type = typestring.equalsignorecase("get") ? pdu.get : (typestring.equalsignorecase("getnext") ? pdu.getnext : pdu.getbulk); system.out.println("using version " + (version == snmpconstants.version1 ? "v1" :(version == snmpconstants.version2c ? "v2c " : "v3 ")) + " type:" + typestring); pdu pdu = version == snmpconstants.version3 ? new scopedpdu() : new pdu(); pdu.settype(type); pdu.add(new variablebinding(new oid(oid))); pdu.setrequestid(new integer32(0)); responseevent responseevent = snmp.send(pdu, target); if (responseevent.geterror() == null) { pdu responsepdu = responseevent.getresponse(); if (responsepdu.geterrorstatus() == 0) { responsepdu.getvariablebindings().foreach(vb ->system.out.print(vb + " " + null.isexceptionsyntax(vb.getsyntax()))); system.out.println(); //leaf one, other more 1 return responsepdu.getvariablebindings().size() == 1 ? 0 : 1; } else { system.out.println("error code @ pdu," + responsepdu.geterrorstatus() + " text:" + responsepdu.geterrorstatustext() + " index:" + responsepdu.geterrorindex()); } } else { system.out.println("error happend,"); responseevent.geterror().printstacktrace(); } return -1; } }
so question :
(1)is behavior of v3 correctly implemented ? why didn't kind of error?
(2)how test whether oid exists or not using snmp4j?
you receiving called snmp report pdu. happens agent receives request pdu manager , determines important security attributes engine_id
, engine_boots
, engine_time
missing , sends report notify manager that. manager should process report pdu in full accordance called snmp engine discovery procedure described in details in https://tools.ietf.org/html/rfc5343.
Comments
Post a Comment