Skip to content

Commit b70753f

Browse files
committed
Add client example to README.md
Signed-off-by: Wiktor Kwapisiewicz <wiktor@metacode.biz>
1 parent 082fa3b commit b70753f

File tree

1 file changed

+34
-4
lines changed

1 file changed

+34
-4
lines changed

README.md

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,16 @@
33
[![CI](https://github.com/wiktor-k/ssh-agent-lib/actions/workflows/rust.yml/badge.svg)](https://github.com/wiktor-k/ssh-agent-lib/actions/workflows/rust.yml)
44
[![Crates.io](https://img.shields.io/crates/v/ssh-agent-lib)](https://crates.io/crates/ssh-agent-lib)
55

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.
77

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.
99

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
1116

1217
The following example starts listening on a socket and processing requests.
1318
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:
6772
SSH_AUTH_SOCK=\\.\pipe\agent ssh user@example.com
6873
```
6974

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+
```
71101

72102
## License
73103

0 commit comments

Comments
 (0)