2019-12-02 01:13:28 +01:00
|
|
|
"""Mikrotik API for Mikrotik Router."""
|
|
|
|
|
|
|
|
import ssl
|
|
|
|
import logging
|
2019-12-08 08:57:10 +01:00
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
import importlib
|
2019-12-08 09:09:31 +01:00
|
|
|
from .exceptions import ApiEntryNotFound
|
2019-12-08 09:39:23 +01:00
|
|
|
from .const import (
|
|
|
|
DEFAULT_LOGIN_METHOD,
|
|
|
|
DEFAULT_ENCODING,
|
|
|
|
)
|
2019-12-08 08:57:10 +01:00
|
|
|
|
|
|
|
MODULE_PATH = os.path.join(os.path.dirname(__file__), "librouteros", "__init__.py")
|
|
|
|
MODULE_NAME = "librouteros"
|
|
|
|
spec = importlib.util.spec_from_file_location(MODULE_NAME, MODULE_PATH)
|
|
|
|
librouteros = importlib.util.module_from_spec(spec)
|
|
|
|
sys.modules[spec.name] = librouteros
|
|
|
|
spec.loader.exec_module(librouteros)
|
|
|
|
|
2019-12-02 01:13:28 +01:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2019-12-02 03:19:07 +01:00
|
|
|
|
2019-12-02 17:59:49 +01:00
|
|
|
# ---------------------------
|
2019-12-02 01:13:28 +01:00
|
|
|
# MikrotikAPI
|
2019-12-02 17:59:49 +01:00
|
|
|
# ---------------------------
|
2019-12-02 01:13:28 +01:00
|
|
|
class MikrotikAPI:
|
2019-12-02 18:13:55 +01:00
|
|
|
"""Handle all communication with the Mikrotik API."""
|
2019-12-05 20:45:04 +01:00
|
|
|
|
2019-12-08 09:39:23 +01:00
|
|
|
def __init__(self, host, username, password, port=0, use_ssl=True, login_method=DEFAULT_LOGIN_METHOD, encoding=DEFAULT_ENCODING):
|
2019-12-02 18:13:55 +01:00
|
|
|
"""Initialize the Mikrotik Client."""
|
|
|
|
self._host = host
|
|
|
|
self._use_ssl = use_ssl
|
|
|
|
self._port = port
|
|
|
|
self._username = username
|
|
|
|
self._password = password
|
|
|
|
self._login_method = login_method
|
|
|
|
self._encoding = encoding
|
|
|
|
self._ssl_wrapper = None
|
2019-12-05 20:45:04 +01:00
|
|
|
|
2019-12-02 18:13:55 +01:00
|
|
|
self._connection = None
|
|
|
|
self._connected = False
|
|
|
|
self.error = ""
|
2019-12-05 20:45:04 +01:00
|
|
|
|
2019-12-02 18:13:55 +01:00
|
|
|
# Default ports
|
|
|
|
if not self._port:
|
|
|
|
self._port = 8729 if self._use_ssl else 8728
|
2019-12-05 20:45:04 +01:00
|
|
|
|
2019-12-02 18:13:55 +01:00
|
|
|
# ---------------------------
|
|
|
|
# connect
|
|
|
|
# ---------------------------
|
|
|
|
def connect(self):
|
|
|
|
"""Connect to Mikrotik device."""
|
|
|
|
self.error = ""
|
2019-12-05 22:10:42 +01:00
|
|
|
self._connected = None
|
2019-12-05 20:45:04 +01:00
|
|
|
|
2019-12-02 18:13:55 +01:00
|
|
|
kwargs = {
|
|
|
|
"encoding": self._encoding,
|
|
|
|
"login_methods": self._login_method,
|
|
|
|
"port": self._port,
|
|
|
|
}
|
2019-12-05 20:45:04 +01:00
|
|
|
|
2019-12-02 18:13:55 +01:00
|
|
|
if self._use_ssl:
|
|
|
|
if self._ssl_wrapper is None:
|
|
|
|
ssl_context = ssl.create_default_context()
|
|
|
|
ssl_context.check_hostname = False
|
|
|
|
ssl_context.verify_mode = ssl.CERT_NONE
|
|
|
|
self._ssl_wrapper = ssl_context.wrap_socket
|
|
|
|
kwargs["ssl_wrapper"] = self._ssl_wrapper
|
2019-12-05 20:45:04 +01:00
|
|
|
|
2019-12-02 18:13:55 +01:00
|
|
|
try:
|
|
|
|
self._connection = librouteros.connect(self._host, self._username, self._password, **kwargs)
|
|
|
|
except (
|
2019-12-02 19:22:34 +01:00
|
|
|
librouteros.exceptions.TrapError,
|
|
|
|
librouteros.exceptions.MultiTrapError,
|
|
|
|
librouteros.exceptions.ConnectionClosed,
|
|
|
|
librouteros.exceptions.ProtocolError,
|
2019-12-08 09:57:28 +01:00
|
|
|
librouteros.exceptions.FatalError,
|
|
|
|
ssl.SSLError
|
2019-12-02 18:13:55 +01:00
|
|
|
) as api_error:
|
|
|
|
_LOGGER.error("Mikrotik %s: %s", self._host, api_error)
|
|
|
|
self.error_to_strings("%s" % api_error)
|
|
|
|
self._connection = None
|
|
|
|
return False
|
|
|
|
else:
|
|
|
|
_LOGGER.info("Mikrotik Connected to %s", self._host)
|
|
|
|
self._connected = True
|
2019-12-05 20:45:04 +01:00
|
|
|
|
2019-12-02 18:13:55 +01:00
|
|
|
return self._connected
|
2019-12-05 20:45:04 +01:00
|
|
|
|
2019-12-02 18:13:55 +01:00
|
|
|
# ---------------------------
|
|
|
|
# error_to_strings
|
|
|
|
# ---------------------------
|
|
|
|
def error_to_strings(self, error):
|
2019-12-05 20:42:14 +01:00
|
|
|
"""Translate error output to error string."""
|
2019-12-02 18:13:55 +01:00
|
|
|
self.error = "cannot_connect"
|
|
|
|
if error == "invalid user name or password (6)":
|
|
|
|
self.error = "wrong_login"
|
2019-12-08 09:57:28 +01:00
|
|
|
|
|
|
|
if "ALERT_HANDSHAKE_FAILURE" in error:
|
|
|
|
self.error = "ssl_handshake_failure"
|
2019-12-05 20:45:04 +01:00
|
|
|
|
2019-12-02 18:13:55 +01:00
|
|
|
return
|
2019-12-05 20:45:04 +01:00
|
|
|
|
2019-12-02 18:13:55 +01:00
|
|
|
# ---------------------------
|
|
|
|
# connected
|
|
|
|
# ---------------------------
|
|
|
|
def connected(self):
|
|
|
|
"""Return connected boolean."""
|
|
|
|
return self._connected
|
2019-12-05 20:45:04 +01:00
|
|
|
|
2019-12-02 18:13:55 +01:00
|
|
|
# ---------------------------
|
|
|
|
# path
|
|
|
|
# ---------------------------
|
|
|
|
def path(self, path):
|
|
|
|
"""Retrieve data from Mikrotik API."""
|
|
|
|
if not self._connected or not self._connection:
|
|
|
|
if not self.connect():
|
|
|
|
return None
|
2019-12-05 20:45:04 +01:00
|
|
|
|
2019-12-02 18:13:55 +01:00
|
|
|
try:
|
|
|
|
response = self._connection.path(path)
|
|
|
|
tuple(response)
|
|
|
|
except librouteros.exceptions.ConnectionClosed:
|
|
|
|
_LOGGER.error("Mikrotik %s connection closed", self._host)
|
|
|
|
self._connected = False
|
|
|
|
self._connection = None
|
|
|
|
return None
|
|
|
|
except (
|
2019-12-02 19:22:34 +01:00
|
|
|
librouteros.exceptions.TrapError,
|
|
|
|
librouteros.exceptions.MultiTrapError,
|
|
|
|
librouteros.exceptions.ProtocolError,
|
|
|
|
librouteros.exceptions.FatalError
|
2019-12-02 18:13:55 +01:00
|
|
|
) as api_error:
|
|
|
|
_LOGGER.error("Mikrotik %s connection error %s", self._host, api_error)
|
|
|
|
return None
|
2019-12-05 20:45:04 +01:00
|
|
|
|
2019-12-02 18:13:55 +01:00
|
|
|
return response if response else None
|
2019-12-05 20:45:04 +01:00
|
|
|
|
2019-12-03 01:47:38 +01:00
|
|
|
# ---------------------------
|
|
|
|
# update
|
|
|
|
# ---------------------------
|
|
|
|
def update(self, path, param, value, mod_param, mod_value):
|
2019-12-05 20:42:14 +01:00
|
|
|
"""Modify a parameter"""
|
2019-12-07 21:20:36 +01:00
|
|
|
entry_found = False
|
2019-12-06 01:17:01 +01:00
|
|
|
if not self._connected or not self._connection:
|
|
|
|
if not self.connect():
|
|
|
|
return None
|
|
|
|
|
2019-12-06 01:30:07 +01:00
|
|
|
response = self.path(path)
|
|
|
|
if response is None:
|
|
|
|
return False
|
2019-12-05 20:45:04 +01:00
|
|
|
|
2019-12-06 01:30:07 +01:00
|
|
|
for tmp in response:
|
|
|
|
if param not in tmp:
|
|
|
|
continue
|
2019-12-05 20:45:04 +01:00
|
|
|
|
2019-12-06 01:30:07 +01:00
|
|
|
if tmp[param] != value:
|
|
|
|
continue
|
2019-12-05 20:45:04 +01:00
|
|
|
|
2019-12-07 21:20:36 +01:00
|
|
|
entry_found = True
|
2019-12-06 01:30:07 +01:00
|
|
|
params = {
|
|
|
|
'.id': tmp['.id'],
|
|
|
|
mod_param: mod_value
|
|
|
|
}
|
2019-12-05 20:45:04 +01:00
|
|
|
|
2019-12-06 01:30:07 +01:00
|
|
|
try:
|
2019-12-06 01:17:01 +01:00
|
|
|
response.update(**params)
|
2019-12-06 01:30:07 +01:00
|
|
|
except librouteros.exceptions.ConnectionClosed:
|
|
|
|
_LOGGER.error("Mikrotik %s connection closed", self._host)
|
|
|
|
self._connected = False
|
|
|
|
self._connection = None
|
|
|
|
return None
|
|
|
|
except (
|
|
|
|
librouteros.exceptions.TrapError,
|
|
|
|
librouteros.exceptions.MultiTrapError,
|
|
|
|
librouteros.exceptions.ProtocolError,
|
|
|
|
librouteros.exceptions.FatalError
|
|
|
|
) as api_error:
|
|
|
|
_LOGGER.error("Mikrotik %s connection error %s", self._host, api_error)
|
|
|
|
return None
|
2019-12-05 20:45:04 +01:00
|
|
|
|
2019-12-07 21:20:36 +01:00
|
|
|
if not entry_found:
|
|
|
|
error = "Parameter \"{}\" with value \"{}\" not found".format(param, value)
|
|
|
|
raise ApiEntryNotFound(error)
|
|
|
|
|
2019-12-04 20:13:11 +01:00
|
|
|
return True
|
2019-12-05 20:45:04 +01:00
|
|
|
|
2019-12-04 20:13:11 +01:00
|
|
|
# ---------------------------
|
|
|
|
# run_script
|
|
|
|
# ---------------------------
|
|
|
|
def run_script(self, name):
|
2019-12-05 20:42:14 +01:00
|
|
|
"""Run script"""
|
2019-12-07 21:20:36 +01:00
|
|
|
entry_found = False
|
2019-12-06 01:17:01 +01:00
|
|
|
if not self._connected or not self._connection:
|
|
|
|
if not self.connect():
|
|
|
|
return None
|
|
|
|
|
2019-12-06 01:30:07 +01:00
|
|
|
response = self.path('/system/script')
|
|
|
|
if response is None:
|
|
|
|
return False
|
2019-12-05 20:45:04 +01:00
|
|
|
|
2019-12-06 01:30:07 +01:00
|
|
|
for tmp in response:
|
|
|
|
if 'name' not in tmp:
|
|
|
|
continue
|
2019-12-05 20:45:04 +01:00
|
|
|
|
2019-12-06 01:30:07 +01:00
|
|
|
if tmp['name'] != name:
|
|
|
|
continue
|
2019-12-05 20:45:04 +01:00
|
|
|
|
2019-12-07 21:20:36 +01:00
|
|
|
entry_found = True
|
2019-12-06 01:30:07 +01:00
|
|
|
try:
|
2019-12-06 01:17:01 +01:00
|
|
|
run = response('run', **{'.id': tmp['.id']})
|
2019-12-06 01:30:07 +01:00
|
|
|
except librouteros.exceptions.ConnectionClosed:
|
|
|
|
_LOGGER.error("Mikrotik %s connection closed", self._host)
|
|
|
|
self._connected = False
|
|
|
|
self._connection = None
|
|
|
|
return None
|
|
|
|
except (
|
|
|
|
librouteros.exceptions.TrapError,
|
|
|
|
librouteros.exceptions.MultiTrapError,
|
|
|
|
librouteros.exceptions.ProtocolError,
|
|
|
|
librouteros.exceptions.FatalError
|
|
|
|
) as api_error:
|
|
|
|
_LOGGER.error("Mikrotik %s connection error %s", self._host, api_error)
|
|
|
|
return None
|
|
|
|
|
|
|
|
tuple(run)
|
2019-12-05 20:45:04 +01:00
|
|
|
|
2019-12-07 21:20:36 +01:00
|
|
|
if not entry_found:
|
|
|
|
error = "Script \"{}\" not found".format(name)
|
|
|
|
raise ApiEntryNotFound(error)
|
|
|
|
|
2019-12-04 20:13:11 +01:00
|
|
|
return True
|
2019-12-08 11:20:02 +01:00
|
|
|
|
|
|
|
# ---------------------------
|
|
|
|
# get_traffic
|
|
|
|
# ---------------------------
|
|
|
|
def get_traffic(self, interfaces):
|
|
|
|
"""Get traffic stats"""
|
|
|
|
traffic = None
|
|
|
|
if not self._connected or not self._connection:
|
|
|
|
if not self.connect():
|
|
|
|
return None
|
|
|
|
|
|
|
|
response = self.path('/interface')
|
|
|
|
if response is None:
|
|
|
|
return None
|
|
|
|
|
|
|
|
args = {'interface': interfaces, 'once': True}
|
|
|
|
try:
|
|
|
|
traffic = response('monitor-traffic', **args)
|
|
|
|
except librouteros.exceptions.ConnectionClosed:
|
|
|
|
_LOGGER.error("Mikrotik %s connection closed", self._host)
|
|
|
|
self._connected = False
|
|
|
|
self._connection = None
|
|
|
|
return None
|
|
|
|
except (
|
|
|
|
librouteros.exceptions.TrapError,
|
|
|
|
librouteros.exceptions.MultiTrapError,
|
|
|
|
librouteros.exceptions.ProtocolError,
|
|
|
|
librouteros.exceptions.FatalError
|
|
|
|
) as api_error:
|
|
|
|
_LOGGER.error("Mikrotik %s connection error %s", self._host, api_error)
|
|
|
|
return None
|
|
|
|
|
|
|
|
return traffic
|