Innovenergy_trunk/csharp/Lib/SysTools/Edges/RemoteCommandToProcess.cs

50 lines
1.5 KiB
C#
Raw Normal View History

using InnovEnergy.Lib.SysTools.Process;
using InnovEnergy.Lib.SysTools.Remote;
2023-02-16 12:57:06 +00:00
namespace InnovEnergy.Lib.SysTools.Edges;
2023-02-16 12:57:06 +00:00
public static class RemoteCommandToProcess
{
public static ProcessResult ExecuteBlocking(this RemoteCommand command, Dictionary<String, String> variables = null, Boolean logToConsole = false)
{
return command
.ToLocalCommand()
.ExecuteBlocking(variables, logToConsole);
}
public static AsyncProcess ToAsyncProcess(this RemoteCommand command, Dictionary<String, String> variables = null)
{
return command
.ToLocalCommand()
.ToAsyncProcess(variables);
}
public static SyncProcess ToSyncProcess(this RemoteCommand command, Dictionary<String, String> variables = null)
{
return command
.ToLocalCommand()
.ToSyncProcess(variables);
}
private static SysCommand ToLocalCommand(this RemoteCommand command)
{
var ssh = "ssh"
.Opt("o", "ConnectTimeout=5") // TODO
.Opt("o", "PasswordAuthentication=no")
.Opt("o", "StrictHostKeyChecking=no")
.Opt("o", "UserKnownHostsFile=/dev/null")
.Opt("o", "LogLevel=ERROR");
var host = command.Host;
if (host.KeyFile.HasValue)
ssh = ssh.Opt("i", host.KeyFile.Value);
if (host.Port != 22)
ssh = ssh.Opt("p", host.Port);
return ssh
.Arg($"{host.User}@{host.Address}")
.Arg(command.Path + command.Args);
}
}