我想构建一个像SNMP设备(如交换机等)工作的应用程序,以使用snmp监控应用程序(如solarwinds,zabbix等)来监控一些项目。
我使用了SNMPsharpNet组件,成功接收到Get消息,但无法响应消息,请看这里:
UdpTarget target = new UdpTarget((IPAddress)new IpAddress(_peerIP.Address),162,5000,3);
nmpV2Packet pkt = new SnmpV2Packet();
try
{
pkt.decode(_inbuffer, inlen);
}
pkt.Pdu.VbList.RemoveAt(0);
pkt.Pdu.VbList.Add(new Oid("1.3.6.1.2.1.1.1.0"), new OctetString("Micromoje")); //sysDescr
pkt.Pdu.VbList.Add(new Oid("1.3.6.1.2.1.1.2.0"), new Oid("1.3.6.1.2.1.1.0")); //sysObjectID
pkt.Pdu.VbList.Add(new Oid("1.3.6.1.2.1.1.3.0"), new TimeTicks(2324)); //sysUpTime
pkt.Pdu.VbList.Add(new Oid("1.3.6.1.2.1.1.4.0"), new OctetString("DCU Nodes")); //sysContact
pkt.Pdu.VbList.Add(new Oid("1.3.6.1.2.1.1.5.0"), new OctetString("DCU Managing")); //sysName
AgentParameters aparam = new AgentParameters(SnmpVersion.Ver2, new OctetString("public"));
SnmpV2Packet response = new SnmpV2Packet("public");
response = target.Request(pkt.Pdu, aparam) as SnmpV2Packet;当我使用此代码时,我收到错误消息“请求已达到最大重试次数”。
然后我尝试了下面的代码:
pkt.Pdu.VbList.RemoveAt(0);
pkt.Pdu.VbList.Add(new Oid("1.3.6.1.2.1.1.1.0"), new OctetString("Micromoje")); //sysDescr
pkt.Pdu.VbList.Add(new Oid("1.3.6.1.2.1.1.2.0"), new Oid("1.3.6.1.2.1.1.0")); //sysObjectID
pkt.Pdu.VbList.Add(new Oid("1.3.6.1.2.1.1.3.0"), new TimeTicks(2324)); //sysUpTime
pkt.Pdu.VbList.Add(new Oid("1.3.6.1.2.1.1.4.0"), new OctetString("DCU Nodes")); //sysContact
pkt.Pdu.VbList.Add(new Oid("1.3.6.1.2.1.1.5.0"), new OctetString("DCU Managing")); //sysName
SnmpV2Packet response = new SnmpV2Packet("public"); //= target.Request(pkt.Pdu, aparam) as SnmpV2Packet;
response.Pdu.SetVbList(pkt.Pdu.VbList);
response.Pdu.Type = PduType.Set;
try
{
byte[] buf = response.encode();
_socket.SendTo(buf, (EndPoint)_peerIP);
}当我使用这个代码时,我在监控系统端收到错误消息‘not respond with the supplied read/write community string’。
最后我无法连接我的应用程序作为SNMP设备和测试连接失败,请帮助我,
发布于 2020-09-30 00:05:07
您需要使用相同的RequestId进行应答。此外,类型应为PduType.Response。我更新了你的代码:
int requestId = pkt.Pdu.RequestId;
pkt.Pdu.VbList.RemoveAt(0);
pkt.Pdu.VbList.Add(new Oid("1.3.6.1.2.1.1.1.0"), new OctetString("Micromoje")); //sysDescr
pkt.Pdu.VbList.Add(new Oid("1.3.6.1.2.1.1.2.0"), new Oid("1.3.6.1.2.1.1.0")); //sysObjectID
pkt.Pdu.VbList.Add(new Oid("1.3.6.1.2.1.1.3.0"), new TimeTicks(2324)); //sysUpTime
pkt.Pdu.VbList.Add(new Oid("1.3.6.1.2.1.1.4.0"), new OctetString("DCU Nodes")); //sysContact
pkt.Pdu.VbList.Add(new Oid("1.3.6.1.2.1.1.5.0"), new OctetString("DCU Managing")); //sysName
SnmpV2Packet response = new SnmpV2Packet("public"); //= target.Request(pkt.Pdu, param) as SnmpV2Packet;
response.Pdu.SetVbList(pkt.Pdu.VbList);
response.Pdu.Type = PduType.Response;
response.Pdu.RequestId = requestId;
try
{
byte[] buf = response.encode();
_socket.SendTo(buf, (EndPoint)_peerIP);
}https://stackoverflow.com/questions/37963262
复制相似问题