-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathssh.go
150 lines (132 loc) · 2.73 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 sshutils
import (
"bytes"
"fmt"
"log"
"os"
"sync"
"github.com/pkg/sftp"
"golang.org/x/crypto/ssh"
)
var lock sync.Mutex
var ptyModes = ssh.TerminalModes{
ssh.ECHO: 0,
ssh.TTY_OP_ISPEED: 14400,
ssh.TTY_OP_OSPEED: 14400,
}
type RealTimePrinter struct {
prefix []byte
buf []byte
}
func (p *RealTimePrinter) Write(b []byte) (int, error) {
p.buf = append(p.buf, b...)
for {
c := bytes.Index(p.buf, []byte{'\n'})
if c < 0 {
break
}
w := append(p.prefix, p.buf[:c+1]...)
lock.Lock()
if n, err := os.Stdout.Write(w); err != nil {
lock.Unlock()
return n, err
}
lock.Unlock()
p.buf = p.buf[c+1:]
}
return len(b), nil
}
func (p *RealTimePrinter) End() {
// Flush last bytes if data didn't end with a new-line
if len(p.buf) > 0 {
p.Write([]byte{'\n'})
}
}
type Client struct {
*ssh.Client
ses *ssh.Session
SFTP *sftp.Client
Name string
Stdout bytes.Buffer
Stderr bytes.Buffer
}
type Runner interface {
Prepare(c *Client, h string, args []string) (string, error)
Clean(c *Client, h string) error
}
type (
//RunFunc func(c *Client, h string, a []string) error
WaitChan chan struct{}
)
func (c *Client) Run(cmd string) error {
if c.ses == nil {
s, err := c.NewSession() // TODO: never closed
if err != nil {
return err
}
// This has the side-effect of killing the process if the user
// disconnects; add flag?
if err := s.RequestPty("xterm", 80, 40, ptyModes); err != nil {
log.Println(c.Name, "request-pty error:", err)
}
r := &RealTimePrinter{[]byte(c.Name + "> "), nil}
defer r.End()
s.Stdout = r
s.Stderr = r
c.ses = s
}
return c.ses.Run(cmd)
}
var (
auths []ssh.AuthMethod
defaultUser string
forceUser string
)
func Run(h Target, kh *KnownHosts, r Runner, args []string) WaitChan {
c := make(WaitChan, 1)
go func() {
defer close(c)
u := h.User
if forceUser != "" {
u = forceUser
}
if u == "" {
u = defaultUser
}
hka := kh.GetHKA(h.canonical())
cfg := &ssh.ClientConfig{
User: u,
Auth: auths,
HostKeyAlgorithms: hka,
HostKeyCallback: kh.VerifyKey,
}
//log.Println("HKA for", h.canonical(), "=", cfg.HostKeyAlgorithms)
host := h.dialer()
c, err := ssh.Dial("tcp", host, cfg)
if err != nil {
log.Println(h.Name, "failed:", fmt.Errorf("Dialing %v: %w", host, err))
return
}
defer c.Close()
cli := &Client{Client: c, Name: h.Name}
defer r.Clean(cli, host)
cmd, err := r.Prepare(cli, host, args)
if err != nil {
log.Println(h.Name, "failed:", err)
}
if err := cli.Run(cmd); err != nil {
log.Println(h.Name, "failed:", err)
}
}()
return c
}
func WaitAll(ws []WaitChan) {
for _, w := range ws {
ok := true
for ok {
select {
case _, ok = <-w:
}
}
}
}