5

我在 CentOS 机器上运行 .NET Core 控制台应用程序。下面的代码正在执行普通命令,例如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 文件,需要提取并保存在当前位置。有人可以纠正我我在哪里吗

4

1 回答 1

11

如果应用程序在 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;
于 2017-09-29T12:13:06.623 回答