95 lines
2.6 KiB
C#
95 lines
2.6 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;
|
|
using ln.types;
|
|
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",
|
|
"1.3.6.1.2.1.4.20.1.3"
|
|
});
|
|
|
|
foreach (Sequence[] row in ipTable)
|
|
{
|
|
IPAddress ip = (row[0].Items[1] as IPAddr).IP;
|
|
int ifIndex = (int)(row[1].Items[1] as Integer).LongValue;
|
|
IPAddress mask = (row[2].Items[1] as IPAddr).IP;
|
|
|
|
if (interfaces.ContainsKey(ifIndex))
|
|
{
|
|
interfaces[ifIndex].AddIPAddress(new CIDR(ip,mask));
|
|
}
|
|
}
|
|
|
|
|
|
return interfaces.Values.ToArray();
|
|
}
|
|
|
|
|
|
public class Interface
|
|
{
|
|
public String Name { get; set; }
|
|
public CIDR[] IPAddresses => _IPAddresses.ToArray();
|
|
|
|
|
|
private List<CIDR> _IPAddresses = new List<CIDR>();
|
|
|
|
|
|
public Interface()
|
|
{
|
|
}
|
|
|
|
public void AddIPAddress(CIDR ip)
|
|
{
|
|
_IPAddresses.Add(ip);
|
|
}
|
|
|
|
public override string ToString()
|
|
{
|
|
return String.Format("[Interface Name={0} IPAddresses=({1})]",Name,String.Join(", ",IPAddresses.Select((x)=>x.ToString())));
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
}
|