-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsshbuddy.go
More file actions
61 lines (54 loc) · 1.15 KB
/
sshbuddy.go
File metadata and controls
61 lines (54 loc) · 1.15 KB
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
package iotdaemon
import (
"errors"
"os"
"golang.org/x/crypto/ssh"
)
type SSHBuddy struct {
User string
Address string
Command string
KeyPath string
KeyPassphrase string
InsecureHosts bool
}
func (b *SSHBuddy) Run() ([]byte, error) {
cfg := &ssh.ClientConfig{
User: b.User,
Auth: []ssh.AuthMethod{},
}
if b.InsecureHosts {
cfg.HostKeyCallback = ssh.InsecureIgnoreHostKey()
} else {
return nil, errors.New("unsupported")
}
if b.KeyPath == "" {
return nil, errors.New("unsupported: need key")
}
data, err := os.ReadFile(b.KeyPath)
if err != nil {
return nil, err
}
signer, err := b.parse(data)
if err != nil {
return nil, err
}
cfg.Auth = append(cfg.Auth, ssh.PublicKeys(signer))
conn, err := ssh.Dial("tcp", b.Address, cfg)
if err != nil {
return nil, err
}
defer conn.Close()
sess, err := conn.NewSession()
if err != nil {
return nil, err
}
defer sess.Close()
return sess.CombinedOutput(b.Command)
}
func (b *SSHBuddy) parse(in []byte) (ssh.Signer, error) {
if b.KeyPassphrase == "" {
return ssh.ParsePrivateKey(in)
}
return ssh.ParsePrivateKeyWithPassphrase(in, []byte(b.KeyPassphrase))
}