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 172.24.80.1 161 gives me following output

Connection to 127.0.0.1 161 port [udp/snmp] succeeded!

Here is my relevant piece of code:

SNMPV3UserInfo userInfo = new SNMPV3UserInfo();
		userInfo.setUsername("usr_1");
		userInfo.setAddress("100.94.135.193");
		userInfo.setAuthPswd("temp_password");
		userInfo.setPrivPswd("temp_password");

MPv3 mp = new MPv3();
        mp.setLocalEngineID(LOCAL_ENGINE_ID.getValue());

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

        CommandResponder cr = new CommandResponder() {
            @Override
            public void processPdu(CommandResponderEvent commandResponderEvent) {
                System.out.println("..............1Anirban1...........");
            }
        };

        //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);
        snmp.getMessageDispatcher().addMessageProcessingModel(mp);

        USM usm = new USM(SecurityProtocols.getInstance(), LOCAL_ENGINE_ID, 0);
        SecurityModels.getInstance().addSecurityModel(usm);
        snmp.getMessageDispatcher().addMessageProcessingModel(mp);
        snmp.getMessageDispatcher().addCommandResponder(cr);
        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.getMessageDispatcher().addCommandResponder(snmp);
        snmp.getUSM().addUser(new OctetString(userInfo.getUsername()), user);
        snmp.listen();

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  127.0.0.1 1.3.6.1.4.1.27675.20.10.1.2.0

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, “1.3.6.1.2.1.31.1.1.1.17”. My indexes are 10, 20, 30 40 … and so on. So, the output of snmp walk should be 1.3.6.1.2.1.31.1.1.1.17.10, 1.3.6.1.2.1.31.1.1.1.17.20, 1.3.6.1.2.1.31.1.1.1.17.30 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 1.3.6.1.2.1.31.1.1.1.17.1.10,1.3.6.1.2.1.31.1.1.1.17.1.20 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());
            retval.append(getColumn(col).getColumnID());
            retval.append(index);
            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[] {
                                    DefaultMOFactory.getInstance().createSubIndex(oid,
                                            SMIConstants.SYNTAX_INTEGER, 1, 1)
                            },
                            true,
                            new MOTableIndexValidator() {
                                public boolean isValidIndex(OID index) {
                                    boolean isValidIndex = true;
                                    //--AgentGen BEGIN=snmp4jDemoEntry::isValidIndex
                                    //--AgentGen END
                                    return isValidIndex;
                                }
                            });

            MOMutableTableModel snmp4jDemoEntryModel =
                    DefaultMOFactory.getInstance().createTableModel(oid,
                            snmp4jDemoEntryIndex,
                            columns);

            MOTable<DefaultMOMutableRow2PC,
                    MOColumn,
                    MOMutableTableModel> snmp4jDemoEntry =
                    new MyMOtable(oid, snmp4jDemoEntryIndex, columns, snmp4jDemoEntryModel);

Where columns are:
new MOColumn[] {new MOMutableColumn(1,
                SMIConstants.SYNTAX_GAUGE32,
                DefaultMOFactory.getInstance().createAccess(MOAccessImpl.ACCESSIBLE_FOR_READ_CREATE),
                new Gauge32(1))};

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

Then 1.3.6.1.2.1.31.1.1.1.17.1.10,1.3.6.1.2.1.31.1.1.1.17.20 could be a row in the table.

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

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