如何使用 snmp4j 为 Mib 设置值
how to set value to Mib using snmp4j
我正在尝试创建 snmp 管理器和代理的简单示例。
我需要做的就是从给定的 OID 中读取当前值,写入一些新值来替换它,然后读回它。
我的 get 函数运行良好,但我的 set 运行不正常。
在行中:PDU responsePDU = event.getResponse();
responsePDU 等于 null,我不明白为什么?
非常欢迎任何帮助!
public ResponseEvent set(OID oid,String val) throws IOException
{
PDU pdu = new PDU();
VariableBinding varBind = new VariableBinding(oid,new OctetString(val));
pdu.add(varBind);
pdu.setType(PDU.SET);
pdu.setRequestID(new Integer32(1));
Target target=getTargetForWrite();
ResponseEvent event = snmp.set(pdu, target);
if(event != null)
{
System.out.println("\nResponse:\nGot Snmp Set Response from Agent");
System.out.println("Snmp Set Request = " + event.getRequest().getVariableBindings());
PDU responsePDU = event.getResponse();
System.out.println("\nresponsePDU = "+responsePDU);
if (responsePDU != null)
{
int errorStatus = responsePDU.getErrorStatus();
int errorIndex = responsePDU.getErrorIndex();
String errorStatusText = responsePDU.getErrorStatusText();
System.out.println("\nresponsePDU = "+responsePDU);
if (errorStatus == PDU.noError)
{
System.out.println("Snmp Set Response = " + responsePDU.getVariableBindings());
}
else
{
System.out.println("errorStatus = "+responsePDU);
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);
}
}
return event;
}
throw new RuntimeException("GET timed out");
}
private Target getTargetForWrite()
{
CommunityTarget target = new CommunityTarget();
target.setCommunity(new OctetString("private"));
target.setVersion(SnmpConstants.version2c);
target.setAddress(GenericAddress.parse(address));
target.setRetries(2);
target.setTimeout(1500);
return target;
}
需要将社区设置为public
private Target getTargetForWrite()
{
CommunityTarget target = new CommunityTarget();
target.setCommunity(new OctetString("public"));\<---
target.setVersion(SnmpConstants.version2c);
target.setAddress(GenericAddress.parse(address));
target.setRetries(2);
target.setTimeout(1500);
return target;
}
我正在尝试创建 snmp 管理器和代理的简单示例。 我需要做的就是从给定的 OID 中读取当前值,写入一些新值来替换它,然后读回它。
我的 get 函数运行良好,但我的 set 运行不正常。
在行中:PDU responsePDU = event.getResponse();
responsePDU 等于 null,我不明白为什么?
非常欢迎任何帮助!
public ResponseEvent set(OID oid,String val) throws IOException
{
PDU pdu = new PDU();
VariableBinding varBind = new VariableBinding(oid,new OctetString(val));
pdu.add(varBind);
pdu.setType(PDU.SET);
pdu.setRequestID(new Integer32(1));
Target target=getTargetForWrite();
ResponseEvent event = snmp.set(pdu, target);
if(event != null)
{
System.out.println("\nResponse:\nGot Snmp Set Response from Agent");
System.out.println("Snmp Set Request = " + event.getRequest().getVariableBindings());
PDU responsePDU = event.getResponse();
System.out.println("\nresponsePDU = "+responsePDU);
if (responsePDU != null)
{
int errorStatus = responsePDU.getErrorStatus();
int errorIndex = responsePDU.getErrorIndex();
String errorStatusText = responsePDU.getErrorStatusText();
System.out.println("\nresponsePDU = "+responsePDU);
if (errorStatus == PDU.noError)
{
System.out.println("Snmp Set Response = " + responsePDU.getVariableBindings());
}
else
{
System.out.println("errorStatus = "+responsePDU);
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);
}
}
return event;
}
throw new RuntimeException("GET timed out");
}
private Target getTargetForWrite()
{
CommunityTarget target = new CommunityTarget();
target.setCommunity(new OctetString("private"));
target.setVersion(SnmpConstants.version2c);
target.setAddress(GenericAddress.parse(address));
target.setRetries(2);
target.setTimeout(1500);
return target;
}
需要将社区设置为public
private Target getTargetForWrite()
{
CommunityTarget target = new CommunityTarget();
target.setCommunity(new OctetString("public"));\<---
target.setVersion(SnmpConstants.version2c);
target.setAddress(GenericAddress.parse(address));
target.setRetries(2);
target.setTimeout(1500);
return target;
}