example_test.go 958 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. // Copyright 2016 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package agent_test
  5. import (
  6. "log"
  7. "net"
  8. "os"
  9. "golang.org/x/crypto/ssh"
  10. "golang.org/x/crypto/ssh/agent"
  11. )
  12. func ExampleClientAgent() {
  13. // ssh-agent has a UNIX socket under $SSH_AUTH_SOCK
  14. socket := os.Getenv("SSH_AUTH_SOCK")
  15. conn, err := net.Dial("unix", socket)
  16. if err != nil {
  17. log.Fatalf("net.Dial: %v", err)
  18. }
  19. agentClient := agent.NewClient(conn)
  20. config := &ssh.ClientConfig{
  21. User: "username",
  22. Auth: []ssh.AuthMethod{
  23. // Use a callback rather than PublicKeys
  24. // so we only consult the agent once the remote server
  25. // wants it.
  26. ssh.PublicKeysCallback(agentClient.Signers),
  27. },
  28. HostKeyCallback: ssh.InsecureIgnoreHostKey(),
  29. }
  30. sshc, err := ssh.Dial("tcp", "localhost:22", config)
  31. if err != nil {
  32. log.Fatalf("Dial: %v", err)
  33. }
  34. // .. use sshc
  35. sshc.Close()
  36. }