OctetString community = new OctetString("public"); AgentParameters param = new AgentParameters(community); param.Version = SnmpVersion.Ver1; IpAddress agent = new IpAddress("192.168.0.50"); // Construct target UdpTarget target = new UdpTarget((IPAddress)agent, 161, 2000, 1); // Pdu class used for all requests Pdu pdu = new Pdu(PduType.Get); pdu.VbList.Add("1.3.6.1.2.1.1.1.0"); //sysDescr pdu.VbList.Add("1.3.6.1.2.1.1.2.0"); //sysObjectID pdu.VbList.Add("1.3.6.1.2.1.1.3.0"); //sysUpTime pdu.VbList.Add("1.3.6.1.2.1.1.4.0"); //sysContact pdu.VbList.Add("1.3.6.1.2.1.1.5.0"); //sysName // Make SNMP request SnmpV1Packet result = (SnmpV1Packet)target.Request(pdu, param); 如果使用target.RequestAsync()//这个方法怎么写? if (result != null) { // ErrorStatus other then 0 is an error returned by // the Agent - see SnmpConstants for error definitions if (result.Pdu.ErrorStatus != 0) { // agent reported an error with the request this.txtContent.Text += string.Format("Error in SNMP reply. Error {0} index {1} \r\n", result.Pdu.ErrorStatus, result.Pdu.ErrorIndex); } else { // Reply variables are returned in the same order as they were added // to the VbList this.txtContent.Text += string.Format("sysDescr({0}) ({1}): {2} \r\n", result.Pdu.VbList[0].Oid.ToString(), SnmpConstants.GetTypeName(result.Pdu.VbList[0].Value.Type), result.Pdu.VbList[0].Value.ToString()); this.txtContent.Text += string.Format("sysObjectID({0}) ({1}): {2} \r\n", result.Pdu.VbList[1].Oid.ToString(), SnmpConstants.GetTypeName(result.Pdu.VbList[1].Value.Type), result.Pdu.VbList[1].Value.ToString()); this.txtContent.Text += string.Format("sysUpTime({0}) ({1}): {2} \r\n", result.Pdu.VbList[2].Oid.ToString(), SnmpConstants.GetTypeName(result.Pdu.VbList[2].Value.Type), result.Pdu.VbList[2].Value.ToString()); this.txtContent.Text += string.Format("sysContact({0}) ({1}): {2} \r\n", result.Pdu.VbList[3].Oid.ToString(), SnmpConstants.GetTypeName(result.Pdu.VbList[3].Value.Type), result.Pdu.VbList[3].Value.ToString()); this.txtContent.Text += string.Format("sysName({0}) ({1}): {2} \r\n", result.Pdu.VbList[4].Oid.ToString(), SnmpConstants.GetTypeName(result.Pdu.VbList[4].Value.Type), result.Pdu.VbList[4].Value.ToString()); } } else { this.txtContent.Text += string.Format("No response received from SNMP agent. \r\n"); } target.Dispose(); }
SnmpV1Packet result = (SnmpV1Packet)target.Request(pdu, param); 如果使用target.RequestAsync()//这个方法怎么写?
OctetString community = new OctetString("public"); AgentParameters param = new AgentParameters(community);
param.Version = SnmpVersion.Ver1;
IpAddress agent = new IpAddress("192.168.0.50"); // Construct target
UdpTarget target = new UdpTarget((IPAddress)agent, 161, 2000, 1); // Pdu class used for all requests
Pdu pdu = new Pdu(PduType.Get);
pdu.VbList.Add("1.3.6.1.2.1.1.1.0"); //sysDescr
pdu.VbList.Add("1.3.6.1.2.1.1.2.0"); //sysObjectID
pdu.VbList.Add("1.3.6.1.2.1.1.3.0"); //sysUpTime
pdu.VbList.Add("1.3.6.1.2.1.1.4.0"); //sysContact
pdu.VbList.Add("1.3.6.1.2.1.1.5.0"); //sysName // Make SNMP request
SnmpV1Packet result = (SnmpV1Packet)target.Request(pdu, param);
如果使用target.RequestAsync()//这个方法怎么写? if (result != null)
{
// ErrorStatus other then 0 is an error returned by
// the Agent - see SnmpConstants for error definitions
if (result.Pdu.ErrorStatus != 0)
{
// agent reported an error with the request
this.txtContent.Text += string.Format("Error in SNMP reply. Error {0} index {1} \r\n",
result.Pdu.ErrorStatus,
result.Pdu.ErrorIndex);
}
else
{
// Reply variables are returned in the same order as they were added
// to the VbList
this.txtContent.Text += string.Format("sysDescr({0}) ({1}): {2} \r\n",
result.Pdu.VbList[0].Oid.ToString(), SnmpConstants.GetTypeName(result.Pdu.VbList[0].Value.Type),
result.Pdu.VbList[0].Value.ToString());
this.txtContent.Text += string.Format("sysObjectID({0}) ({1}): {2} \r\n",
result.Pdu.VbList[1].Oid.ToString(), SnmpConstants.GetTypeName(result.Pdu.VbList[1].Value.Type),
result.Pdu.VbList[1].Value.ToString());
this.txtContent.Text += string.Format("sysUpTime({0}) ({1}): {2} \r\n",
result.Pdu.VbList[2].Oid.ToString(), SnmpConstants.GetTypeName(result.Pdu.VbList[2].Value.Type),
result.Pdu.VbList[2].Value.ToString());
this.txtContent.Text += string.Format("sysContact({0}) ({1}): {2} \r\n",
result.Pdu.VbList[3].Oid.ToString(), SnmpConstants.GetTypeName(result.Pdu.VbList[3].Value.Type),
result.Pdu.VbList[3].Value.ToString());
this.txtContent.Text += string.Format("sysName({0}) ({1}): {2} \r\n",
result.Pdu.VbList[4].Oid.ToString(), SnmpConstants.GetTypeName(result.Pdu.VbList[4].Value.Type),
result.Pdu.VbList[4].Value.ToString());
}
}
else
{
this.txtContent.Text += string.Format("No response received from SNMP agent. \r\n");
}
target.Dispose();
}
SnmpV1Packet result = (SnmpV1Packet)target.Request(pdu, param);
如果使用target.RequestAsync()//这个方法怎么写?