37 lines
772 B
C#
37 lines
772 B
C#
using System.Text;
|
|
|
|
namespace McpSsh.Server.Ssh;
|
|
|
|
public static class RemoteShellCommand
|
|
{
|
|
public static string Build(string command, string? cwd)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(cwd))
|
|
{
|
|
return command;
|
|
}
|
|
|
|
return $"cd {Quote(cwd)} && {command}";
|
|
}
|
|
|
|
public static string Quote(string value)
|
|
{
|
|
var builder = new StringBuilder(value.Length + 2);
|
|
builder.Append('\'');
|
|
foreach (var character in value)
|
|
{
|
|
if (character == '\'')
|
|
{
|
|
builder.Append("'\\''");
|
|
}
|
|
else
|
|
{
|
|
builder.Append(character);
|
|
}
|
|
}
|
|
|
|
builder.Append('\'');
|
|
return builder.ToString();
|
|
}
|
|
}
|