forked from crosbymichael/slex
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
190 lines (175 loc) · 4.38 KB
/
main.go
File metadata and controls
190 lines (175 loc) · 4.38 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
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
package main
import (
"bufio"
"net"
"os"
"path/filepath"
"strings"
"sync"
log "github.com/Sirupsen/logrus"
"github.com/codegangsta/cli"
)
// preload initializes any global options and configuration
// before the main or sub commands are run
func preload(context *cli.Context) error {
if context.GlobalBool("debug") {
log.SetLevel(log.DebugLevel)
}
return nil
}
// hostHosts returns a list of host addresses that are specified on the
// command line and also in a hosts file separated by new lines.
func loadHosts(context *cli.Context) ([]string, error) {
hosts := []string(context.GlobalStringSlice("host"))
if hostsFile := context.GlobalString("hosts"); hostsFile != "" {
f, err := os.Open(hostsFile)
if err != nil {
return nil, err
}
defer f.Close()
s := bufio.NewScanner(f)
for s.Scan() {
if err := s.Err(); err != nil {
return nil, err
}
hosts = append(hosts, s.Text())
}
}
return hosts, nil
}
// multiplexAction uses the arguments passed via the command line and
// multiplexes them across multiple SSH connections
func multiplexAction(context *cli.Context) {
c, err := newCommand(context)
if err != nil {
log.Fatal(err)
}
log.Debug(c)
hosts, err := loadHosts(context)
if err != nil {
log.Fatal(err)
}
sections, err := parseSshConfigFile(filepath.Join(os.Getenv("HOME"), ".ssh", "config"))
if err != nil {
log.Fatal(err)
}
if len(hosts) == 0 {
log.Fatal("no host specified for command to run")
}
log.Debugf("hosts %v", hosts)
group := &sync.WaitGroup{}
for _, h := range hosts {
group.Add(1)
go executeCommand(c, h, sections[h], context.GlobalBool("A"), context.GlobalBool("quiet"), group)
}
group.Wait()
log.Debugf("finished executing %s on all hosts", c)
}
func executeCommand(c command, host string, section *SshConfigFileSection, agentForwarding, quiet bool, group *sync.WaitGroup) {
defer group.Done()
var (
err error
originalHost = host
)
if host, err = cleanHost(host); err != nil {
log.WithField("host", originalHost).Error(err)
return
}
if err = runSSH(c, host, section, agentForwarding, quiet); err != nil {
log.WithField("host", host).Error(err)
return
}
}
// runSSH executes the given command on the given host
func runSSH(c command, host string, section *SshConfigFileSection, agentForwarding, quiet bool) error {
config, err := newSshClientConfig(host, section, c.User, c.Identity, agentForwarding)
if err != nil {
return err
}
session, err := config.NewSession(config.host)
if err != nil {
return err
}
if !quiet {
w := newBufCloser(os.Stdout)
defer w.Close()
session.Stderr, session.Stdout = w, w
}
defer func() {
session.Close()
log.Printf("Session complete from %s", host)
}()
for key, value := range c.Env {
if err := session.Setenv(key, value); err != nil {
return err
}
}
return session.Run(c.Cmd)
}
// cleanHost parses out the hostname/ip and port. If no port is
// specified then port 22 is appended to the hostname/ip
func cleanHost(host string) (string, error) {
h, port, err := net.SplitHostPort(host)
if err != nil {
if !strings.Contains(err.Error(), "missing port in address") {
return "", err
}
port = "22"
h = host
}
if port == "" {
port = "22"
}
return net.JoinHostPort(h, port), nil
}
func main() {
app := cli.NewApp()
app.Name = "slex"
app.Usage = "SSH commands multiplexed"
app.Version = "1"
app.Author = "@crosbymichael"
app.Email = "crosbymichael@gmail.com"
app.Before = preload
app.Flags = []cli.Flag{
cli.BoolFlag{
Name: "debug",
Usage: "enable debug output for the logs",
},
cli.StringSliceFlag{
Name: "host",
Value: &cli.StringSlice{},
Usage: "SSH host address",
},
cli.StringFlag{
Name: "hosts",
Usage: "file containing host addresses separated by a new line",
},
cli.StringFlag{
Name: "user,u",
Value: "root",
Usage: "user to execute the command as",
},
cli.StringFlag{
Name: "identity,i",
Value: "id_rsa",
Usage: "SSH identity to use for connecting to the host",
},
cli.BoolFlag{
Name: "agent,A",
Usage: "Forward authentication request to the ssh agent",
},
cli.StringSliceFlag{
Name: "env,e",
Usage: "set environment variables for SSH command",
Value: &cli.StringSlice{},
},
cli.BoolFlag{
Name: "quiet,q",
Usage: "disable output from the ssh command",
},
}
app.Action = multiplexAction
if err := app.Run(os.Args); err != nil {
log.Fatal(err)
}
}