我正在尝试在Kotlin和snmp4j上制作snmp代理。 我编写了以下代码:
class MockAgent(host: String,port: Int) : CommandResponder {
var mib: MutableMap<OID,AbstractVariable> = mutableMapOf(
OID("1.3.6.1.4.1.99999.1.1.1.3.1.1") to UnsignedInteger32(30)
)
...
override fun <A : Address> processpdu(e: CommandResponderEvent<A>): Unit {
val command: PDU = e.getPDU();
if (command.getType() == PDU.GET) {
val valueBindings: ArrayList<VariableBinding> = ArrayList()
val commandVarBindings = command.getVariableBindings()
for (cmdVarBind in commandVarBindings) {
val oid: OID = cmdVarBind.getOid()
if (this.mib.containsKey(oid)) {
valueBindings.add(VariableBinding(oid,this.mib.get(oid)))
}
}
command.setVariableBindings(valueBindings)
command.setType(PDU.RESPONSE)
command.setErrorIndex(0)
command.setErrorStatus(0)
println(command)
val statusInformation = StatusInformation()
val ref: StateReference<A> = e.getStateReference()
e.setProcessed(true)
try {
println("Try sending response...")
e.getMessageDispatcher().returnResponsePdu(e.getMessageProcessingModel(),e.getSecurityModel(),e.getSecurityName(),e.getSecurityLevel(),command,e.getMaxSizeResponsePDU(),ref,statusInformation
)
} catch (e: Exception) {
println("Error while sending response" + e.getLocalizedMessage())
}
}
}
当我尝试发出snmpget请求时,出现以下错误:
尝试发送响应...发送响应时出错首先无效 子标识符(必须为0、1或2)RESPONSE [requestID = 1066792362, errorStatus =成功(0),errorIndex = 0,VBS [1.3.6.1.4.1.99999.1.1.1.3 = 4.6]]
快照请求:
snmpget -v 2c -c public localhost:1610 1.3.6.1.4.1.99999.1.1.1.3
在谷歌搜索之后,我发现可以禁用OID格式的信息。但是我找不到办法。如果有人可以给我一些解决方法的提示,我将非常高兴。谢谢!