143 lines
4.6 KiB
C#
143 lines
4.6 KiB
C#
using System;
|
|
using ln.snmp.types;
|
|
using System.Net;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using ln.snmp.channel;
|
|
namespace ln.snmp
|
|
{
|
|
public enum SnmpVersion : int { V1 = 0, V2c = 1, V3 = 3 }
|
|
|
|
public class SNMPInterface
|
|
{
|
|
public SnmpPDUChannel PDUChannel { get; set; }
|
|
|
|
public IPEndPoint RemoteEndpoint { get; set; }
|
|
|
|
public SnmpVersion SnmpVersion => PDUChannel.SnmpVersion;
|
|
|
|
public SNMPInterface(SnmpPDUChannel PDUChannel,IPEndPoint remoteEndpoint)
|
|
{
|
|
this.PDUChannel = PDUChannel;
|
|
RemoteEndpoint = remoteEndpoint;
|
|
}
|
|
|
|
|
|
private List<Sequence> snmpRequest<T>(IEnumerable<ObjectIdentifier> objectIdentifiers) where T: PDU, new()
|
|
{
|
|
T pdu = new T();
|
|
foreach (ObjectIdentifier oid in objectIdentifiers)
|
|
{
|
|
pdu.Add(new Sequence(new Variable[] { oid, NullValue.Instance }));
|
|
}
|
|
|
|
GetResponse responsePDU = PDUChannel.RequestResponse(pdu, RemoteEndpoint) as GetResponse;
|
|
Sequence varBinds = responsePDU.VarBinds as Sequence;
|
|
List<Sequence> results = new List<Sequence>();
|
|
|
|
foreach (Variable varBind in varBinds.Items)
|
|
{
|
|
results.Add(varBind as Sequence);
|
|
}
|
|
|
|
return results;
|
|
}
|
|
|
|
public List<Sequence> snmpGet(IEnumerable<ObjectIdentifier> objectIdentifiers)
|
|
{
|
|
return snmpRequest<GetRequest>(objectIdentifiers);
|
|
}
|
|
public Variable snmpGet(ObjectIdentifier objectIdentifier)
|
|
{
|
|
return snmpGet(new ObjectIdentifier[] { objectIdentifier })[0].Items[1];
|
|
}
|
|
public Variable snmpGet(string objectIdentifier)
|
|
{
|
|
return snmpGet(new ObjectIdentifier[] { new ObjectIdentifier(objectIdentifier) })[0].Items[1];
|
|
}
|
|
public List<Sequence> snmpGet(IEnumerable<String> objectIdentifiers)
|
|
{
|
|
return snmpGet(objectIdentifiers.Select((x) => new ObjectIdentifier(x)));
|
|
}
|
|
|
|
public List<Sequence> snmpGetNext(IEnumerable<ObjectIdentifier> objectIdentifiers)
|
|
{
|
|
return snmpRequest<GetNextRequest>(objectIdentifiers);
|
|
}
|
|
public Sequence snmpGetNext(ObjectIdentifier objectIdentifier)
|
|
{
|
|
return snmpGetNext(new ObjectIdentifier[] { objectIdentifier })[0];
|
|
}
|
|
public Sequence snmpGetNext(string objectIdentifier)
|
|
{
|
|
return snmpGetNext(new ObjectIdentifier[] { new ObjectIdentifier(objectIdentifier) })[0];
|
|
}
|
|
public List<Sequence> snmpGetNext(IEnumerable<String> objectIdentifiers)
|
|
{
|
|
return snmpGetNext(objectIdentifiers.Select((x) => new ObjectIdentifier(x)));
|
|
}
|
|
|
|
|
|
public List<Sequence> snmpGetBulk(ObjectIdentifier objectIdentifier)
|
|
{
|
|
List<Sequence> results = new List<Sequence>();
|
|
List<Sequence> parts;
|
|
|
|
do
|
|
{
|
|
parts = snmpRequest<GetBulkRequest>(new ObjectIdentifier[] { objectIdentifier });
|
|
|
|
foreach (Sequence ps in parts)
|
|
{
|
|
ObjectIdentifier oid = ps.Items[0] as ObjectIdentifier;
|
|
if (objectIdentifier.Contains(oid))
|
|
results.Add(ps);
|
|
else
|
|
return results;
|
|
}
|
|
|
|
|
|
} while (parts.Count >= 32);
|
|
|
|
return results;
|
|
}
|
|
public List<Sequence> snmpGetBulk(String objectIdentifier)
|
|
{
|
|
return snmpGetBulk(new ObjectIdentifier(objectIdentifier));
|
|
}
|
|
|
|
|
|
|
|
public List<Sequence> snmpWalk(ObjectIdentifier objectIdentifier)
|
|
{
|
|
if (SnmpVersion == SnmpVersion.V1)
|
|
{
|
|
List<Sequence> results = new List<Sequence>();
|
|
ObjectIdentifier oiLast = objectIdentifier;
|
|
|
|
while (objectIdentifier.Contains(oiLast))
|
|
{
|
|
Sequence next = snmpGetNext(oiLast);
|
|
oiLast = next.Items[0] as ObjectIdentifier;
|
|
if (objectIdentifier.Contains(oiLast))
|
|
results.Add(next);
|
|
}
|
|
return results;
|
|
} else if (SnmpVersion == SnmpVersion.V2c)
|
|
{
|
|
return snmpGetBulk(objectIdentifier);
|
|
}
|
|
else
|
|
{
|
|
throw new NotImplementedException();
|
|
}
|
|
}
|
|
public List<Sequence> snmpWalk(string objectIdentifier)
|
|
{
|
|
return snmpWalk(new ObjectIdentifier(objectIdentifier));
|
|
}
|
|
|
|
|
|
}
|
|
}
|