github.com/glycerine/xcryptossh@v7.0.4+incompatible/agent/example_test.go (about)

     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  	"context"
     9  	"log"
    10  	"net"
    11  	"os"
    12  
    13  	ssh "github.com/glycerine/xcryptossh"
    14  	"github.com/glycerine/xcryptossh/agent"
    15  )
    16  
    17  func ExampleClientAgent() {
    18  	ctx, cancelctx := context.WithCancel(context.Background())
    19  	defer cancelctx()
    20  
    21  	halt := ssh.NewHalter()
    22  	defer halt.ReqStop.Close()
    23  
    24  	// ssh-agent has a UNIX socket under $SSH_AUTH_SOCK
    25  	socket := os.Getenv("SSH_AUTH_SOCK")
    26  	conn, err := net.Dial("unix", socket)
    27  	if err != nil {
    28  		log.Fatalf("net.Dial: %v", err)
    29  	}
    30  	agentClient := agent.NewClient(conn)
    31  	config := &ssh.ClientConfig{
    32  		User: "username",
    33  		Auth: []ssh.AuthMethod{
    34  			// Use a callback rather than PublicKeys
    35  			// so we only consult the agent once the remote server
    36  			// wants it.
    37  			ssh.PublicKeysCallback(agentClient.Signers),
    38  		},
    39  		HostKeyCallback: ssh.InsecureIgnoreHostKey(),
    40  		Config:          ssh.Config{Halt: halt},
    41  	}
    42  
    43  	sshc, err := ssh.Dial(ctx, "tcp", "localhost:22", config)
    44  	if err != nil {
    45  		log.Fatalf("Dial: %v", err)
    46  	}
    47  	// .. use sshc
    48  	sshc.Close()
    49  }