2010-06-21 79 views
0

当使用SharpSSh和SshExec类时,我无法使RunCommand正常工作,它始终返回一个空字符串。当我调试SharpSsh库时,它在尝试从流中读取命令时返回-1。它在我在同一个库中使用sftp类时起作用,但该类不支持我需要的所有ftp命令。SharpSSh:SshExec中的RunCommand始终返回一个空字符串

这是一个标准的例子,我不能让这个产生正确的结果要么

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"); 

我如何能得到RunCommand函数来运行一些命令任何想法? 感谢您的帮助:)

回答

1

从.RunCommand得到标准输出和错误流, 我重复我贴出答案:SharpSSH - SSHExec, run command, and wait 5 seconds for data!

你可能想尝试以下过载:

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(); 

如果他们的API做了名字所暗示的,它应该将stdOut中的命令的标准输出和stdError中的标准错误。

有关标准流的更多信息,请查看:http://en.wikipedia.org/wiki/Standard_streams