/
main.go
82 lines (67 loc) · 1.74 KB
/
main.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
package xssh2
import (
"bytes"
"io/ioutil"
"log"
"time"
"github.com/pkg/errors"
"golang.org/x/crypto/ssh"
)
// Client is the ssh client
type Client struct {
sshCfg *ssh.ClientConfig
}
// Config contains the ssh config
type Config struct {
User string
PrivateKeyFile string
}
// NewClient returns new ssh client
func NewClient(c *Config) (*Client, error) {
key, err := ioutil.ReadFile(c.PrivateKeyFile)
if err != nil {
return nil, errors.Errorf("unable to read private key: %v", err)
}
// Create the Signer for this private key.
signer, err := ssh.ParsePrivateKey(key)
if err != nil {
return nil, errors.Errorf("unable to parse private key: %v", err)
}
sshCfg := &ssh.ClientConfig{
User: c.User,
Auth: []ssh.AuthMethod{
ssh.PublicKeys(signer),
},
// I'm using InsecureIgnoreHostKey for re-used private IP(s)
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
Timeout: 10 * time.Second,
}
return &Client{
sshCfg: sshCfg,
}, nil
}
// Connect will open connection to the remote server and perform the SSH handshake.
func (c *Client) Connect(ip, port string) (*ssh.Client, error) {
sshClient, err := ssh.Dial("tcp", ip+":"+port, c.sshCfg)
if err != nil {
return nil, errors.Errorf("unable to connect: %v", err)
}
return sshClient, nil
}
// Run will run all commands
func (c *Client) Run(sshClient *ssh.Client, cmds []string) error {
var b bytes.Buffer
for i := range cmds {
log.Printf("Running cmd[%v]: [%v]\n", i, cmds[i])
session, err := sshClient.NewSession()
if err != nil {
return errors.Errorf("Failed to create session: %v", err)
}
defer session.Close()
session.Stdout = &b
if err := session.Run(cmds[i]); err != nil {
return errors.Errorf("Failed to run: %v", err)
}
}
return nil
}