public class SNMP_Walk
{
/* 建構式 */
public SNMP_Walk(NmsDevice dev, string oid)
{
mDevice = dev;
OID = oid;
}
/* 屬性 */
private NmsDevice mDevice = new NmsDevice();
private string OID { get; set; }
/* 方法 */
public Task<NmsDevice> OidPullingAsync()
{
/* 一般程式寫法 */
//Task<NmsDevice> task = new Task<NmsDevice>(SnmpWalk);
//task.Start();
/* 黏巴達 語法 */
//Task<NmsDevice> task = Task<NmsDevice>.Factory.StartNew(() => { return SnmpWalk(); });
//return task;
/* 匿名方式 黏巴達 語法*/
return Task<NmsDevice>.Factory.StartNew(() => { return SnmpWalk(); });
}
private NmsDevice SnmpWalk()
{
string IP = mDevice.IP;
List<string> list = new List<string>();
list = SnmpWalkList(IP, OID);
/* 串接回傳值 */
mDevice.PullingOidResult = "";
foreach (string ss in list)
{
mDevice.PullingOidResult += string.Format("{0}{1}", ss, Environment.NewLine);
}
return mDevice;
}
private List<string> SnmpWalkList(string IP, string oid)
{
List<string> list = new List<string>();
try
{
// SNMP community name
OctetString community = new OctetString("public");
// Define agent parameters class
AgentParameters param = new AgentParameters(community);
// Set SNMP version to 1
param.Version = SnmpVersion.Ver1;
// Construct the agent address object
// IpAddress class is easy to use here because
// it will try to resolve constructor parameter if it doesn't
// parse to an IP address
IpAddress agent = new IpAddress(IP);
// Construct target
UdpTarget target = new UdpTarget((IPAddress)agent, 161, 2000, 1);
// Define Oid that is the root of the MIB
// tree you wish to retrieve
Oid rootOid = new Oid(oid.Replace("\"", "")); // ifDescr
// This Oid represents last Oid returned by
// the SNMP agent
Oid lastOid = (Oid)rootOid.Clone();
// Pdu class used for all requests
Pdu pdu = new Pdu(PduType.GetNext);
#region Loop through results
while (lastOid != null)
{
// When Pdu class is first constructed, RequestId is set to a random value
// that needs to be incremented on subsequent requests made using the
// same instance of the Pdu class.
if (pdu.RequestId != 0)
{
pdu.RequestId += 1;
}
// Clear Oids from the Pdu class.
pdu.VbList.Clear();
// Initialize request PDU with the last retrieved Oid
pdu.VbList.Add(lastOid);
// Make SNMP request
SnmpV1Packet result = (SnmpV1Packet)target.Request(pdu, param);
// You should catch exceptions in the Request if using in real application.
// If result is null then agent didn't reply or we couldn't parse the reply.
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
Console.WriteLine(string.Format("Error in SNMP reply. Error {0} index {1}",
result.Pdu.ErrorStatus,
result.Pdu.ErrorIndex));
lastOid = null;
break;
}
else
{
// Walk through returned variable bindings
foreach (Vb v in result.Pdu.VbList)
{
// Check that retrieved Oid is "child" of the root OID
if (rootOid.IsRootOf(v.Oid))
{
//listBox1.Items.Add(string.Format("{0} ({1}): {2}",
// v.Oid.ToString(),
// SnmpConstants.GetTypeName(v.Value.Type),
// v.Value.ToString()) );
list.Add(string.Format("{0}{1}", v.Value.ToString(), Environment.NewLine));
lastOid = v.Oid;
}
else
{
// we have reached the end of the requested
// MIB tree. Set lastOid to null and exit loop
lastOid = null;
}
}
}
}
else
{
list.Add(string.Format("No response received from SNMP agent.{0}", Environment.NewLine));
}
}
#endregion
target.Close();
return list;
}
catch (Exception ex)
{
list.Add(string.Format("No response received from SNMP agent.{0}", Environment.NewLine));
return list;
}
}
}