ln.snmp/rfc1213/RFC1213.cs

92 lines
2.5 KiB
C#

// /**
// * File: RFC1213.cs
// * Author: haraldwolff
// *
// * This file and it's content is copyrighted by the Author and / or copyright holder.
// * Any use wihtout proper permission is illegal and may lead to legal actions.
// *
// *
// **/
using System;
using System.Net;
using System.Collections.Generic;
using ln.snmp.endpoint;
using ln.snmp.types;
using System.Linq;
namespace ln.snmp.rfc1213
{
public static class RFC1213
{
public static Interface[] GetInterfaces(SnmpEndpoint endpoint)
{
Dictionary<int,Interface> interfaces = new Dictionary<int,Interface>();
Sequence[][] ifTable = endpoint.snmpWalk(new String[] {
"1.3.6.1.2.1.2.2.1.2",
"1.3.6.1.2.1.2.2.1.10",
"1.3.6.1.2.1.2.2.1.16",
"1.3.6.1.2.1.2.2.1.14",
"1.3.6.1.2.1.2.2.1.20"
});
foreach (Sequence[] row in ifTable)
{
ObjectIdentifier index = (row[0].Items[0] as ObjectIdentifier);
Interface intf = new Interface();
intf.Name = (row[0].Items[1] as OctetString).StringValue;
interfaces.Add(index.OIDValue[0],intf);
}
Sequence[][] ipTable = endpoint.snmpWalk(new String[]
{
"1.3.6.1.2.1.4.20.1.1",
"1.3.6.1.2.1.4.20.1.2"
});
foreach (Sequence[] row in ipTable)
{
IPAddress ip = (row[0].Items[1] as IPAddr).IP;
int ifIndex = (int)(row[1].Items[1] as Integer).LongValue;
if (interfaces.ContainsKey(ifIndex))
{
interfaces[ifIndex].AddIPAddress(ip);
}
}
return interfaces.Values.ToArray();
}
public class Interface
{
public String Name { get; set; }
public System.Net.IPAddress[] IPAddresses => _IPAddresses.ToArray();
private List<System.Net.IPAddress> _IPAddresses = new List<System.Net.IPAddress>();
public Interface()
{
}
public void AddIPAddress(System.Net.IPAddress ip)
{
_IPAddresses.Add(ip);
}
public override string ToString()
{
return String.Format("[Interface Name={0} IPAddresses=({1})]",Name,String.Join(", ",IPAddresses.Select((x)=>x.ToString())));
}
}
}
}