| 263 | } |
| 264 | |
| 265 | func ExamplePublicKeys() { |
| 266 | var hostKey ssh.PublicKey |
| 267 | // A public key may be used to authenticate against the remote |
| 268 | // server by using an unencrypted PEM-encoded private key file. |
| 269 | // |
| 270 | // If you have an encrypted private key, the crypto/x509 package |
| 271 | // can be used to decrypt it. |
| 272 | key, err := os.ReadFile("/home/user/.ssh/id_rsa") |
| 273 | if err != nil { |
| 274 | log.Fatalf("unable to read private key: %v", err) |
| 275 | } |
| 276 | |
| 277 | // Create the Signer for this private key. |
| 278 | signer, err := ssh.ParsePrivateKey(key) |
| 279 | if err != nil { |
| 280 | log.Fatalf("unable to parse private key: %v", err) |
| 281 | } |
| 282 | |
| 283 | config := &ssh.ClientConfig{ |
| 284 | User: "user", |
| 285 | Auth: []ssh.AuthMethod{ |
| 286 | // Use the PublicKeys method for remote authentication. |
| 287 | ssh.PublicKeys(signer), |
| 288 | }, |
| 289 | HostKeyCallback: ssh.FixedHostKey(hostKey), |
| 290 | } |
| 291 | |
| 292 | // Connect to the remote server and perform the SSH handshake. |
| 293 | client, err := ssh.Dial("tcp", "host.com:22", config) |
| 294 | if err != nil { |
| 295 | log.Fatalf("unable to connect: %v", err) |
| 296 | } |
| 297 | defer client.Close() |
| 298 | } |
| 299 | |
| 300 | func ExampleClient_Listen() { |
| 301 | var hostKey ssh.PublicKey |