/
client.go
102 lines (88 loc) · 1.93 KB
/
client.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
package main
import (
"bytes"
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"time"
"golang.org/x/crypto/ssh"
)
var cmdsfile = flag.String("cmdsfile", "cmds.json", "json file with commands to execute")
type Cmd struct {
Host string
Command string
}
func ParseCommands(filename string) []*Cmd {
jsonBytes, err := ioutil.ReadFile(filename)
if err != nil {
panic(err)
}
cmds := make([]*Cmd, 0)
err = json.Unmarshal(jsonBytes, &cmds)
if err != nil {
panic(err)
}
return cmds
}
type SignerContainer struct {
signers []ssh.Signer
}
func (t *SignerContainer) Key(i int) (key ssh.PublicKey, err error) {
if i >= len(t.signers) {
return
}
key = t.signers[i].PublicKey()
return
}
func main() {
flag.Parse()
results := make(chan string, 10)
timeout := time.After(65 * time.Second)
cmds := ParseCommands(*cmdsfile)
for _, cmd := range cmds {
go func(command *Cmd) {
results <- executeCmd(command.Command, command.Host)
}(cmd)
}
for i := 0; i < len(cmds); i++ {
select {
case res := <-results:
fmt.Print(res)
case <-timeout:
fmt.Println("Timed out!")
return
}
}
}
func executeCmd(cmd, hostname string) string {
// return fmt.Sprintf("executing %v on %v\n", cmd, hostname)
config := &ssh.ClientConfig{
User: "bob",
Auth: []ssh.AuthMethod{ssh.Password("bob2")},
Config: ssh.Config{Ciphers: []string{"aes192-ctr"}},
}
fmt.Printf("executing %v on %v\n", cmd, hostname)
client, err := ssh.Dial("tcp", hostname, config)
if err != nil {
fmt.Println(err)
}
if client == nil {
fmt.Printf("Connection to %v failed\n", hostname)
return ""
}
session, err := client.NewSession()
if err != err {
panic(err)
}
defer session.Close()
var stdoutBuf bytes.Buffer
session.Stdout = &stdoutBuf
err = session.Start(cmd)
if err != nil {
fmt.Printf("Session failed with error: %v\n", err)
}
session.Wait()
fmt.Printf("DONE: %v on %v\n", cmd, hostname)
return hostname + ": " + stdoutBuf.String()
}