2017-09-26 139 views
0

Im在CentOs上运行dotnet核心控制台应用程序。以下代码正在执行正常命令liki“正常运行时间”。但不执行的“LZ4 -dc --no稀疏vnp.tar.lz4 |焦油XF - Logs.pdf”使用dotnet核心在Centos上执行linux命令

try 
     { 
     var connectionInfo = new ConnectionInfo("server","username",new PasswordAuthenticationMethod("username", "pwd")); 
      using (var client = new SshClient(connectionInfo)) 
      { 
       client.Connect(); 
           Console.WriteLine("Hello World!"); 
       var command=client.CreateCommand("lz4 -dc --no-sparse vnp.tar | tar xf - Logs.pdf"); 
       var result = command.Execute(); 
       Console.WriteLine("yup ! UNIX Commands Executed from C#.net Application");     
       Console.WriteLine("Response came form UNIX Shell" + result); 

       client.Disconnect(); 
      } 
     } 
     catch (Exception ex) 
     { 

      Console.WriteLine(ex.Message); 
     } 

预计产量Logs.pdf文件需要被提取并保存在当前位置。有人可以纠正我在哪里IM

回答

0

如果应用程序是在Linux机器上运行,那么你可以试试这个也:

string command = "write your command here"; 
string result = ""; 
using (System.Diagnostics.Process proc = new System.Diagnostics.Process()) 
{ 
    proc.StartInfo.FileName = "/bin/bash"; 
    proc.StartInfo.Arguments = "-c \" " + command + " \""; 
    proc.StartInfo.UseShellExecute = false; 
    proc.StartInfo.RedirectStandardOutput = true; 
    proc.StartInfo.RedirectStandardError = true; 
    proc.Start(); 

    result += proc.StandardOutput.ReadToEnd(); 
    result += proc.StandardError.ReadToEnd(); 

    proc.WaitForExit(); 
} 
return result;