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

See Also:

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#optionsHash (readonly)

Returns Options used for the connection

See Also:

  • Client#connect


22
23
24
# File 'lib/mikrotik/client.rb', line 22

def options
  @options
end

Class Method Details

.connect(options = { :username => 'admin', :port => 8728 }) ⇒ Client

Connects to a RouterOS device. Required options are :host and :password - :username and :port default to admin and 8728 respectively

Examples:

Connection with minimum options set

Mikrotik::Client.connect({
  :host => '192.168.1.1',  
  :password => 'topsecret'
})

Connection with all options set to override defaults

Mikrotik::Client.connect({
  :host => '10.200.0.21',
  :port => 3450,
  :username => 'apiuser',
  :password => 'topsecret'
})

Options Hash (options):

  • :host (String)

    Device's IP address

  • :port (Integer) — default: 8728

    Port of the API service on the device

  • :username (String) — default: 'admin'

    API username to authenticate with

  • :password (String)

    Password for the given username

Raises:

  • (ArgumentError)

    If not all required options are given



48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/mikrotik/client.rb', line 48

def self.connect(options = { :username => 'admin', :port => 8728 })
  defaults = { :username => 'admin', :port => 8728 }
  options = defaults.merge(options)
  # Check required options have been given
  all_required_options = !([:host, :port, :username, :password].map { |option|
    options.key?(option) && !options[option].nil?
  }.include?(false))
  raise ArgumentError, "Options for host and password must be given" unless all_required_options

  c = EM.connect options[:host], options[:port], self, options
  c.pending_connect_timeout = 10.0
  c
end

Instance Method Details

#command(*path) ⇒ Object

Shortcut for creating a Mikrotik::Command

See Also:



106
107
108
# File 'lib/mikrotik/client.rb', line 106

def command(*path)
  Mikrotik::Command.new(*path)
end

#execute(command) ⇒ Mikrotik::Command

Sends a command to the device



94
95
96
# File 'lib/mikrotik/client.rb', line 94

def execute(command)
  send command
end

#initialize(opts = {}) ⇒ Object



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

#inspectObject



110
111
112
# File 'lib/mikrotik/client.rb', line 110

def inspect
  "#<#{self.class.name}:0x#{self.object_id} host=#{@options[:host]}>"
end

#logged_in?Boolean



99
100
101
# File 'lib/mikrotik/client.rb', line 99

def logged_in?
  @logged_in
end

#loginObject

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 
  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
      (self)
    }.on_trap { |trap|
      Mikrotik.debug [:client, :on_login_failure]
      if has_event_handler? :on_login_failure then
        (self)
      else
        raise Mikrotik::Errors::UnhandledTrap, 'Login failed'
      end
    }

  }
end