/
shelly.go
574 lines (499 loc) · 12.6 KB
/
shelly.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
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
package fstools
import (
"bufio"
"bytes"
"errors"
"fmt"
"io"
"io/ioutil"
"log"
"os"
"os/exec"
"path/filepath"
"strconv"
"strings"
"sync"
"time"
"golang.org/x/crypto/ssh"
)
const private_ssh_key_default = "%s/.ssh/id_rsa"
var ssh_key = ""
func GetPrivateSSHKey() string {
if strings.EqualFold(ssh_key, "") {
ssh_key = fmt.Sprintf(private_ssh_key_default, os.Getenv("HOME"))
}
return ssh_key
}
func SetPrivateSSHKey(newkey string) {
ssh_key = newkey
}
func Touch(fileName string) error {
_, err := os.Stat(fileName)
if os.IsNotExist(err) {
file, err := os.Create(fileName)
defer file.Close()
return err
} else {
currentTime := time.Now().Local()
return os.Chtimes(fileName, currentTime, currentTime)
}
}
func System3(cmd string) (err error) {
arglist := strings.Split(cmd, " ")
var b bytes.Buffer
err = Popen3(&b,
exec.Command(arglist[0], arglist[1:]...),
)
io.Copy(os.Stdout, &b)
return
}
//was "readlines"
func Grep(path string, musthave string, mustnothave string) ([]string, error) {
file, err := os.Open(path)
if err != nil {
return nil, err
}
defer file.Close()
var lines []string
scanner := bufio.NewScanner(file)
for scanner.Scan() {
if (musthave == "" || strings.Contains(scanner.Text(), musthave)) &&
(mustnothave == "" || !strings.Contains(scanner.Text(), mustnothave)) {
lines = append(lines, scanner.Text())
}
}
return lines, scanner.Err()
}
func ReadFileToSlice(filename string, force bool) ([]string, error) {
if b, err := FileExists(filename); err != nil || !b {
if err != nil && !force {
//file existance test fails entirely ( doesn't mean file doesn't exist )
return make([]string, 0), err
}
if !b && !force {
//file does not exist, but we want to throw an error indicating as such
return make([]string, 0), errors.New("file not found")
}
//ignore the fact that the file doesn't exist and return an empty array, quietly
return make([]string, 0), nil
}
file, err := os.Open(filename)
if err != nil {
return nil, err
}
defer file.Close()
var lines []string
scanner := bufio.NewScanner(file)
for scanner.Scan() {
lines = append(lines, scanner.Text())
}
return lines, scanner.Err()
}
func WriteSliceToFile(filename string, content []string) error {
return WriteStringToFile(filename, strings.Join(content, "\n"))
}
func WriteStringToFile(filename string, content string) error {
f, err := os.OpenFile(filename, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0600)
if err != nil {
panic(err)
}
defer f.Close()
if _, err = f.WriteString(content); err != nil {
panic(err)
}
return err
}
//formerly Execute
func Popen3(output_buffer *bytes.Buffer, stack ...*exec.Cmd) (err error) {
var error_buffer bytes.Buffer
pipe_stack := make([]*io.PipeWriter, len(stack)-1)
i := 0
for ; i < len(stack)-1; i++ {
stdin_pipe, stdout_pipe := io.Pipe()
stack[i].Stdout = stdout_pipe
stack[i].Stderr = &error_buffer
stack[i+1].Stdin = stdin_pipe
pipe_stack[i] = stdout_pipe
}
stack[i].Stdout = output_buffer
stack[i].Stderr = &error_buffer
// if err := call(stack, pipe_stack); err != nil {
// log.Fatalln(string(error_buffer.Bytes()), err)
// }
// return err
return call(stack, pipe_stack)
}
func call(stack []*exec.Cmd, pipes []*io.PipeWriter) (err error) {
if stack[0].Process == nil {
if err = stack[0].Start(); err != nil {
return err
}
}
if len(stack) > 1 {
if err = stack[1].Start(); err != nil {
return err
}
defer func() {
if err == nil {
pipes[0].Close()
err = call(stack[1:], pipes[1:])
}
}()
}
return stack[0].Wait()
}
func FileExists(filename string) (bool, error) {
_, err := os.Stat(filename)
if err == nil {
return true, nil
}
if errors.Is(err, os.ErrNotExist) {
return false, nil
}
return false, err
}
func PopentoString(cmd string) (string, error) {
arglist := strings.Split(cmd, " ")
//app:=arglist[0]
var b bytes.Buffer
if err := Popen3(&b,
exec.Command(arglist[0], arglist[1:]...),
exec.Command("head", "-1"),
); err != nil {
log.Fatalln(err)
}
return b.String(), nil
}
func PopentoStringAwk(cmd string, awk int) (string, error) {
arglist := strings.Split(cmd, " ")
//app:=arglist[0]
awkstatement := fmt.Sprintf("{print $%d}'", awk)
var b bytes.Buffer
if err := Popen3(&b,
exec.Command(arglist[0], arglist[1:]...),
exec.Command("head", "-1"),
exec.Command("awk", awkstatement),
); err != nil {
log.Fatalln(err)
}
return b.String(), nil
}
func Popen3GrepFast(cmd string, musthave string, mustnothave string) ([]string, error) {
var b bytes.Buffer
arglist := strings.Split(cmd, " ")
//check for len<2 and return null array
var greplist []string
if len(musthave) != 0 {
greplist = strings.Split(musthave, "&")
}
var antigreplist []string
if len(mustnothave) != 0 {
antigreplist = strings.Split("-v "+mustnothave, "&")
}
//app:=arglist[0]
var err error
//case 1: popen and grep
if len(greplist) > 0 && len(antigreplist) == 0 {
if err = Popen3(&b,
exec.Command(arglist[0], arglist[1:]...),
exec.Command("grep", greplist[0:]...),
); err != nil {
log.Fatalln(err)
}
} else if len(greplist) == 0 && len(antigreplist) > 0 {
//case 2: popen and antigrep
if err = Popen3(&b,
exec.Command(arglist[0], arglist[1:]...),
exec.Command("grep", antigreplist[0:]...),
); err != nil {
log.Fatalln(err)
}
} else if len(greplist) > 0 && len(antigreplist) > 0 {
//case 3: popen, grep and antigrep
if err = Popen3(&b,
exec.Command(arglist[0], arglist[1:]...),
exec.Command("grep", greplist[0:]...),
exec.Command("grep", antigreplist[0:]...),
); err != nil {
log.Fatalln(err)
}
}
var bstring = b.String()
var slice []string
if len(strings.TrimSpace(bstring)) != 0 {
slice = strings.Split(b.String(), "\n")
}
if len(slice) < 2 {
return slice, nil
}
return slice[:len(slice)-1], nil
}
func Popen3Grep(cmd string, musthave string, mustnothave string) ([]string, error) {
var b bytes.Buffer
arglist := strings.Split(cmd, " ")
//check for len<2 and return null array
var slice []string
var greplist []string
if len(musthave) != 0 {
greplist = strings.Split(musthave, "&")
}
var antigreplist []string
if len(mustnothave) != 0 {
antigreplist = strings.Split(mustnothave, "&")
}
//app:=arglist[0]
var err error
if err = Popen3(&b, exec.Command(arglist[0], arglist[1:]...)); err != nil {
return slice, err
}
var bstring = b.String()
if len(strings.TrimSpace(bstring)) != 0 {
slice = strings.Split(b.String(), "\n")
}
newslice := make([]string, 0)
for i := 0; i < len(slice); i++ {
//case 1: there are neither musthaves nor mustnothaves
if len(strings.TrimSpace(slice[i])) != 0 {
if len(greplist) == 0 && len(antigreplist) == 0 {
//not really grep at all.. just expensive popen
newslice = append(newslice, slice[i])
} else if len(greplist) > 0 && len(antigreplist) == 0 {
// only look at musthaves
if SliceContains(greplist, slice[i]) {
newslice = append(newslice, slice[i])
}
} else if len(musthave) == 0 && len(antigreplist) > 0 {
if !SliceContains(antigreplist, slice[i]) {
newslice = append(newslice, slice[i])
}
} else {
if SliceContains(greplist, slice[i]) && !SliceContains(antigreplist, slice[i]) {
newslice = append(newslice, slice[i])
}
}
}
}
return newslice, nil
}
// func Popen3DoubleGrep(cmd string, musthave string) ([]string, error) {
// var b bytes.Buffer
// arglist := strings.Split(cmd, " ")
// //check for len<2 and return null array
// var greplist []string
// if len(musthave) != 0 {
// greplist = strings.Split(musthave, "&")
// }
// //app:=arglist[0]
// var err error
// //case 1: popen and grep
// if len(greplist) == 2 {
// if err = Popen3(&b,
// exec.Command(arglist[0], arglist[1:]...),
// exec.Command("grep", greplist[0]),
// exec.Command("grep", greplist[1]),
// ); err != nil {
// log.Fatalln(err)
// }
// } else {
// return make([]string, 0), &os.SyscallError{}
// }
// slice := strings.Split(b.String(), "\n")
// return slice[:len(slice)-1], nil
// }
func SSHPopenToString(hostname string, command string) (string, error) {
client, session, err := getsshclient(hostname)
if err != nil {
//panic(err)
return "", err
}
out, err := session.CombinedOutput(command)
if err != nil {
//panic(err)
return "", err
}
//fmt.Println(string(out))
client.Close()
return strings.TrimSpace(string(out)), nil
}
//write otuput content (including new lines) to file outputfile (including full absolute path) on remoteserver over SSH
func StringToFileOverSSH(outputContent string, remoteserver string, outputfile string) error {
client, session, err := getsshclient(remoteserver)
defer client.Close()
if err != nil {
return err
}
var stdin io.WriteCloser
stdin, err = session.StdinPipe()
if err != nil {
log.Fatalf("Unable to setup stdin for session: %v", err)
}
go func() {
defer stdin.Close()
io.Copy(stdin, bytes.NewReader([]byte(outputContent+"\n")))
}()
if err = session.Run("cat >" + outputfile); err != nil {
return err
}
return nil
}
func getsshclient(host string) (*ssh.Client, *ssh.Session, error) {
key, err := ioutil.ReadFile(GetPrivateSSHKey())
if err != nil {
log.Fatalf("unable to read private key: %v", err)
}
// Create the Signer for this private key.
signer, err := ssh.ParsePrivateKey(key)
if err != nil {
log.Fatalf("unable to parse private key: %v", err)
}
sshConfig := &ssh.ClientConfig{
User: os.Getenv("USER"),
Auth: []ssh.AuthMethod{
ssh.PublicKeys(signer),
},
}
sshConfig.SetDefaults()
sshConfig.HostKeyCallback = ssh.InsecureIgnoreHostKey()
client, err := ssh.Dial("tcp", host+":22", sshConfig)
if err != nil {
return nil, nil, err
}
session, err := client.NewSession()
if err != nil {
client.Close()
return nil, nil, err
}
return client, session, nil
}
func DmidecodeProduct() (string, error) {
var b bytes.Buffer
if err := Popen3(&b,
exec.Command("dmidecode"),
exec.Command("grep", "-i", "product"),
exec.Command("head", "-1"),
exec.Command("awk", "{print $3}"),
); err != nil {
log.Fatalln(err)
}
return b.String(), nil
}
func Copy(src, dst string) (int64, error) {
sourceFileStat, err := os.Stat(src)
if err != nil {
return 0, err
}
if !sourceFileStat.Mode().IsRegular() {
return 0, fmt.Errorf("%s is not a regular file", src)
}
source, err := os.Open(src)
if err != nil {
return 0, err
}
defer source.Close()
destination, err := os.Create(dst)
if err != nil {
return 0, err
}
defer destination.Close()
nBytes, err := io.Copy(destination, source)
return nBytes, err
}
func Move(src, dst string) error {
return os.Rename(src, dst)
}
func GetFirstFile(rootpath string, hint string) string {
var result string
err := filepath.Walk(rootpath, func(path string, info os.FileInfo, err error) error {
if info.IsDir() {
return nil
}
if strings.HasSuffix(path, hint) {
//VerbosePrintln("found: " + path)
result = path
}
return nil
})
if err != nil {
fmt.Printf("walk error [%v]\n", err)
}
return result
}
func ExecToFile(cli string, ofile string) (err error) {
err = nil
arglist := strings.Split(cli, " ")
var outfile *os.File
cmd := exec.Command(arglist[0], arglist[1:]...)
// open the out file for writing
if !strings.EqualFold(ofile, "") {
outfile, err = os.Create(ofile)
if err != nil {
panic(err)
}
defer outfile.Close()
cmd.Stdout = outfile
}
err = cmd.Start()
if err != nil {
return
}
err = cmd.Wait()
return
}
func Spinny(sigChan chan bool) {
quit := false
for !quit {
s := "|/-\\"
for i := 0; i < len(s); i++ {
fmt.Printf("%c", s[i])
time.Sleep(100 * time.Millisecond)
fmt.Printf("\b")
}
select {
// case msg1 := <-messages:
// //fmt.Println("received", msg1)
// quit = true
case sig := <-sigChan:
quit = sig //fmt.Println("received signal", sig)
default:
//fmt.Println("not yet")
}
}
fmt.Printf(" \b")
}
func System3AndSpin(cmd string, redirect string) (err error) {
//var errorRec error
var wg sync.WaitGroup
wg.Add(1)
//messages := make(chan string)
sigChan := make(chan bool)
errorChan := make(chan error)
go func() {
defer wg.Done()
defer close(errorChan)
defer close(sigChan)
e := ExecToFile(cmd, redirect)
sigChan <- true
errorChan <- e
}()
go Spinny(sigChan)
//errorRec = <-errorChan
err = <-errorChan
wg.Wait()
return err
}
func Error2ExitCode(err error) int {
//fmt.Printf("err was %q\n", err.Error())
if strings.HasPrefix(err.Error(), "exit status ") {
r, _ := strconv.ParseInt(err.Error()[12:], 10, 32)
return int(r)
}
return 255
}
func Hostname() (string, error) {
var hostname string
var err error
if hostname, err = PopentoString("hostname -s"); err != nil {
return "localhost", err
}
return strings.TrimSpace(hostname), nil
}