Running a command that produces no output with SharpSSH

Posted by Paolo Tedesco on Stack Overflow See other posts from Stack Overflow or by Paolo Tedesco
Published on 2010-05-07T15:44:30Z Indexed on 2010/05/07 15:48 UTC
Read the original article Hit count: 751

Filed under:
|
|

I want to run a command using ssh.
I am using the SharpSSH library, as in this example:

using System;
using Tamir.SharpSsh;

class Program {
    static void Main(string[] args) {
        string hostName = "host.foo.com";
        string userName = "user";
        string privateKeyFile = @"C:\privatekey.private";
        string privateKeyPassword = "xxx";

        SshExec sshExec = new SshExec(hostName, userName);
        sshExec.AddIdentityFile(privateKeyFile, privateKeyPassword);
        sshExec.Connect();
        string command = string.Join(" ", args);
        Console.WriteLine("command = {0}", command);
        string output = sshExec.RunCommand(command);

        int code = sshExec.ChannelExec.getExitStatus();
        sshExec.Close();
        Console.WriteLine("code = {0}", code);
        Console.WriteLine("output = {0}", output);
    }
}

My problem is that when the command I run produces no output, I get -1 as return code, instead of the code returned by the command on the remote machine.
Has someone encountered this problem, or am I doing something wrong?

© Stack Overflow or respective owner

Related posts about c#

Related posts about ssh