-
Notifications
You must be signed in to change notification settings - Fork 1
/
ssh.go
150 lines (121 loc) · 2.78 KB
/
ssh.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package main
import (
"bufio"
"fmt"
"io"
"io/ioutil"
"net"
"os"
"os/user"
"path/filepath"
"strings"
"sync"
"time"
"github.com/subchen/go-log"
"golang.org/x/crypto/ssh"
"golang.org/x/crypto/ssh/agent"
)
type SSHConnection struct {
*ssh.Client
}
type SSHHost struct {
unresolved string
resolved net.TCPAddr
}
// Creates a new SSHConnection.
func NewSSHConnection(config *ssh.ClientConfig, addr net.TCPAddr) (*SSHConnection, error) {
log.Debugln("connecting to", addr.String())
clientConn, err := ssh.Dial("tcp", addr.String(), config)
if err != nil {
return nil, fmt.Errorf("failed to dial %s", err)
}
return &SSHConnection{Client: clientConn}, nil
}
func (c *SSHConnection) Exec(command string) (ExecResult, error) {
res := ExecResult{}
session, err := c.NewSession()
if err != nil {
return res, err
}
defer session.Close()
stdout, err := session.StdoutPipe()
if err != nil {
return res, fmt.Errorf("unable to get session.StdoutPipe: %v", err)
}
stderr, err := session.StderrPipe()
if err != nil {
return res, fmt.Errorf("unable to get session.StderrPipe: %v", err)
}
startTime := time.Now()
remoteOutput := []OutErr{}
outputChan := make(chan OutErr, 256)
wg := &sync.WaitGroup{}
scanLines := func(stream StreamType, rdr io.Reader) {
scanner := bufio.NewScanner(rdr)
for scanner.Scan() {
outputChan <- OutErr{
Stream: stream,
Line: scanner.Text(),
Timestamp: time.Since(startTime).Nanoseconds() / 1000000,
}
}
wg.Done()
}
wg.Add(2)
go scanLines(Stdout, stdout)
go scanLines(Stderr, stderr)
go func() {
wg.Wait()
close(outputChan)
}()
outputDone := make(chan struct{})
go func() {
for outputLine := range outputChan {
remoteOutput = append(remoteOutput, outputLine)
}
outputDone <- struct{}{}
}()
res.Error = session.Run(command)
res.ExecutionTimeMicros = time.Since(startTime).Nanoseconds() / 1000000
err = res.Error
<-outputDone
res.Output = remoteOutput
return res, err
}
func sshAgent() ssh.AuthMethod {
if sshAgent, err := net.Dial("unix", os.Getenv("SSH_AUTH_SOCK")); err == nil {
return ssh.PublicKeysCallback(agent.NewClient(sshAgent).Signers)
}
return nil
}
func sshKeys() ([]ssh.AuthMethod, error) {
usr, err := user.Current()
if err != nil {
return nil, err
}
keyFiles, err := filepath.Glob(usr.HomeDir + "/.ssh/id_*")
if err != nil {
return nil, err
}
keys := []ssh.AuthMethod{}
for _, f := range keyFiles {
if !strings.HasSuffix(f, ".pub") {
key := publicKeyFile(f)
if key != nil {
keys = append(keys, key)
}
}
}
return keys, err
}
func publicKeyFile(keyPath string) ssh.AuthMethod {
buffer, err := ioutil.ReadFile(keyPath)
if err != nil {
return nil
}
key, err := ssh.ParsePrivateKey(buffer)
if err != nil {
return nil
}
return ssh.PublicKeys(key)
}