ln.skyscanner/identify/NodeIdentifier.cs

119 lines
3.5 KiB
C#

// /**
// * File: NodeIdentifier.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.Collections.Generic;
using System.Net;
using Renci.SshNet;
using System.Net.Sockets;
using System.Text;
using System.IO.Pipes;
using System.IO;
using System.Threading;
namespace ln.skyscanner.identify
{
public class NodeIdentifier
{
static KeyValuePair<string, string>[] sshAuthorizations = new KeyValuePair<string, string>[]
{
new KeyValuePair<string, string>("skytron","67E3xpTcMbwR"),
new KeyValuePair<string, string>("skytron","v1kXbeCux0Td"),
new KeyValuePair<string, string>("skytron","DVqxof1JQ9at")
};
public IPAddress Host { get; }
public Dictionary<string, String> Hints => hints;
Dictionary<string, string> hints = new Dictionary<string, string>();
public NodeIdentifier(IPAddress host)
{
Host = host;
}
public void Identify()
{
TestSSH();
}
public void TestSSH()
{
byte[] buffer = new byte[256];
int l;
TcpClient tcp = new TcpClient();
try
{
SshClient ssh;
tcp.Connect(new IPEndPoint(Host, 13022));
tcp.ReceiveTimeout = 2500;
l = tcp.GetStream().Read(buffer, 0, buffer.Length);
tcp.Close();
hints.Add("SSH-CONNECT", Encoding.UTF8.GetString(buffer, 0, l));
Console.WriteLine("SSH-Connect-Banner: {0}", hints["SSH-CONNECT"]);
foreach (KeyValuePair<string,string> auth in sshAuthorizations)
{
ssh = new SshClient(Host.ToString(), 13022, auth.Key, auth.Value);
ssh.Connect();
if (ssh.IsConnected)
{
ssh.Disconnect();
hints.Add("SSH-LOGIN", auth.Key);
hints.Add("SSH-PASSWORD", auth.Value);
break;
}
ssh.Disconnect();
}
if (!hints.ContainsKey("SSH-LOGIN"))
return;
ssh = new SshClient(Host.ToString(), 13022, hints["SSH-LOGIN"], hints["SSH-PASSWORD"]);
ssh.Connect();
try
{
hints.Add("SSH_AVAILABLE", "YES");
hints.Add("SSH-VERSION", ssh.ConnectionInfo.ServerVersion);
MemoryStream stdout = new MemoryStream();
MemoryStream stderr = new MemoryStream();
Shell shell = ssh.CreateShell(Encoding.UTF8, "", stdout, stderr);
shell.Start();
Thread.Sleep(500);
shell.Stop();
hints.Add("SSH-SHELL-BANNER", Encoding.UTF8.GetString(stdout.GetBuffer()));
ssh.Disconnect();
} catch (Exception e)
{
Console.WriteLine(e);
if (ssh.IsConnected)
ssh.Disconnect();
}
} catch (Exception e)
{
hints.Add("SSH_AVAILABLE", "NO");
Console.WriteLine(e);
}
}
}
}