ln.skyscanner/Program.cs

248 lines
8.3 KiB
C#

// /**
// * File: Program.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 Renci.SshNet;
using ln.skyscanner.identify;
using System.Net;
using System.Collections.Generic;
using ln.snmp.types;
using System.IO;
using ln.snmp;
using ln.snmp.endpoint;
using System.Diagnostics;
using ln.perfdb.storage;
using ln.logging;
using System.Security.Cryptography;
using ln.snmp.asn1;
using System.Linq;
using ln.types;
using System.Runtime.InteropServices;
using ln.snmp.rfc1213;
using ln.types.sync;
using ln.skyscanner.crawl;
using System.Threading;
using System.Net.NetworkInformation;
namespace ln.skyscanner
{
class MainClass
{
private static void DumpPerfValues(PerfValue[] perfValues)
{
int n = 0;
Logging.Log("----------------------------------------------");
Logging.Log("Dumping {0} perfValues:", perfValues.Length);
foreach (PerfValue perfValue in perfValues)
{
Logging.Log("PerfValue: [{1,6}] {0}", perfValue, n);
n++;
}
Logging.Log("");
}
public static void Main(string[] args)
{
Logger.ConsoleLogger.MaxLogLevel = LogLevel.DEBUG;
Crawler crawler = new Crawler();
Logging.Log(LogLevel.DEBUG, "Find host...");
crawler.Crawl(IPAddress.Parse("10.10.10.2"));
int n = 0;
do
{
Logging.Log(LogLevel.DEBUG, "QueuedJobs: {0} Pool: {1}", crawler.QueuedJobs, String.Join(", ", crawler.ThreadStates));
Thread.Sleep(1000);
if (crawler.QueuedJobs == 0)
n++;
else
n = 0;
} while (n < 10);
crawler.Stop();
crawler._CrawlPool.Save();
return;
//PerfFile perfFile = new PerfFile("test.lnpv");
//perfFile.Open();
//if (perfFile.FirstSection == null)
//{
// PerfFile.PerfFileSection section = new PerfFile.PerfFileSection(perfFile, null, 1440, 60, AggregationMethod.AVERAGE);
// section = new PerfFile.PerfFileSection(perfFile, section,1728,300, AggregationMethod.AVERAGE);
// section = new PerfFile.PerfFileSection(perfFile, section, 2016, 900, AggregationMethod.AVERAGE);
// section = new PerfFile.PerfFileSection(perfFile, section, 1344, 3600, AggregationMethod.AVERAGE);
// section = new PerfFile.PerfFileSection(perfFile, section, 1344, 10800, AggregationMethod.AVERAGE);
//}
//int n = 10;
//for (long ts = 1000; ts < 160000; ts += 60)
//{
// perfFile.Write(ts, n++);
//}
//PerfValue[] dump = new PerfValue[perfFile.FirstSection.nRecords];
//perfFile.FirstSection.ReadRecords(dump, 0, 0, dump.Length);
//DumpPerfValues(dump);
//DumpPerfValues(perfFile.QueryTime(3600, 0));
//DumpPerfValues(perfFile.QueryTime(3600 * 24, 0));
//DumpPerfValues(perfFile.QueryTime(3600 * 24, 3600));
//perfFile.Close();
//return;
SNMPEngine.DEBUG = true;
SNMPEngine engine = new SNMPEngine();
engine.Timeout = 2000;
SnmpV1Endpoint v1endpoint = new SnmpV1Endpoint(engine, new IPEndPoint(IPAddress.Parse("10.75.1.10"), 161), "ByFR4oW98hap");
SnmpV2Endpoint v2endpoint = new SnmpV2Endpoint(engine, new IPEndPoint(IPAddress.Parse("10.113.254.4"), 161), "ghE7wUmFPoPpkRno");
USMEndpoint v3endpoint = new USMEndpoint(engine, new IPEndPoint(IPAddress.Parse("10.10.10.1"), 161));
v3endpoint.AuthMethod = SnmpV3AuthMethod.SHA;
v3endpoint.AuthKeyPhrase = "qVy3hnZJ2fov";
//v3endpoint.AuthKeyPhrase = "maplesyrup";
v3endpoint.Username = "skytron";
SnmpEndpoint intf = v3endpoint;
RFC1213.Interface[] interfaces = RFC1213.GetInterfaces(v3endpoint);
foreach (RFC1213.Interface netIf in interfaces)
{
Logging.Log(LogLevel.INFO, "Interface: {0}",netIf);
}
//Stopwatch stopWatch = Stopwatch.StartNew();
//Sequence[][] ifTable = intf.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"
// });
//stopWatch.Stop();
//Console.WriteLine("Time for table walk: {0}ms", stopWatch.ElapsedMilliseconds);
//foreach (Sequence[] sequences in ifTable)
//{
// Console.Write("{0,24} ", sequences);
// foreach (Sequence sequence in sequences)
// {
// Console.Write("{0}\t", sequence?.Items?[1].Value);
// }
// Console.WriteLine("");
//}
engine.Close();
//NodeIdentifier identifier = new NodeIdentifier(IPAddress.Parse("10.10.10.1"));
//identifier.Identify();
//foreach (KeyValuePair<string,string> hint in identifier.Hints)
//{
// Console.WriteLine("{0:16} = {1}", hint.Key, hint.Value);
//}
}
public static void TestAuthKey(String filename,USMEndpoint v3endpoint)
{
FileStream fileStream = new FileStream(filename, FileMode.Open);
byte[] source = new byte[fileStream.Length];
int nread = fileStream.Read(source, 0, source.Length);
fileStream.Close();
fileStream.Dispose();
ASN1Value asn = new ASN1Value(source);
USMMessage usm = new USMMessage(asn);
byte[] repro1 = asn.AsByteArray;
byte[] repro2 = ((ASN1Value)usm).AsByteArray;
Logging.Log(LogLevel.DEBUG, "Source: {0}", BitConverter.ToString(source));
Logging.Log(LogLevel.DEBUG, "Repro1: {0}", BitConverter.ToString(repro1));
Logging.Log(LogLevel.DEBUG, "Repro2: {0}", BitConverter.ToString(repro2));
if (!source.SequenceEqual(repro1))
Logging.Log(LogLevel.ERROR, "Repro1 does not match!");
else
Logging.Log(LogLevel.ERROR, "Repro1 matches!");
if (!source.SequenceEqual(repro2))
Logging.Log(LogLevel.ERROR, "Repro2 does not match!");
else
Logging.Log(LogLevel.ERROR, "Repro2 matches!");
if (!repro1.SequenceEqual(repro2))
Logging.Log(LogLevel.ERROR, "Repro1 != Repro2!");
else
Logging.Log(LogLevel.ERROR, "Repro1/2 match!");
usm.Dump();
usm.SecurityParameters.Dump();
byte[] auth1 = usm.SecurityParameters.msgAuthenticationParameters.Bytes;
usm.SecurityParameters.msgAuthenticationParameters.Bytes = new byte[12];
Logging.Log(LogLevel.DEBUG, "Source: {0}", BitConverter.ToString(source));
v3endpoint.RemoteEngineID = usm.SecurityParameters.msgAuthoritativeEngineID;
v3endpoint.CacheAuthoritativeEngineTime = usm.SecurityParameters.msgAuthoritativeEngineTime;
v3endpoint.CacheAuthoritativeEngineBoots = usm.SecurityParameters.msgAuthoritativeEngineBoots;
v3endpoint.LocalizeKeys();
v3endpoint.AuthenticateMessage(usm);
byte[] auth2 = usm.SecurityParameters.msgAuthenticationParameters.Bytes;
Logging.Log(LogLevel.DEBUG, "Authenticated: {0}", BitConverter.ToString(((ASN1Value)usm).AsByteArray));
Logging.Log(LogLevel.DEBUG, "Original Auth Token: {0}", BitConverter.ToString(auth1));
Logging.Log(LogLevel.DEBUG, "Calculated Auth Token: {0}", BitConverter.ToString(auth2));
if (auth1.SequenceEqual(auth2))
{
Logging.Log(LogLevel.DEBUG, "MATCH");
}
else
{
Logging.Log(LogLevel.DEBUG, "NO MATCH");
}
}
}
}