I am attempting to connect to a device that is using a non-standard port number with SNMP (501) using the SNMP++ library. When I create the Snmp object using any port other than 161, I get timeout errors. However, I am still able to use the netsnmp snmp_get using port 501 successfully.
Am I missing something with setting the port number? Does SNMP++ only support port 161?
snmp++ does not have any special restrictions on the ports. You could use the snmpNext example to access the device. If SNMPv1 with public community is configured, the command “./snmpNext 220.127.116.11 -P501” should work.
Some more details:
When constructing a Snmp object, you should pass “0” as the port number, so a (random) free client port will be used. This is OK for your use case, as this port is used as the source port for the communication to the device.
To access a device that listens on port 501 you have to create a SnmpTarget (either CTarget or UTarget) object with a UdpAddress that has been set to the port 501.
You should easily find the relevant code in the snmpNext.cpp example.
Hello again. I have a follow up question now that I’m getting deeper into using this library. I am trying to send a set command, but am struggling to get it correct.
Snmp snmp = new Snmp(status, 501); // I have also tried 0 for the port, status indicates success
vb.set_value(6); // Also tried using Snmp_pp::SnmpInt32 and casting to int explicitly here
pdu += vb;
int retval = snmp->set(pdu, target_address);
retval indicates SNMP_CLASS_SUCCESS but when I get that same oid the value has not changed. I am able to successfully set this oid when using net-snmp command
Thanks for pointing me to the examples, I had missed those! I’m also curious, earlier you said that I should not have a leading . in the oid string. I do have those on some of my gets and they seem to be working fine. Why should the sets not have that? (Or should I really not have them at all?)
you’re right: the Oid class ignores the leading dot when parsing the oid string (get or set request does not matter). But if you want to be on the safe side, do not use them. I’m pretty sure that a leading dot has caused problems at some time.