SNMP++ set value OID Wrong Type

Hi

I am using SNMP++ on Linux and with QT 5.15.1.

Snmp get works with all data types (int, oid, octetStr and timeTicks). Snmp set works with integers as well.
Setting an oid variable does not work. I get the error SNMP: Cannot create/modify variable, Wrong Type
Setting the value over a windows device with this tool works fine (SnmpWalk (Command-line Tool) | SNMPSoft Tools | EZ5 Systems)
We use the RFC 1628 MIB (rfc1628)

Here are the codes:

Init (works fine)
Oid m_id(oid_string, true);
m_ipAddress = IpAddress(ipAddress);
m_cTarget = CTarget(m_ipAddress, community, community); // SNMP++ community target
m_cTarget.set_version(snmp_version(1));
m_vb.set_oid(oid_string);

m_snmp = new Snmp(m_status); //(m_ipAddress.get_ip_version() == Address::version_ipv4), 0
if (m_status != SNMP_CLASS_SUCCESS) { // check creation status
    std::cout << "snmp++ session not created" << m_snmp->error_msg(m_status); // if fail, print error string
    return;
}

Set integer value (works fine)
const int val = 1;
Oid testOid(".1.3.6.1.2.1.33.1.8.1.0");
int status = 0;
Vb test(testOid);
Snmp * snmpTest = new Snmp(status);
if (status != SNMP_CLASS_SUCCESS) { // check creation status
std::cout << “snmp++ session not created” << snmpTest->error_msg(status); // if fail, print error string
return;
}
Pdu pduTest;
test.set_value(val);
pduTest += test;
if ((status = snmpTest->set(pduTest, m_cTarget)) != SNMP_CLASS_SUCCESS){
qDebug() << “snmp set error” << snmpTest->error_msg(status);
} else {
qDebug() << “SNMP Set wirte value:” << val;
}

Set oid value (error)
int status = 0;
Snmp * snmpTest = new Snmp(status);
if (status != SNMP_CLASS_SUCCESS) { // check creation status
std::cout << “snmp++ session not created” << snmpTest->error_msg(status); // if fail, print error string
return;
}
Oid oid(".1.3.6.1.2.1.33.1.7.7.1");
Vb vb(".1.3.6.1.2.1.33.1.7.1.0");
vb.set_value(oid);
Pdu setPdu;
setPdu += vb;
if ((status = snmpTest->set(setPdu, m_cTarget)) != SNMP_CLASS_SUCCESS){
qDebug() << “snmp set error (UPS test)” << snmpTest->error_msg(status);
} else {
qDebug() << “Initiate ups test with” << oid.get_printable() << vb.get_printable_value();
}

→ SNMP: Cannot create/modify variable, Wrong Type

OID dotted number strings do not start with a dot, they start with a number (i.e. 1).

Maybe that is part of the problem.
If not, please check the type of the object instance of the agent with a GET request first.
It might be that it’s a bug in the agent that implements the instance as OCTET STRING instead of OBJECT IDENTIFIER.

That with the dottet oid makes no difference.

If i check the type of the object, its return OID. So that sould be correct.
The code itself should be correct as far as you can see?
Can I check the instance type of the value I set from the variable binding (vb), to get sure if its a oid?

Sure, you can check the type of the Vb value:
https://www.agentpp.com/doc/snmp++3.x/classVb.html#aea6b50ca09548e6b0edc0051c4f77ee3
by using the get_syntax() method before sending it, to avoid overload errors when setting values using the various overloaded operators.

I used the get_syntax() when reading from the oid.
It returned 6. Seems that this should be a OctetStr, correct?

When I try to set the value with a OctetStr type, I have the same response as before.
Code with OctetStr:
OctetStr octecStr = “1.3.6.1.2.1.33.1.7.7.1”;
Vb vb6(“1.3.6.1.2.1.33.1.7.1.0”);
vb6.set_value(octecStr);
Pdu setPdu6;
setPdu6 += vb6;
if ((status = snmpTest->set(setPdu6, m_cTarget)) != SNMP_CLASS_SUCCESS){
qDebug() << “snmp set error (UPS test)” << snmpTest->error_msg(status);
} else {
qDebug() << “Initiate ups test with” << oid.get_printable() << vb6.get_printable_value();
}
→ SNMP: Cannot create/modify variable, Wrong Type

Syntax 6 is Oid, OctetString would be 4. So you should set the value as Oid.

As the code looks right, I can only suggest two options

  • Try with SNMPv1 (snmp_version version1). Your code m_cTarget.set_version(snmp_version(1)); will use SNMPv2c
  • Use Wireshark to capture the requests of snmp++ and the other tool and compare the two request messages.

We deliberately use SNMP V2c, the UPS only offers v2c or v3. Therefore this will not work.
Any other suggestion?

Then there is only the option to capture the network packets and compare them.

Sorry for the late reply, the UPS was out of the office.

With the conversion of the variable to const char * it worked, finally.

QString str = “1.3.6.1.2.1.33.1.7.7.4”;
QByteArray ba = str.toLocal8Bit();
const char *c_str = ba.data();
Oid testOidChar(c_str);

vb.set_value(testOidChar);

1 Like