我想用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