Last active
August 29, 2015 13:58
-
-
Save darinkes/10024037 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Diagnostics; | |
using System.Threading.Tasks; | |
using System.Timers; | |
using Renci.SshNet; | |
namespace SshNetWaitHandleIssue | |
{ | |
class Program | |
{ | |
private static SshClient _client; | |
private static Task _task; | |
private static Timer _timer; | |
private static int _counter; | |
static void Main() | |
{ | |
_client = new SshClient(res.host, int.Parse(res.port), res.user, res.password); | |
_client.ErrorOccurred += _client_ErrorOccurred; | |
_timer = new Timer(500); | |
_timer.Elapsed += _timer_Elapsed; | |
_counter = 0; | |
try | |
{ | |
_client.Connect(); | |
_timer.Start(); | |
} | |
catch (Exception exception) | |
{ | |
Console.WriteLine(exception); | |
Debug.WriteLine(exception); | |
} | |
Console.WriteLine(@"Enter to Exit"); | |
Console.ReadLine(); | |
_timer.Stop(); | |
if (_client != null && _client.IsConnected) | |
_client.Disconnect(); | |
} | |
static void _client_ErrorOccurred(object sender, Renci.SshNet.Common.ExceptionEventArgs e) | |
{ | |
Console.Write("Error-Occured"); | |
Console.WriteLine(e.Exception); | |
Debug.WriteLine(e.Exception); | |
} | |
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"); | |
_timer.Stop(); | |
return; | |
} | |
_counter++; | |
Console.WriteLine("Run: {0}", _counter); | |
using (var shell = _client.CreateShellStream("dumb", 80, 80, 640, 480, 0)) | |
{ | |
Console.WriteLine("Line: " + shell.ReadLine()); | |
} | |
//Console.WriteLine(_client.RunCommand("ls -l").Result); | |
} | |
catch (Exception exception) | |
{ | |
Console.WriteLine(exception); | |
Debug.WriteLine(exception); | |
_timer.Stop(); | |
} | |
}); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment