|
3 | 3 | [](https://github.com/wiktor-k/ssh-agent-lib/actions/workflows/rust.yml)
|
4 | 4 | [](https://crates.io/crates/ssh-agent-lib)
|
5 | 5 |
|
6 |
| -A collection of types for writing custom SSH agents as specified by the [SSH Agent Protocol Internet Draft](https://datatracker.ietf.org/doc/html/draft-miller-ssh-agent). |
| 6 | +A collection of types for writing custom SSH agents and connecting to existing ones. |
7 | 7 |
|
8 |
| -This makes it possible to utilize remote keys not supported by the default OpenSSH agent. |
| 8 | +The types in this crate closely follow the [SSH Agent Protocol Internet Draft](https://datatracker.ietf.org/doc/html/draft-miller-ssh-agent) specification and can be used to utilize remote keys not supported by the default OpenSSH agent. |
9 | 9 |
|
10 |
| -## Example |
| 10 | +## Examples |
| 11 | + |
| 12 | +The following examples show a sample agent and a sample client. |
| 13 | +For more elaborate example see the `examples` directory or [crates using `ssh-agent-lib`](https://crates.io/crates/ssh-agent-lib/reverse_dependencies). |
| 14 | + |
| 15 | +### Agent |
11 | 16 |
|
12 | 17 | The following example starts listening on a socket and processing requests.
|
13 | 18 | On Unix it uses `ssh-agent.sock` Unix domain socket while on Windows it uses a named pipe `\\.\pipe\agent`.
|
@@ -67,7 +72,32 @@ On Windows the path of the pipe has to be used:
|
67 | 72 | SSH_AUTH_SOCK=\\.\pipe\agent ssh user@example.com
|
68 | 73 | ```
|
69 | 74 |
|
70 |
| -For more elaborate example see the `examples` directory or [crates using `ssh-agent-lib`](https://crates.io/crates/ssh-agent-lib/reverse_dependencies). |
| 75 | +### Client |
| 76 | + |
| 77 | +The following example connects to the agent pointed to by the `SSH_AUTH_SOCK` environment variable and prints identities (public keys) that the agent knows of: |
| 78 | + |
| 79 | +```rust,no_run |
| 80 | +use service_binding::Binding; |
| 81 | +use ssh_agent_lib::client::connect; |
| 82 | +
|
| 83 | +#[tokio::main] |
| 84 | +async fn main() -> Result<(), Box<dyn std::error::Error>> { |
| 85 | + #[cfg(unix)] |
| 86 | + let mut client = |
| 87 | + connect(Binding::FilePath(std::env::var("SSH_AUTH_SOCK")?.into()).try_into()?)?; |
| 88 | +
|
| 89 | + #[cfg(windows)] |
| 90 | + let mut client = |
| 91 | + connect(Binding::NamedPipe(std::env::var("SSH_AUTH_SOCK")?.into()).try_into()?)?; |
| 92 | +
|
| 93 | + eprintln!( |
| 94 | + "Identities that this agent knows of: {:#?}", |
| 95 | + client.request_identities().await? |
| 96 | + ); |
| 97 | +
|
| 98 | + Ok(()) |
| 99 | +} |
| 100 | +``` |
71 | 101 |
|
72 | 102 | ## License
|
73 | 103 |
|
|
0 commit comments