Skip to content

Instantly share code, notes, and snippets.

@darinkes
Created April 10, 2014 13:09
Show Gist options
  • Save darinkes/10380451 to your computer and use it in GitHub Desktop.
Save darinkes/10380451 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using System.Timers;
using Renci.SshNet;
namespace SshTests
{
class Program
{
private static SshClient _client;
private static Task _task;
private static Timer _timer;
private static int _counter;
private static List<Dictionary<Exception, int>> _exceptions;
private static DateTime _startDateTime;
private static DateTime _endDateTime;
static void Main()
{
_exceptions = new List<Dictionary<Exception, int>>();
_client = new SshClient(res.host, int.Parse(res.port), res.user, res.password);
_client.ErrorOccurred += _client_ErrorOccurred;
_timer = new Timer(100);
_timer.Elapsed += _timer_Elapsed;
_counter = 0;
try
{
_client.Connect();
_startDateTime = DateTime.Now;
Console.WriteLine(@"SSH-Version: "+ _client.ConnectionInfo.ServerVersion);
Console.WriteLine(@"SSH -V: " + _client.RunCommand("ssh -V").Error);
_timer.Start();
}
catch (Exception exception)
{
Console.WriteLine(exception);
Debug.WriteLine(exception);
}
Console.WriteLine(@"Enter to stop test");
Console.ReadLine();
_timer.Stop();
if (_client != null && _client.IsConnected)
_client.Disconnect();
_endDateTime = DateTime.Now;
Console.WriteLine(@"Run-Time: " + (_endDateTime - _startDateTime));
Console.WriteLine(@"Exceptions: " + _exceptions.Count);
foreach (var exception in _exceptions)
{
Console.WriteLine(@"Runs: {0} ({1})", exception.First().Key.Message, exception.First().Value);
}
Console.WriteLine(@"Enter to exit");
Console.ReadLine();
}
static void _client_ErrorOccurred(object sender, Renci.SshNet.Common.ExceptionEventArgs e)
{
Console.Write(@"Error-Occured");
Console.WriteLine(e.Exception);
Debug.WriteLine(e.Exception);
var bla = new Dictionary<Exception, int>
{
{
e.Exception, _counter
}
};
_exceptions.Add(bla);
}
static void _timer_Elapsed(object sender, ElapsedEventArgs e)
{
if (_task != null && !_task.IsCompleted)
{
Console.WriteLine(@"Task still running");
return;
}
_task = Task.Factory.StartNew(() =>
{
try
{
if (_client == null || !_client.IsConnected)
{
Console.WriteLine(@"Client null or not connected");
if (_client != null)
{
_client.Connect();
_counter = 0;
}
else
_timer.Stop();
return;
}
_counter++;
Console.WriteLine(@"Run: {0}", _counter);
Debug.WriteLine("Run: {0}", _counter);
using (var shell = _client.CreateShellStream("xterm", 80, 80, 640, 480, 0))
{
Debug.WriteLine("Opened Shell");
//Console.WriteLine("Line: " + shell.ReadLine());
}
//Console.WriteLine(_client.RunCommand("ls -l").Result);
}
catch (Exception exception)
{
Console.WriteLine(exception);
Debug.WriteLine(exception);
Console.WriteLine(@"Connected: " + (_client == null ? "NULL" : _client.IsConnected.ToString()));
var bla = new Dictionary<Exception, int>
{
{
exception, _counter
}
};
_exceptions.Add(bla);
}
});
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment