我正在使用snmp4J设置的DOS snmp命令:
snmpset -c private -v 2c 192.168.1.26 .1.3.6.1.4.1.991.100.5.19.3.0 u 1
这是代码:
public class SnmpSetExample {
private static String ipAddress = "192.168.1.26";
private static String port = "161";
private static String sysContactOid = ".1.3.6.1.4.1.991.100.5.19.3.0";
private static int sysContactValue = 1;
private static int snmpVersion = SnmpConstants.version2c;
private static String community = "private";
public static void main(String[] args) throws Exception {
System.out.println("SNMP SET Demo");
TransportMapping transport = new DefaultUdpTransportMapping();
transport.listen();
CommunityTarget comtarget = new CommunityTarget();
comtarget.setCommunity(new OctetString(community));
comtarget.setVersion(snmpVersion);
comtarget.setAddress(new UdpAddress(ipAddress + "/" + port));
comtarget.setRetries(2);
comtarget.setTimeout(1000);
PDU pdu = new PDU();
// Setting the Oid and Value for sysContact variable
OID oid = new OID(sysContactOid);
Variable var = new Integer32(sysContactValue);
VariableBinding varBind = new VariableBinding(oid, var);
pdu.add(varBind);
pdu.setType(PDU.SET);
pdu.setRequestID(new Integer32(1));
// Create Snmp object for sending data to Agent
Snmp snmp = new Snmp(transport);
ResponseEvent response = snmp.set(pdu, comtarget);
if (response != null) {
System.out.println("\nResponse:\nGot Snmp Set Response from Agent");
PDU responsePDU = response.getResponse();
if (responsePDU != null) {
int errorStatus = responsePDU.getErrorStatus();
int errorIndex = responsePDU.getErrorIndex();
String errorStatusText = responsePDU.getErrorStatusText();
if (errorStatus == PDU.noError) {
System.out.println("Snmp Set Response = " + responsePDU.getVariableBindings());
} else {
System.out.println("Error: Request Failed");
System.out.println("Error Status = " + errorStatus);
System.out.println("Error Index = " + errorIndex);
System.out.println("Error Status Text = " + errorStatusText);
}
} else {
System.out.println("Error: Response PDU is null");
}
} else {
System.out.println("Error: Agent Timeout... ");
}
snmp.close();
}
}
但我的错误结果是:
...
Response:
Got Snmp Set Response from Agent
Error: Response PDU is null
PDU已准备好设置为.SET。有人知道参数或代码中缺少什么?提前谢谢。
答案 0 :(得分:0)
in:变量var = new Integer32(sysContactValue);
更改Integer32类型的Gauge32或UnsignedInterger32。有必要始终检查要发送的值的数据类型。