forked from go-routeros/routeros
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
114 lines (92 loc) · 1.93 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
package main
import (
"flag"
"log"
"strings"
"time"
"gopkg.in/routeros.v2"
)
var (
command = flag.String("command", "/ip/firewall/address-list/listen", "RouterOS command")
address = flag.String("address", "127.0.0.1:8728", "RouterOS address and port")
username = flag.String("username", "admin", "User name")
password = flag.String("password", "admin", "Password")
timeout = flag.Duration("timeout", 10*time.Second, "Cancel after")
async = flag.Bool("async", false, "Call Async()")
useTLS = flag.Bool("tls", false, "Use TLS")
)
func dial() (*routeros.Client, error) {
if *useTLS {
return routeros.DialTLS(*address, *username, *password, nil)
}
return routeros.Dial(*address, *username, *password)
}
func main() {
flag.Parse()
c, err := dial()
if err != nil {
log.Fatal(err)
}
defer c.Close()
c.Queue = 100
if *async {
explicitAsync(c)
} else {
implicitAsync(c)
}
}
func explicitAsync(c *routeros.Client) {
errC := c.Async()
go func() {
l, err := c.ListenArgs(strings.Split(*command, " "))
if err != nil {
log.Fatal(err)
}
go func() {
time.Sleep(*timeout)
log.Print("Cancelling the RouterOS command...")
_, err := l.Cancel()
if err != nil {
log.Fatal(err)
}
}()
log.Print("Waiting for !re...")
for sen := range l.Chan() {
log.Printf("Update: %s", sen)
}
err = l.Err()
if err != nil {
log.Fatal(err)
}
log.Print("Done!")
c.Close()
}()
err := <-errC
if err != nil {
log.Fatal(err)
}
}
func implicitAsync(c *routeros.Client) {
l, err := c.ListenArgs(strings.Split(*command, " "))
if err != nil {
log.Fatal(err)
}
go func() {
time.Sleep(*timeout)
log.Print("Cancelling the RouterOS command...")
_, err := l.Cancel()
if err != nil {
log.Fatal(err)
}
}()
log.Print("Waiting for !re...")
for sen := range l.Chan() {
log.Printf("Update: %s", sen)
}
err = l.Err()
if err != nil {
log.Fatal(err)
}
log.Print("Done!")
c.Close()
}