routeros-api 0.2.0 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +8 -0
- data/README.md +2 -4
- data/examples/async.rb +20 -0
- data/examples/simple.rb +5 -3
- data/lib/routeros/api.rb +25 -5
- data/lib/routeros/async.rb +37 -0
- data/lib/routeros/response.rb +2 -2
- data/lib/routeros/word_stream.rb +12 -3
- metadata +8 -7
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b5febdeccbba11422cb6b855ea59a0f9fb065d375f8782c0ba6771c083d8d2bf
|
4
|
+
data.tar.gz: 27aab05e7586a813b76ebd300d212828d8dcad0b1347485a3878774f21299a11
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 70e416037d768b5e1c7e9c04db9849703409db8c6d64c14a1015c51c671a2c2f50bb237898a37cbddd6d959fe0696bcb0252f7f80c4f358908b833ecf5ddf039
|
7
|
+
data.tar.gz: 38f1a4a21045413c7e5f31b12362ebc52e746245c79dc5357ce1d0a1a373fd17883e4023da38a695aa9c347f1f554313177bd623428d8ed12ce29408200ed43b
|
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -24,11 +24,9 @@ if response.ok?
|
|
24
24
|
end
|
25
25
|
```
|
26
26
|
|
27
|
-
|
27
|
+
### Async
|
28
28
|
|
29
|
-
|
30
|
-
- [ ] Async support
|
31
|
-
- [ ] Auto deploy and auto changelog
|
29
|
+
Async gem is supported but not required, if the gem can be loaded a method `async_command` will be available. Look at the examples folder for a full example.
|
32
30
|
|
33
31
|
## Development
|
34
32
|
|
data/examples/async.rb
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "routeros/api"
|
5
|
+
|
6
|
+
api = RouterOS::API.new("192.168.1.1", 8728, ssl: false)
|
7
|
+
response = api.login("test", "test")
|
8
|
+
puts(response) # (OK) []
|
9
|
+
|
10
|
+
if response.ok?
|
11
|
+
if RouterOS::API.async_enabled?
|
12
|
+
response = api.async_command("ip/route/getall")
|
13
|
+
puts(response) # Async::Task
|
14
|
+
puts(response.wait.data) # [{ ... }]
|
15
|
+
else
|
16
|
+
warn('asyn gem could not be loaded')
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
api.close
|
data/examples/simple.rb
CHANGED
@@ -3,11 +3,13 @@
|
|
3
3
|
require "bundler/setup"
|
4
4
|
require "routeros/api"
|
5
5
|
|
6
|
-
api = RouterOS::API.new("192.168.1.1", 8728)
|
7
|
-
response = api.login("
|
6
|
+
api = RouterOS::API.new("192.168.1.1", 8728, ssl: false)
|
7
|
+
response = api.login("test", "test")
|
8
8
|
puts(response) # (OK) []
|
9
9
|
|
10
10
|
if response.ok?
|
11
11
|
response = api.command("ip/route/getall")
|
12
|
-
puts(response.data) #
|
12
|
+
puts(response.data) # [{ ... }]
|
13
13
|
end
|
14
|
+
|
15
|
+
api.close
|
data/lib/routeros/api.rb
CHANGED
@@ -1,6 +1,8 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
require "socket"
|
4
|
+
require "openssl"
|
5
|
+
require_relative "async"
|
4
6
|
require_relative "request"
|
5
7
|
require_relative "response"
|
6
8
|
require_relative "word_stream"
|
@@ -8,12 +10,25 @@ require_relative "word_stream"
|
|
8
10
|
module RouterOS
|
9
11
|
# Main abstraction to connect to RouterOS
|
10
12
|
class API
|
11
|
-
|
13
|
+
include RouterOS::Async
|
14
|
+
|
15
|
+
VERSION = "0.4.0"
|
12
16
|
|
13
17
|
class Error < StandardError; end
|
14
18
|
|
15
|
-
def initialize(host, port)
|
16
|
-
|
19
|
+
def initialize(host, port, ssl: false, ssl_ctx: nil)
|
20
|
+
raw_socket = TCPSocket.new(host, port)
|
21
|
+
|
22
|
+
if ssl
|
23
|
+
ssl_socket = OpenSSL::SSL::SSLSocket.new(raw_socket, ssl_ctx || OpenSSL::SSL::SSLContext.new)
|
24
|
+
ssl_socket.connect
|
25
|
+
socket = ssl_socket
|
26
|
+
else
|
27
|
+
socket = raw_socket
|
28
|
+
end
|
29
|
+
|
30
|
+
@ssl = ssl
|
31
|
+
@stream = RouterOS::WordStream.new(socket)
|
17
32
|
@tag = 0
|
18
33
|
end
|
19
34
|
|
@@ -23,7 +38,12 @@ module RouterOS
|
|
23
38
|
end
|
24
39
|
|
25
40
|
def login(name, password)
|
26
|
-
|
41
|
+
warn("non encrypted connection, be careful") unless @ssl
|
42
|
+
command("/login", { name:, password: })
|
43
|
+
end
|
44
|
+
|
45
|
+
def close
|
46
|
+
@stream.close
|
27
47
|
end
|
28
48
|
|
29
49
|
private
|
@@ -49,7 +69,7 @@ module RouterOS
|
|
49
69
|
|
50
70
|
response << sentence
|
51
71
|
|
52
|
-
break if sentence.include?
|
72
|
+
break if sentence.include?("!done")
|
53
73
|
end
|
54
74
|
|
55
75
|
RouterOS::Response.new(response)
|
@@ -0,0 +1,37 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RouterOS
|
4
|
+
# adds async support if async gem is present
|
5
|
+
module Async
|
6
|
+
@enabled = false
|
7
|
+
|
8
|
+
def self.included(base)
|
9
|
+
base.extend(ClassMethods)
|
10
|
+
end
|
11
|
+
|
12
|
+
def self.enabled?
|
13
|
+
@enabled
|
14
|
+
end
|
15
|
+
|
16
|
+
# class methods to be added when this module is included
|
17
|
+
module ClassMethods
|
18
|
+
def async_enabled?
|
19
|
+
RouterOS::Async.enabled?
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
begin
|
24
|
+
require 'async'
|
25
|
+
|
26
|
+
def async_command(cmd, args = [])
|
27
|
+
Async do
|
28
|
+
command(cmd, args)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
@enabled = true
|
33
|
+
rescue LoadError
|
34
|
+
# async gem is not available, so no-op
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
data/lib/routeros/response.rb
CHANGED
@@ -3,8 +3,8 @@
|
|
3
3
|
module RouterOS
|
4
4
|
# Represents a parsed RouterOS response
|
5
5
|
class Response
|
6
|
-
KNOWN_NUMBER_FIELDS = %i[scope distance target-scope]
|
7
|
-
KNOWN_BOOLEAN_VALUES = %w[true false]
|
6
|
+
KNOWN_NUMBER_FIELDS = %i[scope distance target-scope].freeze
|
7
|
+
KNOWN_BOOLEAN_VALUES = %w[true false].freeze
|
8
8
|
|
9
9
|
attr_reader :raw_sentences, :data, :tag
|
10
10
|
|
data/lib/routeros/word_stream.rb
CHANGED
@@ -19,6 +19,10 @@ module RouterOS
|
|
19
19
|
@stream.read(read_length)
|
20
20
|
end
|
21
21
|
|
22
|
+
def close
|
23
|
+
@stream.close
|
24
|
+
end
|
25
|
+
|
22
26
|
# utility to encode the length
|
23
27
|
def self.encode_length(length)
|
24
28
|
case length
|
@@ -49,9 +53,14 @@ module RouterOS
|
|
49
53
|
|
50
54
|
private
|
51
55
|
|
56
|
+
def readbyte
|
57
|
+
bytes = @stream.read(1).bytes
|
58
|
+
bytes[0]
|
59
|
+
end
|
60
|
+
|
52
61
|
# reads the length from the underlying stream
|
53
62
|
def read_length
|
54
|
-
length =
|
63
|
+
length = readbyte
|
55
64
|
mask = 0xFF
|
56
65
|
|
57
66
|
if length & 0x80 == 0x00 # first bit is 0
|
@@ -66,7 +75,7 @@ module RouterOS
|
|
66
75
|
byte_amount = 4
|
67
76
|
mask = 0x1F
|
68
77
|
elsif length & 0xF8 == 0xF0 # first, second, third and fourth bits are 1, fifth is 0
|
69
|
-
length =
|
78
|
+
length = readbyte
|
70
79
|
byte_amount = 3
|
71
80
|
end
|
72
81
|
|
@@ -74,7 +83,7 @@ module RouterOS
|
|
74
83
|
|
75
84
|
(byte_amount - 1).times do
|
76
85
|
length <<= 8
|
77
|
-
length +=
|
86
|
+
length += readbyte
|
78
87
|
end
|
79
88
|
|
80
89
|
length
|
metadata
CHANGED
@@ -1,16 +1,16 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: routeros-api
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.4.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Marcelo Barros
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2024-10-
|
11
|
+
date: 2024-10-22 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
|
-
description:
|
13
|
+
description:
|
14
14
|
email:
|
15
15
|
- mcbarros@gmail.com
|
16
16
|
executables: []
|
@@ -23,8 +23,10 @@ files:
|
|
23
23
|
- LICENSE.txt
|
24
24
|
- README.md
|
25
25
|
- Rakefile
|
26
|
+
- examples/async.rb
|
26
27
|
- examples/simple.rb
|
27
28
|
- lib/routeros/api.rb
|
29
|
+
- lib/routeros/async.rb
|
28
30
|
- lib/routeros/request.rb
|
29
31
|
- lib/routeros/response.rb
|
30
32
|
- lib/routeros/word_stream.rb
|
@@ -33,10 +35,9 @@ homepage: https://github.com/mcbarros/routeros-api
|
|
33
35
|
licenses:
|
34
36
|
- MIT
|
35
37
|
metadata:
|
36
|
-
homepage_uri: https://github.com/mcbarros/routeros-api
|
37
38
|
source_code_uri: https://github.com/mcbarros/routeros-api
|
38
39
|
changelog_uri: https://github.com/mcbarros/routeros-api/blob/main/CHANGELOG.md
|
39
|
-
post_install_message:
|
40
|
+
post_install_message:
|
40
41
|
rdoc_options: []
|
41
42
|
require_paths:
|
42
43
|
- lib
|
@@ -52,7 +53,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
52
53
|
version: '0'
|
53
54
|
requirements: []
|
54
55
|
rubygems_version: 3.5.16
|
55
|
-
signing_key:
|
56
|
+
signing_key:
|
56
57
|
specification_version: 4
|
57
58
|
summary: Ruby lib to access RouterOS API.
|
58
59
|
test_files: []
|