views:

172

answers:

1

Hey!

When Using SharpSSh and the SshExec class, I can't get the RunCommand to work, it always returns an empty string. When I debug the SharpSsh library it returns -1 when it tries to read the command from a stream. It works when I use the sftp class in the same library, but that class doesn't support all the ftp commands I need.

Here is a standard example, I can't get this to produce a correct result either

SshConnectionInfo input = Util.GetInput();
SshExec exec = new SshExec(input.Host, input.User);
if(input.Pass != null) exec.Password = input.Pass;
if(input.IdentityFile != null) exec.AddIdentityFile( input.IdentityFile );

Console.Write("Connecting...");
exec.Connect();
Console.WriteLine("OK");
while(true)
{
    Console.Write("Enter a command to execute ['Enter' to cancel]: ");
    string command = Console.ReadLine();
    if(command=="")break;
    string output = exec.RunCommand(command);                
    Console.WriteLine(output);
}
Console.Write("Disconnecting...");
exec.Close();
Console.WriteLine("OK");

Any ideas on how I can get the RunCommand function to run some commands? Thanks for any help :)

A: 

Hi, To get the standard output and error streams from .RunCommand, I'll repeat the answer I posted to: http://stackoverflow.com/questions/4008857/sharpssh-sshexec-run-command-and-wait-5-seconds-for-data

You may want to try the following overload:

SshExec exec = new SshExec("192.168.0.1", "admin", "haha");
exec.Connect();
string stdOut = null;
string stdError = null;
exec.RunCommand("interface wireless scan wlan1 duration=5", ref stdOut, ref stdError);

Console.WriteLine(stdOut);
exec.Close();

If their API does what the name implies, it should put the standard output of your command in stdOut and the standard error in stdError.

For more information about standard streams, check this out: http://en.wikipedia.org/wiki/Standard_streams

TimothyP