OSDN Git Service

new repo
[bytom/vapor.git] / vendor / golang.org / x / crypto / ssh / agent / example_test.go
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
5 package agent_test
6
7 import (
8         "log"
9         "net"
10         "os"
11
12         "golang.org/x/crypto/ssh"
13         "golang.org/x/crypto/ssh/agent"
14 )
15
16 func ExampleClientAgent() {
17         // ssh-agent has a UNIX socket under $SSH_AUTH_SOCK
18         socket := os.Getenv("SSH_AUTH_SOCK")
19         conn, err := net.Dial("unix", socket)
20         if err != nil {
21                 log.Fatalf("net.Dial: %v", err)
22         }
23         agentClient := agent.NewClient(conn)
24         config := &ssh.ClientConfig{
25                 User: "username",
26                 Auth: []ssh.AuthMethod{
27                         // Use a callback rather than PublicKeys
28                         // so we only consult the agent once the remote server
29                         // wants it.
30                         ssh.PublicKeysCallback(agentClient.Signers),
31                 },
32                 HostKeyCallback: ssh.InsecureIgnoreHostKey(),
33         }
34
35         sshc, err := ssh.Dial("tcp", "localhost:22", config)
36         if err != nil {
37                 log.Fatalf("Dial: %v", err)
38         }
39         // .. use sshc
40         sshc.Close()
41 }