Usage Examples and how to redirect output to textbox.

Jan 6, 2012 at 12:47 PM

Hi,

Greetings.  I am a novice & newbie in .net area.  I would like to implement small project to stop/start packages in Unix box through C# front end GUI program.   I tried using sharp ssh, but was not able to redirect output to textbox.   I want the output to textbox and use string maniuplation functions to determine whether a particular package is running on that server or not.

If it is running, I want to start/stop the package.    Below is the few line of code using Sharp SSH, but I am not able to redirect the output to textbox.   I could not find examples of how to achieve the task with sharp ssh.  Now I have decided to use SSH.NET Library,  kind request you to help me with a simple basic example on how to do these kind of activities using SSH.NET Library.    As I am novice, please help me on how to use SSH.NET library also.  I am pretty sure that, this information can be found in usage document,  but I am unable to open it.    Also this information would have dealt in some older posts,   I am unable to find that also.   Sincerely apologize for opening up a new post.

 Regards,

Darshan

 

String

response;   

SshShell ssh = new SshShell("host", "user", "pass" );

ssh.Connect();

ssh.Expect(

"$" );
 

"$"); int readCount = 0;

 

byte[] buff = new byte [1024];

 

while (true )

{

readCount = ssh.IO.Read(buff, 0, buff.Length);

 

if (readCount == -1) break ;

response = System.Text.

Encoding .Default.GetString(buff, 0, readCount); 

}

richTextBox1.Text = response;

 

 

ssh.WriteLine(

"ls -l");

ssh.Expect(

 

Coordinator
Jan 10, 2012 at 2:38 PM

Hi,

 

You can execute command asynchronously, then you can read output on the fly as it becomes available, if command execution takes a while.

 

Here is an example:

			using (var ssh = new SshClient(connectionInfo))
			{
				ssh.Connect();
				var cmd = ssh.CreateCommand("ls -l");   //  very long list
				var asynch = cmd.BeginExecute(delegate(IAsyncResult ar)
				{
					Console.WriteLine("Finished.");
				}, null);

				var reader = new StreamReader(cmd.OutputStream);

				while (!asynch.IsCompleted)
				{
					var result = reader.ReadToEnd();
					if (string.IsNullOrEmpty(result))
						continue;
					Console.Write(result);
				}
				cmd.EndExecute(asynch);
			}

Hope it helps,

Thanks,

Oleg

Jan 11, 2012 at 9:01 AM

Thanks a lot for the example, Oleg.   Can you also please guide me on how to use this library ?  I mean after downloading Renci.SshNet folder, what should I do ?  What all dll's I need to refer ?   Sorry for asking such a basic question,  but I could not find this information.

 

 

 

Jan 17, 2012 at 10:02 AM

It's a class library. You can include/add to your Solution Renci.SshNet.csproj and then reference it in your own project.

Or, you can just open Renci.SshNet.csproj in Visual Studio and build it. Then go to the directory where it is locate and you will see a Bin folder with "Renci.SshNet.dll" in it. Reference that dll in your own project.

Feb 5, 2014 at 6:39 PM
Edited Feb 5, 2014 at 6:40 PM
Hello I'm trying to do the same as Nagadarshan and create an SSH terminal on a web page. I tried your code Oleg but I keep getting errors with this line:

using (var ssh = new SshClient(connectionInfo))

I have added both and referenced both the Renci.SshNet.csproj and Renci.SshNet.dll in my project but it still gives me errors, saying that SshClient isn't found but I get around that by writing "new Renci.SshNet.SshClient(connectionInfo))" (I don't know if this is right) but then no matter what I try it keeps saying that "connectionInfo" cannot be found anywhere.

Is there any chance someone could post up the code working, or even an example project?

That would help me out big time.

Thank you.