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#.
- 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
A new config can be created directly from a ssh file on the disk
varconfigRes=SshConfig.FromFile("path/to/config");if(configRes.IsFailed){configRes.Errors;// Parsing errors can be found herereturn;}varconfig=configRes.Value;Alternatively a config can be created from a string representing the config
varconfigRes=SshConfig.Deserialize("Host test\n User testuser");if(configRes.IsFailed){configRes.Errors;// Parsing errors can be found herereturn;}varconfig=configRes.Value;vartestHost=config.Find("test");// An uncoupled host containing the "User" argumentvartestUser=testHost.User;// testuserA config can also be created directly from code
varconfig=newSshConfig();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
varconfig=newSshConfig();varinsertionRes=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
varconfig=newSshConfig();config.Insert(-1,Keyword.Match,"user testuser host testhost");Additionally there are many other ways, that provide a cleaner way to edit
varconfig=newSshConfig();config.Port=1234;config.Set<ushort>(Keyword.Port,1234);config.PushHost("hostName", host =>{host.User="username";});Of course values can be removed again
varconfig=newSshConfig();config.Port=1234;config.Remove(Keyword.Port);config.PushHost("testHost");config.Remove(parameter =>parameter.IsHost());Values can be queried in various different ways
varconfig=newSshConfig();config.Port=123;config.IdentityFile="~/.ssh/id_rsa1"
config.Insert(-1,Keyword.IdentityFile,"~/.ssh/id_rsa2");varport=config.Port;port=config.Get<ushort>(Keyword.Port);varnumberOfIdentities=config.Count(parameter =>parameter.Is(Keyword.IdentityFile));// [ "~/.ssh/id_rsa1", "~/.ssh/id_rsa2" ]varidentityFiles=config.WhereParam(Keyword.IdentityFile).ToList();varid_rsa2=config.WhereParam(Keyword.IdentityFile).SelectArg().First(fileName =>fileName.EndsWith("2"));varconfig=newSshConfig();config.PushHost("hostName");config.WriteFile("path/to/output/config");