Skip to content

Example code unclear, please describe #10

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
ygoe opened this issue Apr 9, 2025 · 3 comments
Open

Example code unclear, please describe #10

ygoe opened this issue Apr 9, 2025 · 3 comments

Comments

@ygoe
Copy link

ygoe commented Apr 9, 2025

The readme gives me this example code:

var agent = new SshAgent();

var keyFile = new PrivateKeyFile("test.key");
agent.AddIdentity(keyFile);

var keys = agent.RequestIdentities();

using var client = new SshClient("ssh.foo.com", "root", keys);
client.Connect();
Console.WriteLine(client.RunCommand("hostname").Result);

It throws an error saying it can't find a file "test.key" in my current directory. What does this code mean? I want to add SSH agent support to my existing application, that's all. I managed to integrate it like this:

if (string.IsNullOrWhiteSpace(activeProfile.Password) &&
	string.IsNullOrWhiteSpace(activeProfile.KeyFileName))
{
	var agent = new SshAgent();
	var keyFile = new PrivateKeyFile("test.key");
	agent.AddIdentity(keyFile);
	var keys = agent.RequestIdentities();
	authMethods.Add(new PrivateKeyAuthenticationMethod(activeProfile.UserName, keys));
}

connectionInfo = new ConnectionInfo(activeProfile.HostName, port, activeProfile.UserName, authMethods.ToArray());
using (var sshClient = new SshClient(connectionInfo))
// ...

So I added this to the authMethods, hope that's correct. But what about that file name?

@darinkes
Copy link
Owner

var keyFile = new PrivateKeyFile("test.key");
agent.AddIdentity(keyFile);

This is the example to load/add a ssh key from a file into the agent.

@ygoe
Copy link
Author

ygoe commented Apr 12, 2025

Ah, I wasn't expecting the code to send keys to the agent. To me, the agent is only providing keys, and other software somehow manages it (in my case KeePassXC).

Maybe a short descriptive comment above each part of the code would help to explain it. Usually, example code should contain such explanations because the reader isn't familiar with it yet.

@darinkes
Copy link
Owner

This extensions implements the full SSH agent protocol, also linked in the README.
MRs are always welcome if you find something to improve 👍
For me AddIdentity() was kinda self-explanatory and a comment "adds a identity" seemed redundant.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants