-
Notifications
You must be signed in to change notification settings - Fork 9
/
cssh.go
128 lines (117 loc) · 2.78 KB
/
cssh.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
package main
import (
"bufio"
"flag"
"fmt"
"io/ioutil"
"log"
"os"
"strings"
"sync"
"time"
"github.com/42wim/cssh/device"
)
var (
flagHost, flagCmd, flagCmdfrom, flagHostfrom, flagLogdir string
flagPipe, flagVersion bool
wg sync.WaitGroup
cfg config
version = "v0.3.0"
)
func init() {
flag.StringVar(&flagHost, "host", "", "host")
flag.StringVar(&flagCmd, "cmd", "", "single command to execute")
flag.StringVar(&flagCmdfrom, "cmd-from", "", "file containing commands to execute")
flag.StringVar(&flagHostfrom, "host-from", "", "file containing hosts")
flag.StringVar(&flagLogdir, "logdir", "", "directory to log output")
flag.BoolVar(&flagPipe, "pipe", false, "pipe to stdin")
flag.BoolVar(&flagVersion, "version", false, "show version")
flag.Parse()
if flagVersion {
fmt.Println("Version:", version)
return
}
if flagHost == "" && flagHostfrom == "" {
flag.PrintDefaults()
return
}
cfg = readConfig("cssh.conf")
}
func doHost(host string, cmds []string) {
defer wg.Done()
cred := getCredentials(host)
if cred.Username == "" {
log.Println("couldn't get credentials for " + host)
return
}
d := &device.CiscoDevice{Hostname: host, Username: cred.Username, Password: cred.Password, Enable: cred.Enable, Logdir: flagLogdir}
err := d.Connect()
if err != nil {
log.Println(err)
return
}
defer d.Close()
for _, cmd := range cmds {
output, err := d.Cmd(cmd)
if err != nil {
log.Fatal(err)
}
fmt.Print(output)
}
if flagPipe {
scanner := bufio.NewScanner(os.Stdin)
d.Echo = false
d.EnableLog = true
d.Cmd("")
if d == nil {
log.Println("something went wrong in doPipe(" + host + ")")
return
}
for scanner.Scan() {
cmd := scanner.Text()
output, err := d.Cmd(cmd)
if err != nil {
log.Fatal(err)
}
fmt.Print(output)
}
}
}
func parseFile(name string) []string {
lines := []string{}
dat, _ := ioutil.ReadFile(name)
for _, line := range strings.Split(string(dat), "\n") {
if line != "" {
if strings.Contains(line, "#") {
} else {
lines = append(lines, line)
}
}
}
return lines
}
func parseFlags(filename string, stdin string) []string {
lines := []string{}
if filename != "" {
lines = parseFile(filename)
}
if stdin != "" {
lines = append(lines, stdin)
}
return lines
}
func main() {
hosts := strings.Fields(flagHost)
if len(hosts) > 1 {
hosts = append(hosts, parseFlags(flagHostfrom, "")...)
} else {
hosts = parseFlags(flagHostfrom, flagHost)
}
cmds := parseFlags(flagCmdfrom, flagCmd)
wg.Add(len(hosts))
for _, host := range hosts {
time.Sleep(time.Millisecond * 50)
go doHost(host, cmds)
}
wg.Wait()
}