-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
155 lines (128 loc) · 2.79 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
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
151
152
153
154
155
package main
import (
"bytes"
"encoding/json"
"flag"
"fmt"
"golang.org/x/crypto/ssh"
"io/ioutil"
"log"
"os/user"
"strings"
)
var (
username string
password string
hostnames string
keyfile string
envstr string
envs []string
hosts []string
verbose bool
jsonOutput bool
config *ssh.ClientConfig
)
type Output struct {
Hostname string `json:"hostname"`
Output string `json:"output"`
}
func getKeyAuth() (key ssh.Signer) {
buf, err := ioutil.ReadFile(keyfile)
if err != nil {
log.Fatal(err)
}
key, err = ssh.ParsePrivateKey(buf)
if err != nil {
log.Fatal(err)
}
return
}
func runOnHosts(cmd string) {
results := make(chan Output, len(hosts))
for _, hostname := range hosts {
go func(h string) {
results <- executeCmd(cmd, h)
}(hostname)
}
var outs []Output
for i := 0; i < len(hosts); i++ {
select {
case res := <-results:
outs = append(outs, res)
}
}
if jsonOutput {
o, err := json.Marshal(outs)
if err != nil {
panic(err)
}
fmt.Print(string(o))
} else {
for i := 0; i < len(outs); i++ {
output := outs[i].Output
if verbose {
output = outs[i].Hostname + ":\n" + output
}
fmt.Print(output)
}
}
}
func executeCmd(cmd, hostname string) Output {
conn, err1 := ssh.Dial("tcp", hostname+":22", config)
if err1 != nil {
log.Fatal(err1)
}
session, err2 := conn.NewSession()
if err2 != nil {
log.Fatal(err2)
}
defer session.Close()
for _, e := range envs {
envKeyVal := strings.Split(e, "=")
session.Setenv(envKeyVal[0], envKeyVal[1])
}
var stdoutBuf bytes.Buffer
session.Stdout = &stdoutBuf
session.Run(cmd)
return Output{
Hostname: hostname,
Output: stdoutBuf.String(),
}
}
func init() {
user, err := user.Current()
if err != nil {
log.Fatal(err)
}
flag.StringVar(&username, "u", user.Username, "The username of the machines")
flag.StringVar(&password, "p", "", "The password of the machines")
flag.StringVar(&hostnames, "h", "", "The hosts separated by a comma. Ex. host1,host2,host3")
flag.StringVar(&keyfile, "k", "", "The public key to connect to the servers with")
flag.StringVar(&envstr, "e", "", "Environment variables separate by space. Ex. FOO=bar BAR=foo")
flag.BoolVar(&verbose, "v", false, "Show the server name in the output.")
flag.BoolVar(&jsonOutput, "j", false, "Show the output as json. server_name => \"output\"")
}
func main() {
flag.Parse()
hosts = strings.Split(hostnames, ",")
if envstr != "" {
envs = strings.Split(envstr, " ")
}
switch {
case password != "":
config = &ssh.ClientConfig{
User: username,
Auth: []ssh.AuthMethod{
ssh.Password(password),
},
}
case keyfile != "":
config = &ssh.ClientConfig{
User: username,
Auth: []ssh.AuthMethod{
ssh.PublicKeys(getKeyAuth()),
},
}
}
runOnHosts(flag.Arg(0))
}