2010-06-21 77 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