-
Notifications
You must be signed in to change notification settings - Fork 2
/
shellmode.go
231 lines (203 loc) · 5.03 KB
/
shellmode.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
package main
/* this looks like a unique packages, but it actually belongs
to the main package and uses the following main global variables:
Hosts loaded and merged from the YAML-file:
selectedHosts
allHosts
Command prompt filter:
cliLabel
cliScriptFile
cliConfigFile
Sets a decision tree for the rl.guess function
cliCompleter
*/
import (
"fmt"
"io"
"sort"
"strconv"
"strings"
rl "github.com/chzyer/readline"
"github.com/ipcjk/mlxsh/libhost"
)
func filterInput(r rune) (rune, bool) {
switch r {
case rl.CharCtrlZ:
return r, false
}
return r, true
}
func printHosts(x []libhost.HostConfig) {
for _, v := range x {
fmt.Printf("\x1b[32m%s\x1b[0m", v.Hostname)
/* read and sort labels */
labels := make([]string, 0)
for k := range v.Labels {
labels = append(labels, k)
}
sort.Strings(labels)
for _, k := range labels {
fmt.Printf(" \x1b[34m%s=%s\x1b[0m", k, v.Labels[k])
}
fmt.Printf("\n")
}
}
func printAllHosts() {
printHosts(allHosts)
}
func printSelectedHosts() {
fmt.Println("Hosts matched:")
if len(selectedHosts) == 0 {
fmt.Println("None")
return
}
printHosts(selectedHosts)
}
/* loadAutoCompletion
will load an autocompletion tree for the router / switches with the
most matches (counting DeviceType-field from yaml)
*/
func loadAutoCompletion(l *rl.Instance) {
var defaultCliCompletionName = "netiron"
var newCompletionName = ""
var countCompletionName = -1
if len(selectedHosts) == 0 {
return
}
/* Try to load cliCompletion for the type of hosts with the most matches */
var countDeviceTypes = make(map[string]int)
if len(selectedHosts) > 0 {
for x := range selectedHosts {
switch strings.ToLower(selectedHosts[x].DeviceType) {
case "vdx", "slx":
countDeviceTypes["vdx"]++
case "mlx", "cer", "mlxe", "xmr", "iron", "turobiron", "icx", "fcs":
countDeviceTypes["netiron"]++
case "juniper", "junos", "mx", "ex", "j":
countDeviceTypes["junos"]++
}
}
}
for key := range countDeviceTypes {
if countDeviceTypes[key] > countCompletionName {
newCompletionName, countCompletionName = key, countDeviceTypes[key]
}
}
if newCompletionName == "" {
newCompletionName = defaultCliCompletionName
}
loadAutoCompletionNamed(l, newCompletionName)
}
func loadAutoCompletionNamed(l *rl.Instance, newCompletionName string) {
/* Currently hardcoded ;( */
switch newCompletionName {
case "netiron", "mlx", "brocade", "cer", "tuborion", "mlxe":
l.Config.AutoComplete = cliNetironCompleter
case "junos", "juniper":
l.Config.AutoComplete = cliJunOSCompleter
case "vdx", "slx":
l.Config.AutoComplete = cliVDXCompleter
default:
l.Config.AutoComplete = cliNetironCompleter
newCompletionName = "netiron"
}
fmt.Println("Set", newCompletionName, "as default command line autocompletion tree")
}
/* setFilter executes a filter set on allHosts and will also load a pre-defined
auto completion tree */
func setFilter(label string) {
var err error
selectedHosts, err = libhost.LoadMatchesFromSlice(allHosts, label)
if err != nil {
fmt.Printf("Cant find any matches for the filter")
}
cliLabel = label
printSelectedHosts()
}
/* Command line mode: Read commands or config statements from shell, execute on targets */
func runShellMode() {
l, err := rl.NewEx(&rl.Config{
Prompt: "\033[31mmlxsh>\033[0m ",
HistoryFile: getUserHistoryFile(),
InterruptPrompt: "^C",
EOFPrompt: "exit",
HistorySearchFold: true,
FuncFilterInputRune: filterInput,
})
if err != nil {
panic(err)
}
/* Set defaults from flags */
setFilter(cliLabel)
loadAutoCompletion(l)
defer l.Close()
for {
line, err := l.Readline()
if err == rl.ErrInterrupt {
if len(line) == 0 {
break
} else {
continue
}
} else if err == io.EOF {
break
}
line = strings.TrimSpace(line)
switch {
case strings.HasPrefix(line, "clear "):
switch line[6:] {
default:
prepareRunCmd("run", line[6:])
}
case strings.HasPrefix(line, "ls "):
switch line[3:] {
case "hosts", "selhosts":
printSelectedHosts()
case "allhosts":
printAllHosts()
case "filter":
fmt.Println(cliLabel)
default:
printSelectedHosts()
}
case strings.HasPrefix(line, "request "):
switch line[8:] {
default:
prepareRunCmd("run", line[8:])
}
case strings.HasPrefix(line, "run "):
switch line[4:] {
default:
prepareRunCmd("run", line[4:])
}
case strings.HasPrefix(line, "mset filter "):
setFilter(line[12:])
loadAutoCompletion(l)
case strings.HasPrefix(line, "mset complete "):
loadAutoCompletionNamed(l, line[14:])
case strings.HasPrefix(line, "show "):
switch line[5:] {
default:
prepareRunCmd("run", "show "+line[5:])
}
case line == "exit":
goto exit
case line == "":
default:
fmt.Println("command not found or param missing:", strconv.Quote(line))
}
}
exit:
/* close all connections? */
/* cleanup some things? */
}
func prepareRunCmd(confOrRun, line string) {
if confOrRun == "run" {
cliScriptFile = line
cliConfigFile = ""
} else {
cliConfigFile = line
cliScriptFile = ""
}
run()
}