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.
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?
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