I have 2 problems with using SSH.NET library.
I would like to create SSH connection and then disconnect. But if I start asynchronous read, disconnecting causes problems. My program simply freezes when I try to end a connection.
public void button1_Click(object sender, EventArgs e)
{
var ssh = new SshClient(IP, UserName, Password);
ssh.Connect();
var stream = ssh.CreateShellStream("anything", 80, 24, 800, 600, 4096);
byte[] buffer = new byte[1000];
stream.BeginRead(buffer, 0, buffer.Length, null, null);
stream.DataReceived += new EventHandler<Renci.SshNet.Common.ShellDataEventArgs>(
(s, ex) =>
{
Invoke((MethodInvoker)delegate
{
textBox1.AppendText(stream.Read());
});
}
);
stream.WriteLine("ls");
stream.Dispose();
ssh.Disconnect(); //problem here
}
I also do not know how to create a connection that will be availbe from anywhere in the code. I would like to be able to define IP, UserName and Password (e.g. write them in some textBoxes), establish a connection and then interact with it by executing some commands and getting input. Passing the credentials, as I understand, requires some event hanlder (e.g. button1_Click), but then if I would like to interact with the created stream = ssh.CreateShellStream(...) by passing commands with e.g. button2_Click, I cannot do that because "The name "stream" does not exist in the current context".
Earlier I have been doing it by plik (from PuTTY) and Process functionality of C#:
private void ConnectButton_Click(object sender, EventArgs e)
{
...
p = new Process();
...
sw = p.StandardInput;
...
}
where sw is a streamwriter defined outside of the ConnectButton_Click event handler.
Thanks.