-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support configuring the client in code using ssh_config options. (#227)
- Loading branch information
Showing
13 changed files
with
574 additions
and
328 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,34 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.CommandLine; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using Microsoft.Extensions.Logging; | ||
|
||
namespace Tmds.Ssh; | ||
|
||
class Program | ||
{ | ||
static async Task Main(string[] args) | ||
static Task<int> Main(string[] args) | ||
{ | ||
var destinationArg = new Argument<string>(name: "destination", () => "localhost") | ||
{ Arity = ArgumentArity.ZeroOrOne }; | ||
var commandArg = new Argument<string>(name: "command", () => "echo 'hello world'") | ||
{ Arity = ArgumentArity.ZeroOrOne }; | ||
var sshConfigOptions = new Option<string[]>(new[] { "-o", "--option" }, | ||
description: $"Set an SSH Config option, for example: [email protected].{Environment.NewLine}Supported options: {string.Join(", ", Enum.GetValues<SshConfigOption>().Select(o => o.ToString()))}.") | ||
{ Arity = ArgumentArity.ZeroOrMore }; | ||
|
||
var rootCommand = new RootCommand("Execute a command on a remote system over SSH."); | ||
rootCommand.AddOption(sshConfigOptions); | ||
rootCommand.AddArgument(destinationArg); | ||
rootCommand.AddArgument(commandArg); | ||
rootCommand.SetHandler(ExecuteAsync, destinationArg, commandArg, sshConfigOptions); | ||
|
||
return rootCommand.InvokeAsync(args); | ||
} | ||
|
||
static async Task ExecuteAsync(string destination, string command, string[] options) | ||
{ | ||
bool trace = IsEnvvarTrue("TRACE"); | ||
bool log = trace || IsEnvvarTrue("LOG"); | ||
|
@@ -21,10 +43,9 @@ static async Task Main(string[] args) | |
} | ||
}); | ||
|
||
string destination = args.Length >= 1 ? args[0] : "localhost"; | ||
string command = args.Length >= 2 ? args[1] : "echo 'hello world'"; | ||
SshConfigOptions configOptions = CreateSshConfigOptions(options); | ||
|
||
using SshClient client = new SshClient(destination, loggerFactory); | ||
using SshClient client = new SshClient(destination, configOptions, loggerFactory); | ||
|
||
using var process = await client.ExecuteAsync(command); | ||
Task[] tasks = new[] | ||
|
@@ -73,6 +94,32 @@ static void PrintExceptions(Task[] tasks) | |
} | ||
} | ||
|
||
private static SshConfigOptions CreateSshConfigOptions(string[] options) | ||
{ | ||
SshConfigOptions configOptions = new SshConfigOptions(SshConfigOptions.DefaultConfigFilePaths); | ||
|
||
Dictionary<SshConfigOption, SshConfigOptionValue> optionsDict = new(); | ||
foreach (var option in options) | ||
{ | ||
string[] split = option.Split('=', 2); | ||
if (split.Length != 2) | ||
{ | ||
throw new ArgumentException($"Option '{option}' is not in the <Key>=<Value> format."); | ||
} | ||
if (Enum.TryParse<SshConfigOption>(split[0], ignoreCase: true, out var key)) | ||
{ | ||
optionsDict[key] = split[1]; | ||
} | ||
else | ||
{ | ||
throw new ArgumentException($"Unsupported option: {option}."); | ||
} | ||
} | ||
configOptions.Options = optionsDict; | ||
|
||
return configOptions; | ||
} | ||
|
||
static bool IsEnvvarTrue(string variableName) | ||
{ | ||
string? value = Environment.GetEnvironmentVariable(variableName); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.