使用dotnet core在Centos上执行linux命令

时间:2017-09-26 06:25:18

标签: centos .net-core tar lz4

我在CentOs盒子上运行一个dotnet核心控制台应用程序。以下代码正在执行正常命令liki“uptime”。但没有执行“lz4 -dc --no-sparse vnp.tar.lz4 | tar xf - Logs.pdf”

 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文件需要提取并保存在当前位置。有人能纠正我的地方吗

1 个答案:

答案 0 :(得分: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;