Cant connect to SNMP4J by firing snmpget command

My use case is:-

  1. I will have an snmpv3 agent running.
  2. The snmp manager will fire a snmpget command with proper creds and auth
  3. Application should accpt the get command and return appropriate value

I have tried a lotm but I can’t connect the agent using snmpget command. Please help what I am missing,

Agent is running. sudo nc -v -u -w 3 161 gives me following output

Connection to 161 port [udp/snmp] succeeded!

Here is my relevant piece of code:

SNMPV3UserInfo userInfo = new SNMPV3UserInfo();

MPv3 mp = new MPv3();

        ThreadPool threadPool = ThreadPool.create("SnmpGet", 10);
        MessageDispatcher dispatcher = new MultiThreadedMessageDispatcher(threadPool, new MessageDispatcherImpl());

        CommandResponder cr = new CommandResponder() {
            public void processPdu(CommandResponderEvent commandResponderEvent) {

        //byte[] egineId = MPv3.createLocalEngineID(new OctetString(hostIpAddress + UUID.randomUUID().toString()));

        Address address = new UdpAddress(InetAddress.getLocalHost(), 161);
        TransportMappings transportMappings = TransportMappings.getInstance();
        TransportMapping tm = transportMappings.createTransportMapping(address);

        this.userInfo = userInfo;
        snmp = new Snmp(dispatcher, tm);

        USM usm = new USM(SecurityProtocols.getInstance(), LOCAL_ENGINE_ID, 0);
        UsmUser user = new UsmUser(
                new OctetString(userInfo.getUsername()),
                getAuthProtocol(userInfo.getAythType()), new OctetString(userInfo.getAuthPswd()),
                getPrivProtocol(userInfo.getPrivType()), new OctetString(userInfo.getPrivPswd()));

        // Very important to add snmp as command responder which will finally process the PDU:
        snmp.getUSM().addUser(new OctetString(userInfo.getUsername()), user);

Here is the snmpget command I am using:-

sudo snmpget -u usr_1 -l authPriv -a MD5 -x DES -A temp_password -X temp_password -L eo

Your code does not implement a SNMP agent. You should use SNMP4J-AGENT for that.
I do not know what you expect to happen, but your code will not return any response PDU.

The command responder is added twice.

Thank you Frank.
Got a cue from your reply and implemented an agent. It works fine. I also used the SampleAgent that you have provided in the SNMP Agent Distro.

My Use case is:

  1. I have a number of xml files which has performance counters of my network. Each counter has an OID associated with it.
  2. The manager will fire a GET or a WALK with an OID.
  3. My agent would search in the XML files and return the value of it.

Now, my problem is I am not able to sent back a customized RESPONSE type PDU. Command responders are for processing incoming PDUs.

Can you please give me few advices?

Thanks. I have figured it out how to achieve this. I have extended MOScalar class and overridden the get and next methods.

Is that ok?

That is perfect if the "OID"s you refer to are as scalar OBJET-TYPEs. If they are conceptual tables, using the DefaultMOTable with one of the available TableModels would be better.

Thanks Frank, It works.
The only thing is that I dint want to include the column id to the table sub indexes. Is that Possible?

Yes, that should be the normal approach. Maybe you have to give us more details about

Column IDs are not part of INDEX clauses (never) because those represent to row IDs.

I am doing a walk with an oid say, “”. My indexes are 10, 20, 30 40 … and so on. So, the output of snmp walk should be,, and so on.

I have built a MOTable with the data. My column id is 1 and row id are 10, 20 , 30 40 etc… Whn I do snmp walk my my snmp walk out put values are, etc… I am getting an extra “1” which is the columnID.

In the code I found this following function

public OID getCellOID(OID index, int col) {
            OID retval = new OID(getOID());
            return retval;
I tried commenting "retval.append(getColumn(col).getColumnID());", snmpwalk doesnt walk.

My code is as follows.
MOTableIndex snmp4jDemoEntryIndex =
                    DefaultMOFactory.getInstance().createIndex(new MOTableSubIndex[] {
                                            SMIConstants.SYNTAX_INTEGER, 1, 1)
                            new MOTableIndexValidator() {
                                public boolean isValidIndex(OID index) {
                                    boolean isValidIndex = true;
                                    //--AgentGen BEGIN=snmp4jDemoEntry::isValidIndex
                                    //--AgentGen END
                                    return isValidIndex;

            MOMutableTableModel snmp4jDemoEntryModel =

                    MOMutableTableModel> snmp4jDemoEntry =
                    new MyMOtable(oid, snmp4jDemoEntryIndex, columns, snmp4jDemoEntryModel);

Where columns are:
new MOColumn[] {new MOMutableColumn(1,
                new Gauge32(1))};

The extra “1” table entry ID is required to comply with the SMMP standard! You can define a MIB specification where “,” is a table., would then be the table entry and, could the be a column in the table.

Then, could be a row in the table.

But, could never be a scalar because scalar instances must end with a zero instance identifier like,

Hi Frank. Thanks for your help. It works very well.