forked from sg3des/mikrotik
-
Notifications
You must be signed in to change notification settings - Fork 0
/
commands.go
309 lines (247 loc) · 7.54 KB
/
commands.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
package mikrotik
import (
"fmt"
"log"
routeros "gopkg.in/routeros.v2"
)
// ===============================
//
// Mikrotik CMDs
//
// ===============================
//Run one line command on mikrotik by api
func (mik *Mikrotik) Run(cmd string) (*routeros.Reply, error) {
mik.connMutex.Lock()
defer mik.connMutex.Unlock()
re, err := mik.Conn.Run(cmd)
if err != nil {
mik.Conn.Run("")
}
return re, err
}
//RunArgs run many line command on mikrotik by api
func (mik *Mikrotik) RunArgs(cmd string, args ...string) (*routeros.Reply, error) {
mik.connMutex.Lock()
defer mik.connMutex.Unlock()
re, err := mik.Conn.RunArgs(append([]string{cmd}, args...))
if err != nil {
mik.Conn.Run("")
}
return re, err
}
//RunMarshal - run command and marshal response to interface struct
func (mik *Mikrotik) RunMarshal(cmd string, v interface{}) error {
re, err := mik.Run(cmd)
if err != nil {
return err
}
return mik.ParseResponce(re, v)
}
// ParseResponce - Parse the mikrotik's reply into the interface using reflect.
func (mik *Mikrotik) ParseResponce(re *routeros.Reply, v interface{}) error {
for _, resp := range re.Re {
if mik.debug {
log.Println(resp)
}
if err := ValuesFrom(resp.Map).To(v); err != nil {
return err
}
}
return nil
}
//Print returns all items by apipath and marshal it to passed structure
func (mik *Mikrotik) Print(apipath string, v interface{}) error {
return mik.RunMarshal(apipath, v)
}
//Add item from passed struct to apipath
func (mik *Mikrotik) Add(apipath string, v interface{}) error {
re, err := mik.RunArgs(apipath, ToArgs(v)...)
if err != nil {
return err
}
if id, ok := re.Done.Map["ret"]; ok {
SetID(v, id)
}
return nil
}
//Set value of item by id
func (mik *Mikrotik) Set(apipath, id string, v interface{}) error {
args := append([]string{"=.id=" + id}, ToArgs(v)...)
_, err := mik.RunArgs(apipath, args...)
return err
}
//SetOne set value to one field
func (mik *Mikrotik) SetOne(apipath, name, value string) error {
_, err := mik.RunArgs(apipath, fmt.Sprintf("=%s=%s", name, value))
return err
}
//Remove item by id
func (mik *Mikrotik) Remove(apipath, id string) error {
_, err := mik.RunArgs(apipath, "=.id="+id)
return err
}
//Enable item by id
func (mik *Mikrotik) Enable(apipath, id string) error {
_, err := mik.RunArgs(apipath, "=.id="+id)
return err
}
//Disable item by id
func (mik *Mikrotik) Disable(apipath, id string) error {
_, err := mik.RunArgs(apipath, "=.id="+id)
return err
}
//Comment - add comment to item by id
func (mik *Mikrotik) Comment(apipath, id, comment string) error {
_, err := mik.RunArgs(apipath, "=.id="+id, "=comment="+comment)
return err
}
// Debug activates the debug mode on the library
func (mik *Mikrotik) Debug(debug bool) {
mik.debug = debug
}
// Close closes the connection with the Mikrotik
func (mik *Mikrotik) Close() {
mik.Conn.Close()
}
// Delay creates a sleep mode for the mikrotik
func (mik *Mikrotik) Delay(time string) error {
_, err := mik.RunArgs("/delay", "=delay-time="+time)
return err
}
// Reboot reboots the mikrotik
func (mik *Mikrotik) Reboot() error {
_, err := mik.RunArgs("/system/reboot")
return err
}
// Upgrade upgrades the mikrotik
func (mik *Mikrotik) Upgrade() error {
_, err := mik.RunArgs("/system/routerboard/upgrade")
return err
}
// ===============================
//
// Basic CMDs
//
// ===============================
// The cmd struct represents most of the mikrotik's struct including commands like Add, Print, Find, Comment and whatever.
type cmd struct {
mikrotik *Mikrotik
path string
}
// Print simply calls the mikrotik's Print for the commands that use cmd struct.
func (c *cmd) Print(v interface{}) error {
return c.mikrotik.Print(c.path+"/print", v)
}
// Find runs a print using the where argument searching for a precise attribute.
func (c *cmd) Find(v interface{}, where ...string) error {
re, err := c.mikrotik.RunArgs(c.path+"/print", where...)
if err != nil {
return err
}
return c.mikrotik.ParseResponce(re, v)
}
// Find runs a print using the where argument searching for a precise attribute.
func (c *cmd) FindMultipleArgs(v interface{}, where ...string) error {
re, err := c.mikrotik.RunArgs(c.path+"/print", where...)
if err != nil {
return err
}
return c.mikrotik.ParseResponce(re, v)
}
// PrintWhere prints all the information about a specific entity using its ID
func (c *cmd) PrintWhere(id string, v interface{}) error {
re, err := c.mikrotik.RunArgs(c.path+"/print", "where", "?.id="+id)
if err != nil {
return err
}
return c.mikrotik.ParseResponce(re, v)
}
// Add an entity to the mikrotik
func (c *cmd) Add(v interface{}) error {
return c.mikrotik.Add(c.path+"/add", v)
}
//Set value of item by id
func (c *cmd) Set(id string, v interface{}) error {
return c.mikrotik.Set(c.path+"/set", id, v)
}
//Remove item by id
func (c *cmd) Remove(id string) error {
return c.mikrotik.Remove(c.path+"/remove", id)
}
//Enable item by id
func (c *cmd) Enable(id string) error {
return c.mikrotik.Enable(c.path+"/enable", id)
}
//Disable item by id
func (c *cmd) Disable(id string) error {
return c.mikrotik.Disable(c.path+"/disable", id)
}
//Comment - add comment to item by id
func (c *cmd) Comment(id, comment string) error {
return c.mikrotik.Comment(c.path+"/comment", id, comment)
}
// ===============================
//
// Configuration CMDs
//
// ===============================
// The cfg struct represents the basic commands for configurations that use only Print and Set.
type cfg struct {
mikrotik *Mikrotik
path string
}
// Print simply calls the mikrotik's Print for the commands that use cfg struct.
func (c *cfg) Print(v interface{}) error {
return c.mikrotik.Print(c.path+"/print", v)
}
//Set value of item by id
func (c *cfg) Set(name, value string) error {
return c.mikrotik.SetOne(c.path+"/set", name, value)
}
// ===============================
//
// SSH CMDs
//
// ===============================
// The struct sshcmds includes all the methods for the SSH keys managed in System/User
type sshcmds struct {
mikrotik *Mikrotik
path string
}
// Print simply calls the mikrotik's Print for the commands that use sshcmd struct.
func (ssh *sshcmds) Print(v interface{}) error {
return ssh.mikrotik.Print(ssh.path+"/print", v)
}
//Remove item by id
func (ssh *sshcmds) Remove(id string) error {
return ssh.mikrotik.Remove(ssh.path+"/remove", id)
}
// Find runs a print using the where argument searching for a precise attribute.
func (ssh *sshcmds) Find(where string, v interface{}) error {
re, err := ssh.mikrotik.RunArgs(ssh.path+"/print", "?"+where)
if err != nil {
return err
}
return ssh.mikrotik.ParseResponce(re, v)
}
// Import imports the specified SSH key into the specified user
func (ssh *sshcmds) Import(user, keyfile string) error {
_, err := ssh.mikrotik.RunArgs(ssh.path+"/import", "=public-key-file="+keyfile, "=user="+user)
return err
}
// ===============================
//
// Firewall CMDs
//
// ===============================
type firewallOptionsCMD struct {
cmd
}
func (ff *firewallOptionsCMD) ResetCounters(id string) error {
_, err := ff.cmd.mikrotik.RunArgs(ff.cmd.path+"/reset-counters", "=numbers="+id)
return err
}
func (ff *firewallOptionsCMD) ResetCountersAll() error {
_, err := ff.cmd.mikrotik.RunArgs(ff.cmd.path + "/reset-counters-all")
return err
}