Module: Mikrotik::Client
- Defined in:
- lib/mikrotik/client.rb
Overview
Contains all methods for interacting with a device such as logging in and executing commands
Instance Attribute Summary (collapse)
-
- (Hash) options
readonly
Options used for the connection.
Class Method Summary (collapse)
-
+ (Client) connect(options = { :username => 'admin', :port => 8728 })
Connects to a RouterOS device.
Instance Method Summary (collapse)
-
- (Object) command(*path)
Shortcut for creating a Mikrotik::Command.
-
- (Mikrotik::Command) execute(command)
Sends a command to the device.
-
- (Client) initialize(opts = {})
A new instance of Client.
- - (Object) inspect
-
- (Boolean) logged_in?
Status of the session login.
-
- (Object) login
Logs in to the device.
Instance Attribute Details
- (Hash) options (readonly)
Options used for the connection
22 23 24 |
# File 'lib/mikrotik/client.rb', line 22 def @options end |
Class Method Details
+ (Client) connect(options = { :username => 'admin', :port => 8728 })
Connects to a RouterOS device. Required options are :host and :password - :username and :port default to admin and 8728 respectively
48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/mikrotik/client.rb', line 48 def self.connect( = { :username => 'admin', :port => 8728 }) defaults = { :username => 'admin', :port => 8728 } = defaults.merge() # Check required options have been given = !([:host, :port, :username, :password].map { |option| .key?(option) && ![option].nil? }.include?(false)) raise ArgumentError, "Options for host and password must be given" unless c = EM.connect [:host], [:port], self, c.pending_connect_timeout = 10.0 c end |
Instance Method Details
- (Object) command(*path)
Shortcut for creating a Mikrotik::Command
106 107 108 |
# File 'lib/mikrotik/client.rb', line 106 def command(*path) Mikrotik::Command.new(*path) end |
- (Mikrotik::Command) execute(command)
Sends a command to the device
94 95 96 |
# File 'lib/mikrotik/client.rb', line 94 def execute(command) send command end |
- (Client) initialize(opts = {})
A new instance of Client
114 115 116 117 118 119 120 121 122 |
# File 'lib/mikrotik/client.rb', line 114 def initialize(opts = {}) @closing = false @logged_in = false @options = opts @events = {} @buffer = "" @next_tag = 1 extend Mikrotik::Connection end |
- (Object) inspect
110 111 112 |
# File 'lib/mikrotik/client.rb', line 110 def inspect "#<#{self.class.name}:0x#{self.object_id} host=#{@options[:host]}>" end |
- (Boolean) logged_in?
Status of the session login
99 100 101 |
# File 'lib/mikrotik/client.rb', line 99 def logged_in? @logged_in end |
- (Object) login
Logs in to the device. This is called automatically when the TCP connection succeeds. To be sure the device is ready to execute commands before sending any register an event handler on on_login_success and send commands from there
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/mikrotik/client.rb', line 66 def login execute command(:login).on_reply { |reply| challenge = Mikrotik::Utilities.hex_to_bin(reply.result :ret) response = "00" + Digest::MD5.hexdigest(0.chr + @options[:password] + challenge) execute command(:login).with( :name => @options[:username], :response => response ).on_done { |replies| Mikrotik.debug [:client, :on_login_success] @logged_in = true on_login_success(self) }.on_trap { |trap| Mikrotik.debug [:client, :on_login_failure] if has_event_handler? :on_login_failure then on_login_failure(self) else raise Mikrotik::Errors::UnhandledTrap, 'Login failed' end } } end |