forked from JeffreySE/ssgo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
executable file
·477 lines (463 loc) · 17.4 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
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
package main
import (
"fmt"
"github.com/JeffreySE/ssgo/utils"
"github.com/go-ini/ini"
"gopkg.in/alecthomas/kingpin.v2"
"os"
"strings"
"time"
)
var (
app = kingpin.New("ssgo", "A SSH-based command line tool for operating remote hosts.")
_ = app.HelpFlag.Short('h')
example = app.Flag("example", "Show examples of ssgo's command.").Short('e').Default("false").Bool()
inventory = app.Flag("inventory", "For advanced use case, you can specify a host warehouse .ini file (Default is 'config.ini' file in current directory.)").Short('i').ExistingFile()
group = app.Flag("group", "Remote host group name in the inventory file, which must be used with '-i' or '--inventory' argument!").Short('g').String()
hostFile = app.Flag("host-file", "A file contains remote host or host range IP Address.(e.g. 'hosts.example.txt' in current directory.)").ExistingFile()
hostList = app.Flag("host-list", "Remote host or host range IP Address. e.g. 192.168.10.100,192.168.10.101-192.168.10.103,192.168.20.100/28,192.168.30.11-15").String()
password = app.Flag("pass", "The SSH login password for remote hosts.").Short('p').String()
user = app.Flag("user", "The SSH login user for remote hosts. default is 'root'").Short('u').Default("root").String()
port = app.Flag("port", "The SSH login port for remote hosts. default is '22'").Short('P').Default("22").Int()
//timeout = app.Flag("timeout", "Set ssh connection timeout.").Short('t').Default("10s").Duration()
maxExecuteNum = app.Flag("maxExecuteNum", "Set Maximum concurrent count of hosts.").Short('n').Default("20").Int()
output = app.Flag("output", "Output result'log to a file.(Be default if your input is \"log\",ssgo will output logs like \"ssgo-%s.log\")").Short('o').String()
formatMode = app.Flag("format", "For pretty look in terminal,you can format the result with table,simple,json or other style.(Default is simple)").Short('F').Default("simple").String()
jsonRaw = app.Flag("json-raw", "By default, the json data will be formatted and output by the console. You can specify the --json-raw parameter to output raw json data.(Default is false)").Default("false").Bool()
maxTableCellWidth = app.Flag("maxTableCellWidth", "For pretty look,you can set the printed table's max cell width in terminal.(Default is 40)").Short('w').Default("40").Int()
list = app.Command("list", "List available remote hosts from your input. ")
run = app.Command("run", "Run commands on remote hosts.")
scriptFile = run.Flag("script", "Want execute script on remote hosts ? Just specify the path of your script.").PlaceHolder("shell-script.sh").Short('s').ExistingFile()
scriptArgs = run.Flag("args", "Shell script arguments,use this flag with --script if you need.").Short('a').Default("").String()
cmdArgs = run.Flag("cmd", "Specify the commands or command file you want execute on remote hosts. By default will run 'echo pong' command if nothing is specified!").Short('c').Default("").String()
sshCopy = app.Command("copy", "Transfer files between local machine and remote hosts.")
copyAction = sshCopy.Flag("action", "ssgo's copy command do upload or download operations(only accept \"upload\" or \"download\" action)").Required().Short('a').String()
sourcePath = sshCopy.Flag("src", "Source file or directory path on the local machine or remote hosts").Short('s').Required().String()
destinationPath = sshCopy.Flag("dst", "Destination file or directory path on the remote host or local machine.").Short('d').Default("").String()
)
var (
allResultLogs []utils.ResultLogs
)
func main() {
app.Version("1.0.2")
app.VersionFlag.Short('v')
switch kingpin.MustParse(app.Parse(os.Args[1:])) {
case list.FullCommand():
if *example != false {
utils.ShowListCommandUsage()
} else if *inventory != "" && *group != "" {
cfg, err := utils.Cfg(*inventory)
if err != nil {
utils.ColorPrint("ERROR", ">>>", "ERROR: ", err, "\n")
}
if *group == "all" {
//get all hosts in config.ini file
for _, s := range cfg.Sections() {
if s.Name() == "DEFAULT" {
continue
}
listCommandAction(s)
}
} else {
s, _ := cfg.GetSection(*group)
listCommandAction(s)
}
return
} else if *hostFile != "" {
hosts, err := utils.GetAvailableIPFromFile(*hostFile)
if err != nil {
fmt.Errorf("ERROR: %s", err)
}
utils.PrintListHosts(hosts, *maxTableCellWidth)
return
} else if *hostList != "" {
hosts, err := utils.GetAvailableIP(*hostList)
if err != nil {
utils.ColorPrint("ERROR", "", "ERROR:", err, "\n")
}
utils.PrintListHosts(hosts, *maxTableCellWidth)
return
} else {
utils.ShowListCommandUsage()
}
case run.FullCommand():
if *example != false {
utils.ShowRunCommandUsage()
} else if *inventory != "" && *group != "" {
var hosts []string
cfg, err := utils.Cfg(*inventory)
if err != nil {
utils.ColorPrint("ERROR", ">>>", "ERROR: ", err, "\n")
}
if *group == "all" {
//get all hosts in config.ini file
isFinished := false
for index, s := range cfg.Sections() {
if s.Name() == "DEFAULT" {
continue
}
if s.HasKey("hosts") {
h, _ := s.GetKey("hosts")
resHosts, err := utils.GetAvailableIPFromMultiLines(h.String())
if err != nil {
utils.ColorPrint("ERROR", ">>>", "ERROR: ", err, "\n")
return
}
hosts = resHosts
userName := s.Key("user").String()
password := s.Key("pass").String()
port := s.Key("port").MustInt()
cmds, err := checkCommandArgs()
if err != nil {
utils.ColorPrint("ERROR", "", "ERROR:", err, "\n")
return
}
if *formatMode != "json" {
utils.ColorPrint("INFO", ">>> Group Name: ", "["+s.Name()+"]\n")
}
if index == len(cfg.Sections())-1 {
isFinished = true
}
if *scriptFile != "" {
doSSHCommands(userName, password, fmt.Sprintf("from hostgroup %s@%s file", s.Name(), *inventory), "", port, hosts, []string{}, *scriptFile, *scriptArgs, "script", isFinished)
}
if *cmdArgs != "" {
doSSHCommands(userName, password, fmt.Sprintf("from hostgroup %s@%s file", s.Name(), *inventory), "", port, hosts, cmds, "", "", "cmd", isFinished)
}
}
}
return
} else {
s, _ := cfg.GetSection(*group)
if s.HasKey("hosts") {
h, _ := s.GetKey("hosts")
resHosts, err := utils.GetAvailableIPFromMultiLines(h.String())
if err != nil {
utils.ColorPrint("ERROR", ">>>", "ERROR: ", err, "\n")
return
}
hosts = resHosts
userName := s.Key("user").String()
password := s.Key("pass").String()
port := s.Key("port").MustInt()
cmds, err := checkCommandArgs()
if err != nil {
utils.ColorPrint("ERROR", "", "ERROR:", err, "\n")
return
}
if *formatMode != "json" {
utils.ColorPrint("INFO", ">>> Group Name: ", "["+s.Name()+"]\n")
}
if *scriptFile != "" {
doSSHCommands(userName, password, fmt.Sprintf("from hostgroup %s@%s file", s.Name(), *inventory), "", port, hosts, []string{}, *scriptFile, *scriptArgs, "script", true)
}
if *cmdArgs != "" {
doSSHCommands(userName, password, fmt.Sprintf("from hostgroup %s@%s file", s.Name(), *inventory), "", port, hosts, cmds, "", "", "cmd", true)
}
}
}
return
} else if *hostFile != "" {
hosts, err := utils.GetAvailableIPFromFile(*hostFile)
if err != nil {
utils.ColorPrint("ERROR", "", "ERROR:", err, "\n")
return
}
cmds, err := checkCommandArgs()
if err != nil {
utils.ColorPrint("ERROR", "", "ERROR:", err, "\n")
return
}
if *scriptFile != "" {
doSSHCommands(*user, *password, fmt.Sprintf("from file (%s)", *hostFile), "", *port, hosts, []string{}, *scriptFile, *scriptArgs, "script", true)
return
}
if *cmdArgs != "" {
doSSHCommands(*user, *password, fmt.Sprintf("from file (%s)", *hostFile), "", *port, hosts, cmds, "", "", "cmd", true)
}
return
} else if *hostList != "" {
hosts, err := utils.GetAvailableIP(*hostList)
if err != nil {
utils.ColorPrint("ERROR", "", "ERROR:", err, "\n")
return
}
cmds, err := checkCommandArgs()
if err != nil {
utils.ColorPrint("ERROR", "", "ERROR:", err, "\n")
return
}
if *scriptFile != "" {
doSSHCommands(*user, *password, fmt.Sprintf("from list (%s)", *hostList), "", *port, hosts, []string{}, *scriptFile, *scriptArgs, "script", true)
return
}
if *cmdArgs != "" {
doSSHCommands(*user, *password, fmt.Sprintf("from list (%s)", *hostList), "", *port, hosts, cmds, "", "", "cmd", true)
}
return
} else {
utils.ShowRunCommandUsage()
}
case sshCopy.FullCommand():
if *example != false {
utils.ShowFileTransferUsage()
} else if *inventory != "" && *group != "" {
var hosts []string
cfg, err := utils.Cfg(*inventory)
if err != nil {
utils.ColorPrint("ERROR", ">>>", "ERROR: ", err, "\n")
return
}
if *group == "all" {
//get all hosts in config.ini file
isFinished := false
for index, s := range cfg.Sections() {
if s.Name() == "DEFAULT" {
continue
}
if s.HasKey("hosts") {
h, _ := s.GetKey("hosts")
resHosts, err := utils.GetAvailableIPFromMultiLines(h.String())
if err != nil {
utils.ColorPrint("ERROR", ">>>", "ERROR: ", err, "\n")
}
hosts = resHosts
userName := s.Key("user").String()
password := s.Key("pass").String()
port := s.Key("port").MustInt()
utils.ColorPrint("INFO", ">>> Group Name: ", "["+s.Name()+"]\n")
if index == len(cfg.Sections())-1 {
isFinished = true
}
if *copyAction == "upload" {
doSFTPFileTransfer(userName, password, s.Name(), "", port, hosts, *sourcePath, *destinationPath, "upload", isFinished)
} else if *copyAction == "download" {
doSFTPFileTransfer(userName, password, s.Name(), "", port, hosts, *sourcePath, *destinationPath, "download", isFinished)
} else {
utils.ShowFileTransferUsage()
}
}
}
} else {
s, _ := cfg.GetSection(*group)
if s.HasKey("hosts") {
h, _ := s.GetKey("hosts")
resHosts, err := utils.GetAvailableIPFromMultiLines(h.String())
if err != nil {
utils.ColorPrint("ERROR", ">>>", "ERROR: ", err, "\n")
return
}
hosts = resHosts
userName := s.Key("user").String()
password := s.Key("pass").String()
port := s.Key("port").MustInt()
if *copyAction == "upload" {
doSFTPFileTransfer(userName, password, s.Name(), "", port, hosts, *sourcePath, *destinationPath, "upload", true)
} else if *copyAction == "download" {
doSFTPFileTransfer(userName, password, s.Name(), "", port, hosts, *sourcePath, *destinationPath, "download", true)
} else {
utils.ShowFileTransferUsage()
}
}
}
return
} else if *hostFile != "" {
hosts, err := utils.GetAvailableIPFromFile(*hostFile)
if err != nil {
utils.ColorPrint("ERROR", "", "ERROR:", err, "\n")
return
}
if *copyAction == "upload" {
doSFTPFileTransfer(*user, *password, "from-file", "", *port, hosts, *sourcePath, *destinationPath, "upload", true)
} else if *copyAction == "download" {
doSFTPFileTransfer(*user, *password, "from-file", "", *port, hosts, *sourcePath, *destinationPath, "download", true)
} else {
utils.ShowFileTransferUsage()
}
return
} else if *hostList != "" {
hosts, err := utils.GetAvailableIP(*hostList)
if err != nil {
utils.ColorPrint("ERROR", "", "ERROR:", err, "\n")
return
}
if *copyAction == "upload" {
doSFTPFileTransfer(*user, *password, "from-list", "", *port, hosts, *sourcePath, *destinationPath, "upload", true)
} else if *copyAction == "download" {
doSFTPFileTransfer(*user, *password, "from-list", "", *port, hosts, *sourcePath, *destinationPath, "download", true)
} else {
utils.ShowFileTransferUsage()
}
return
} else {
utils.ShowFileTransferUsage()
}
}
}
func listCommandAction(sec *ini.Section) {
utils.ColorPrint("INFO", "", ">>> Group Name: ", "["+sec.Name()+"]\n")
if sec.HasKey("hosts") {
h, _ := sec.GetKey("hosts")
utils.ColorPrint("INFO", "", ">>> Hosts From: ", h.String(), "\n")
hosts, err := utils.GetAvailableIPFromMultiLines(h.String())
if err != nil {
fmt.Errorf("ERROR: %s", err)
return
}
utils.PrintListHosts(hosts, *maxTableCellWidth, sec.Name())
}
}
func checkCommandArgs() ([]string, error) {
var cmds []string
if *cmdArgs != "" {
strPath, err := utils.GetRealPath(*cmdArgs)
if err != nil {
// not real path
if !strings.Contains(*cmdArgs, ";") {
cmds = append(cmds, *cmdArgs)
}
cmds = strings.Split(*cmdArgs, ";")
} else {
// cmd file path, just get the commands content
cmdFileContent, err := utils.GetFileContent(strPath)
cmds = cmdFileContent
if err != nil {
fmt.Println("ERROR:", err)
return cmds, err
}
}
}
return cmds, nil
}
func doSSHCommands(user, password, hostGroupName, key string, port int, todoHosts, cmds []string, scriptFilePath, scriptArgs, action string, isFinished bool) {
var resultLog utils.ResultLogs
if len(cmds) == 0 {
cmds = append(cmds, "echo pong")
}
todoHosts, err := utils.DuplicateIPAddressCheck(todoHosts)
if err != nil {
fmt.Println(err)
return
}
pool := utils.NewPool(*maxExecuteNum, len(todoHosts))
startTime := time.Now()
resultLog.StartTime = startTime.Format("2006-01-02 15:04:05")
resultLog.HostGroup = hostGroupName
switch *formatMode {
case "simple", "table":
utils.ColorPrint("INFO", "", "Tips:", fmt.Sprintf("Process running start: %s\n", resultLog.StartTime))
}
if *output != "" {
utils.WriteAndAppendFile(*output, fmt.Sprintf("Tips: process running start: %s", resultLog.StartTime))
}
chres := make([]chan interface{}, len(todoHosts))
for i, host := range todoHosts {
chres[i] = make(chan interface{}, 1)
go func(h string, a string, chr chan interface{}) {
pool.AddOne()
switch a {
case "script":
utils.SSHRunShellScript(user, password, h, key, scriptFilePath, scriptArgs, port, chr)
case "cmd":
utils.DoSSHRunFast(user, password, h, key, cmds, port, chr)
}
pool.DelOne()
}(host, action, chres[i])
if *formatMode == "simple" || *output != "" {
res := <-chres[i]
if res.(utils.SSHResult).Status == "failed" {
resultLog.ErrorHosts = append(resultLog.ErrorHosts, res)
} else {
resultLog.SuccessHosts = append(resultLog.SuccessHosts, res)
}
utils.FormatResultWithBasicStyle(i, res.(utils.SSHResult))
if *output != "" {
utils.LogSSHResultToFile(i, res.(utils.SSHResult), *output)
}
}
}
switch *formatMode {
case "simple":
utils.FormatResultLogWithSimpleStyle(resultLog, startTime, *maxTableCellWidth, []string{"Result"})
case "table":
utils.FormatResultLogWithTableStyle(chres, resultLog, startTime, *maxTableCellWidth)
case "json":
if *inventory != "" && *group == "all" {
log := utils.GetAllResultLog(chres, resultLog, startTime)
allResultLogs = append(allResultLogs, log)
if isFinished {
utils.FormatResultToJson(allResultLogs, *jsonRaw)
}
} else {
utils.FormatResultLogWithJsonStyle(chres, resultLog, startTime, *jsonRaw)
}
}
if *output != "" {
utils.ResultLogInfo(resultLog, startTime, true, *output)
}
pool.Wg.Wait()
}
func doSFTPFileTransfer(user, password, hostGroupName, key string, port int, todoHosts []string, sourcePath, destinationPath, action string, isFinished bool) {
var resultLog utils.ResultLogs
todoHosts, err := utils.DuplicateIPAddressCheck(todoHosts)
if err != nil {
fmt.Println(err)
return
}
pool := utils.NewPool(*maxExecuteNum, len(todoHosts))
startTime := time.Now()
resultLog.StartTime = startTime.Format("2006-01-02 15:04:05")
resultLog.HostGroup = hostGroupName
if *formatMode == "simple" || *formatMode == "table" {
utils.ColorPrint("INFO", "", "Tips:", fmt.Sprintf("Process running start: %s\n", resultLog.StartTime))
}
if *output != "" {
utils.WriteAndAppendFile(*output, fmt.Sprintf("Tips: process running start: %s", resultLog.StartTime))
}
chres := make([]chan interface{}, len(todoHosts))
for i, host := range todoHosts {
chres[i] = make(chan interface{}, 1)
go func(h string, a, s, d string, chr chan interface{}) {
pool.AddOne()
switch a {
case "upload":
utils.SFTPUpload(user, password, h, key, port, s, d, chr)
case "download":
utils.SFTPDownload(user, password, h, key, port, s, d, chr)
}
pool.DelOne()
}(host, action, sourcePath, destinationPath, chres[i])
if *formatMode == "simple" || *output != "" {
res := <-chres[i]
if res.(utils.SFTPResult).Status == "failed" {
resultLog.ErrorHosts = append(resultLog.ErrorHosts, res)
} else {
resultLog.SuccessHosts = append(resultLog.SuccessHosts, res)
}
utils.SFTPFormatResultWithBasicStyle(i, res.(utils.SFTPResult))
if *output != "" {
utils.LogSFTPResultToFile(i, res.(utils.SFTPResult), *output)
}
}
}
switch *formatMode {
case "simple":
utils.FormatResultLogWithSimpleStyle(resultLog, startTime, *maxTableCellWidth, []string{})
case "table":
utils.FormatResultLogWithTableStyle(chres, resultLog, startTime, *maxTableCellWidth)
case "json":
if *inventory != "" && *group == "all" {
log := utils.GetAllResultLog(chres, resultLog, startTime)
allResultLogs = append(allResultLogs, log)
if isFinished {
utils.FormatResultToJson(allResultLogs, *jsonRaw)
}
} else {
utils.FormatResultLogWithJsonStyle(chres, resultLog, startTime, *jsonRaw)
}
}
if *output != "" {
utils.ResultLogInfo(resultLog, startTime, true, *output)
}
pool.Wg.Wait()
}