Jump to content
mindark

SSH checker

Recommended Posts

Posted (edited)

Program scris în C# care sorteaz? o lista de host-uri cu ssh rulat pe port 22, în servere ?i routere, în dependen?? de cît RAM posed?.

Hosturile într-un fi?ier aparte în format:

host:user:pass

Programul îl rulati prin cmd.

Îl modific s? ar?te ?ara, uptime, viteza de internet etc., dac? topicul cap?t? aten?ie.

Poftim link.

Iat? ?i sursa, dac? cuiva îi este interesant


using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text.RegularExpressions;
using System.Threading;
using Renci.SshNet;

namespace sshb
{
class Program
{
private static readonly Queue<Cred> Hosts = new Queue<Cred>();
private static readonly ManualResetEvent Signal = new ManualResetEvent(false);
private static int _threads;
private static readonly object Locker = new object();
private static TextWriter _routers;
private static TextWriter _servers;

static bool CheckHost(string server, int port)
{
try
{
using (var client = new TcpClient())
{
client.BeginConnect(IPAddress.Parse(server), port, null, null).AsyncWaitHandle.WaitOne(500, true);
if (client.Connected)
return true;
return false;
}
}
catch (Exception)
{
return false;
}
}

private static void DoWork()
{
try
{
while (Hosts.Count > 0)
{
Cred host;
try
{
host = Hosts.Dequeue();
}
catch
{
continue;
}
if (!CheckHost(host.Ip, 22))
{
continue;
}
try
{
using (var client = new SshClient(host.Ip, host.Login, host.Password))
{
Console.WriteLine("trying " + host.Ip + " with login " + host.Login + " and password " +
host.Password);
client.ConnectionInfo.Timeout = TimeSpan.FromSeconds(5);
client.Connect();
try
{
var cmd = client.CreateCommand("cat /proc/meminfo");
cmd.CommandTimeout = TimeSpan.FromSeconds(5);
cmd.Execute();
var ram = Int32.Parse(Regex.Matches(cmd.Result, "MemTotal:.*?(\\d+)")[0].Groups[1].Value);
if (ram / 1024 < 512)
{
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("router: " + host.Ip);
Console.ForegroundColor = ConsoleColor.White;
_routers.WriteLine(host.Ip + ":" + host.Login + ":" + host.Password);
_routers.Flush();
}
else
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("server: " + host.Ip);
Console.ForegroundColor = ConsoleColor.White;
_servers.WriteLine(host.Ip + ":" + host.Login + ":" + host.Password);
_servers.Flush();
}
}
catch
{
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("router: " + host.Ip);
Console.ForegroundColor = ConsoleColor.White;
//If something went wrong with command execution, but it connects
_routers.WriteLine(host.Ip + ":" + host.Login + ":" + host.Password);
_routers.Flush();
}
client.Disconnect();
}
}
catch
{
}
}
}
catch
{
}
lock (Locker)
{
if (Interlocked.Decrement(ref _threads) == 0)
{
Signal.Set();
}
}
}
public static void Main(string[] args)
{
if (args.Count() != 4)
{
Console.WriteLine("sshch <threads> <ips> <routers file> <servers file>");
}
else
{
_routers = new StreamWriter(args[2]);
_servers = new StreamWriter(args[3]);
_threads = Convert.ToInt32(args[0]);
var path = args[1];
if (!File.Exists(path))
{
Console.WriteLine("Some arguments files are missing!");
}
else if (_threads > File.ReadAllLines(path).Count())
{
Console.WriteLine("Threads must be more than ips!");
}
else
{
File.ReadAllLines(path).ToList().ForEach(c => Hosts.Enqueue(new Cred() { Ip = c.Split(':')[0], Login = c.Split(':')[1], Password = c.Split(':')[2] }));
for (var i = 0; i < _threads; i++)
{
new Thread(DoWork).Start();
}
Signal.WaitOne();
_routers.Close();
_servers.Close();
}
}
}
}
class Cred
{
public string Ip { get; set; }
public string Login { get; set; }
public string Password { get; set; }
}
}

Edited by mindark
  • Upvote 1
Posted (edited)

te rog fi mai explicit nu prea am inteles cum se foloseste poate un tutorial video ar fi super

C:\Users\dark\Desktop\ssh chk>sshch.exe

sshch <threads> <ips> <routers file> <servers file>

C:\Users\dark\Desktop\ssh chk>sshch.exe mai departe ? Multumesc.

Edited by d4rkm4nx99

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



×
×
  • Create New...