Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
Open more actions menu

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

37 Commits
37 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

SshTools

Introduction

The ssh config file is a very powerful tool to specify a ssh connection. In C# we have access to the awesome ssh library SSH.NET, but this library does not aim for providing config file parsing support or a very intuitive way of defining hosts. Inspired by the Ssh-Config-Parser library the goal was to provide a library, that links the world of config files to ssh in c#.

Features

  • Configure custom Keywords, Tokens or Criteria
  • Parse or create a new SshConfig
  • Edit this config and it's Hosts / Matches / Arguments
  • Find a matching Host
  • Use this Host to connect with the help of Ssh.Net
  • Save the edited config back to the disk
  • Usage of FluentResults to explain failure
  • Using IEnumerable as base for the config, so LINQ can be used to query parameters

Usage

Creating a new config

A new config can be created directly from a ssh file on the disk

var configRes = SshConfig.FromFile("path/to/config");
if (configRes.IsFailed)
{
    configRes.Errors; // Parsing errors can be found here
    return;
}
var config = configRes.Value;

Alternatively a config can be created from a string representing the config

var configRes = SshConfig.Deserialize("Host test\n  User testuser");
if (configRes.IsFailed)
{
    configRes.Errors; // Parsing errors can be found here
    return;
}
var config = configRes.Value;
var testHost = config.Find("test"); // An uncoupled host containing the "User" argument
var testUser = testHost.User; // testuser

A config can also be created directly from code

var config = new SshConfig();

Editing the config

A new Argument can be inserted easily anywhere by providing

  • index (negative numbers will count from the end)
  • The argument's Keyword (e.g. User, Port, Host, etc)
  • The value, that is complementary to the Keyword's expected type
var config = new SshConfig();
var insertionRes = config.Insert<ushort>(0, Keyword.Port, 1234);

Nodes (Host / Match) can be inserted too, but there is an overload, where the matchString can be defined

var config = new SshConfig();
config.Insert(-1, Keyword.Match, "user testuser host testhost");

Additionally there are many other ways, that provide a cleaner way to edit

var config = new SshConfig();
config.Port = 1234;
config.Set<ushort>(Keyword.Port, 1234);
config.PushHost("hostName", host =>
{
    host.User = "username";
});

Of course values can be removed again

var config = new SshConfig();
config.Port = 1234;
config.Remove(Keyword.Port);

config.PushHost("testHost");
config.Remove(parameter => parameter.IsHost());

Getting values from the config

Values can be queried in various different ways

var config = new SshConfig();
config.Port = 123;
config.IdentityFile = "~/.ssh/id_rsa1"
config.Insert(-1, Keyword.IdentityFile, "~/.ssh/id_rsa2");

var port = config.Port;
port = config.Get<ushort>(Keyword.Port);

var numberOfIdentities = config.Count(parameter => parameter.Is(Keyword.IdentityFile));
// [ "~/.ssh/id_rsa1", "~/.ssh/id_rsa2" ]
var identityFiles = config.WhereParam(Keyword.IdentityFile).ToList();
var id_rsa2 = config
    .WhereParam(Keyword.IdentityFile)
    .SelectArg()
    .First(fileName => fileName.EndsWith("2"));

Export the config

var config = new SshConfig();
config.PushHost("hostName");
config.WriteFile("path/to/output/config");

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

Packages

Contributors

Languages

Morty Proxy This is a proxified and sanitized view of the page, visit original site.