我想用ssh運行一個命令。
我使用SharpSSH library,如下例:運行一個不產生輸出的命令,用SharpSSH
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);
}
}
我的問題是,當我運行命令不產生輸出,我得-1作爲返回代碼,而不是由命令返回上的代碼遠程機器。
有人遇到過這個問題,或者我做錯了什麼?
您是否嘗試過使用RunCommand的重載並忽略stdOut和stdErr並讀取退出的代碼?我想這似乎沒有太大區別。 – kamranicus 2011-01-20 20:50:56