ln.skyscanner/crawl/service/SNMP.cs

270 lines
9.9 KiB
C#

//// /**
//// * File: SNMP.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 ln.snmp;
//using ln.snmp.endpoint;
//using ln.types;
//using ln.snmp.types;
//using System.Runtime.Remoting.Messaging;
//using System.Linq;
//using ln.types.net;
//namespace ln.skyscanner.crawl.service
//{
// public class SNMP : CrawlService
// {
// public string[] defaultCommunities = new string[0];
// public SNMP(string[] defaultSecrets)
// : base("snmp")
// {
// defaultCommunities = defaultSecrets;
// }
// public override bool Check(Crawl crawl)
// {
// if (!crawl.CheckRequiredOption("ping"))
// return false;
// if (!TestCurrentHints(crawl.Host))
// {
// TestDefaults(crawl.Host);
// }
// return true;
// }
// public override bool HostProvidesOption(Crawl crawl, params object[] parameters)
// {
// return (crawl.Host.GetHint("snmp.version",null) != null) && ((crawl.Host.GetHint("snmp.username",null) != null) || (crawl.Host.GetHint("snmp.community",null) != null));
// }
// public bool TestCurrentHints(CrawledHost crawledHost)
// {
// SnmpInterface snmpEndpoint = GetSnmpInterface(crawledHost);
// if (snmpEndpoint != null)
// {
// using (snmpEndpoint)
// {
// try
// {
// Variable prID = snmpEndpoint.snmpGet("1.3.6.1.2.1.1.2.0");
// } catch (SnmpError)
// {
// return false;
// } catch (TimeoutException)
// {
// return false;
// }
// return true;
// }
// }
// return false;
// }
// private bool TestSnmpV3(CrawledHost crawledHost)
// {
// IPv4[] ips = crawledHost.IPAddresses.Concat(new IPv4[] { crawledHost.PrimaryIP }).Distinct().ToArray();
// foreach (IPv4 ip in ips)
// {
// using (USMEndpoint v3endpoint = new USMEndpoint(SNMPEngine.DefaultEngine, new IPEndPoint(ip, 161)))
// {
// try
// {
// v3endpoint.QueryEngineID();
// }
// catch (TimeoutException)
// {
// }
// if (v3endpoint.RemoteEngineID != null)
// {
// crawledHost.SetHint("snmp.version", 3);
// int c = 0;
// foreach (string community in defaultCommunities)
// {
// c++;
// v3endpoint.Username = "skytron";
// v3endpoint.AuthMethod = SnmpV3AuthMethod.SHA;
// v3endpoint.AuthKeyPhrase = community;
// v3endpoint.LocalizeKeys();
// try
// {
// Variable prID = v3endpoint.snmpGet("1.3.6.1.2.1.1.2.0");
// crawledHost.SetHint("snmp.username", "skytron");
// crawledHost.SetHint("snmp.authkey", community);
// crawledHost.SetHint("snmp.address", ip);
// return true;
// }
// catch (TimeoutException)
// {
// }
// catch (SnmpError)
// {
// }
// }
// }
// v3endpoint.Close();
// }
// }
// return false;
// }
// private bool TestSnmpV2(CrawledHost crawledHost)
// {
// IPv4[] ips = crawledHost.IPAddresses.Concat(new IPv4[] { crawledHost.PrimaryIP }).Distinct().ToArray();
// foreach (IPv4 ip in ips)
// {
// using (SnmpV2Endpoint v2endpoint = new SnmpV2Endpoint(SNMPEngine.DefaultEngine, new IPEndPoint(crawledHost.PrimaryIP, 161)))
// {
// foreach (String community in defaultCommunities)
// {
// v2endpoint.CommunityString = community;
// try
// {
// Variable prID = v2endpoint.snmpGet("1.3.6.1.2.1.1.2.0");
// crawledHost.SetHint("snmp.version", 2);
// crawledHost.SetHint("snmp.community", community);
// crawledHost.SetHint("snmp.address", ip);
// return true;
// }
// catch (SnmpError)
// {
// }
// catch (TimeoutException)
// {
// }
// }
// v2endpoint.Close();
// }
// }
// return false;
// }
// private bool TestSnmpV1(CrawledHost crawledHost)
// {
// IPv4[] ips = crawledHost.IPAddresses.Concat(new IPv4[] { crawledHost.PrimaryIP }).Distinct().ToArray();
// foreach (IPv4 ip in ips)
// {
// using (SnmpV1Endpoint v1endpoint = new SnmpV1Endpoint(SNMPEngine.DefaultEngine, new IPEndPoint(crawledHost.PrimaryIP, 161)))
// {
// foreach (String community in defaultCommunities)
// {
// v1endpoint.CommunityString = community;
// try
// {
// Variable prID = v1endpoint.snmpGet("1.3.6.1.2.1.1.2.0");
// crawledHost.SetHint("snmp.version", 1);
// crawledHost.SetHint("snmp.community", community);
// crawledHost.SetHint("snmp.address", ip);
// return true;
// }
// catch (SnmpError)
// {
// }
// catch (TimeoutException)
// {
// }
// }
// v1endpoint.Close();
// }
// }
// return false;
// }
// public bool TestDefaults(CrawledHost crawledHost)
// {
// if (TestSnmpV3(crawledHost) ? true : TestSnmpV2(crawledHost) ? true : TestSnmpV1(crawledHost))
// {
// using (SnmpInterface snmp = GetSnmpInterface(crawledHost))
// {
// Variable prID = snmp.snmpGet("1.3.6.1.2.1.1.2.0");
// crawledHost.SetHint("snmp.sysObjectID", (prID as ObjectIdentifier).AsString);
// try
// {
// Sequence[] seqORids = snmp.snmpWalk("1.3.6.1.2.1.1.9.1.2").ToArray();
// string[] ORids = new string[seqORids.Length];
// for (int n = 0; n < ORids.Length; n++)
// {
// ORids[n] = (seqORids[n].Items[1] as ObjectIdentifier).AsString;
// }
// crawledHost.SetHint("snmp.orids", ORids);
// }
// catch (TimeoutException)
// { }
// }
// return true;
// }
// else
// {
// crawledHost.SetHint("snmp.version", null);
// crawledHost.SetHint("snmp.username", null);
// crawledHost.SetHint("snmp.authkey", null);
// crawledHost.SetHint("snmp.community", null);
// crawledHost.SetHint("snmp.sysObjectID", null);
// return false;
// }
// }
// public static SnmpInterface GetSnmpInterface(CrawledHost crawledHost)
// {
// int snmpVersion = crawledHost.GetHint<int>("snmp.version", -1);
// IPv4 snmpAddress = crawledHost.GetHint<IPv4>("snmp.address", crawledHost.PrimaryIP);
// switch (snmpVersion)
// {
// case 1:
// SnmpV1Endpoint v1 = new SnmpV1Endpoint(SNMPEngine.DefaultEngine, new IPEndPoint(snmpAddress, 161));
// v1.CommunityString = crawledHost.GetHint<string>("snmp.community");
// return v1;
// case 2:
// SnmpV2Endpoint v2 = new SnmpV2Endpoint(SNMPEngine.DefaultEngine, new IPEndPoint(snmpAddress, 161));
// v2.CommunityString = crawledHost.GetHint<string>("snmp.community");
// return v2;
// case 3:
// if (crawledHost.GetHint<string>("snmp.username", null) != null)
// {
// USMEndpoint endpoint = new USMEndpoint(SNMPEngine.DefaultEngine, new System.Net.IPEndPoint(snmpAddress, 161));
// endpoint.AuthMethod = SnmpV3AuthMethod.SHA;
// endpoint.Username = crawledHost.GetHint<string>("snmp.username");
// endpoint.AuthKeyPhrase = crawledHost.GetHint<string>("snmp.authkey");
// return endpoint;
// }
// return null;
// default:
// return null;
// }
// }
// }
//}