使用dotnet core在Centos上执行linux命令
我正在CentOS box上运行.NET核心控制台应用程序。下面的代码是针对正常命令执行的,如使用dotnet core在Centos上执行linux命令,centos,.net-core,tar,lz4,Centos,.net Core,Tar,Lz4,我正在CentOS box上运行.NET核心控制台应用程序。下面的代码是针对正常命令执行的,如uptime,但不针对lz4-dc--no sparse vnp.tar.lz4 | tar xf-Logs.pdf: 试试看 { var connectionInfo=new connectionInfo(“服务器”、“用户名”、新密码身份验证方法(“用户名”、“密码”); 使用(var客户端=新的SshClient(connectionInfo)) { client.Connect(); 控制台。W
uptime
,但不针对lz4-dc--no sparse vnp.tar.lz4 | tar xf-Logs.pdf
:
试试看
{
var connectionInfo=new connectionInfo(“服务器”、“用户名”、新密码身份验证方法(“用户名”、“密码”);
使用(var客户端=新的SshClient(connectionInfo))
{
client.Connect();
控制台。WriteLine(“你好,世界!”);
var command=client.CreateCommand(“lz4-dc——无稀疏vnp.tar | tar xf-Logs.pdf”);
var result=command.Execute();
WriteLine(“是的!从C#.net应用程序执行的UNIX命令”);
WriteLine(“响应来自unixshell”+结果);
client.Disconnect();
}
}
捕获(例外情况除外)
{
控制台写入线(例如消息);
}
预期输出为Logs.pdf文件,需要提取并保存在当前位置。有人能告诉我im的位置吗?如果应用程序在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;
若命令包含引号,则应像
command=command.Replace(“\”,“\ \”)一样对其进行转义代码>如果我需要文件夹/opt/myFile中ls命令的输出,文件名和参数的值应该是什么?