Skip to content

Commit

Permalink
Merge pull request #32 from stevehaskew/v6.43-support
Browse files Browse the repository at this point in the history
Basic support for new login method.
  • Loading branch information
kramarz authored Jul 24, 2018
2 parents a8c9693 + 917072b commit 8c49e8f
Showing 1 changed file with 22 additions and 15 deletions.
37 changes: 22 additions & 15 deletions routeros_api/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,20 @@
from routeros_api import resource


def connect(host, username='admin', password='', port=None, use_ssl=False, ssl_verify=True, ssl_verify_hostname=True, ssl_context=None):
return RouterOsApiPool(host, username, password, port, use_ssl, ssl_verify, ssl_verify_hostname, ssl_context).get_api()
def connect(host, username='admin', password='', port=None, plaintext_login=False, use_ssl=False, ssl_verify=True, ssl_verify_hostname=True, ssl_context=None):
return RouterOsApiPool(host, username, password, port, plaintext_login, use_ssl, ssl_verify, ssl_verify_hostname, ssl_context).get_api()


class RouterOsApiPool(object):
socket_timeout = 15.0

def __init__(self, host, username='admin', password='', port=None, use_ssl=False, ssl_verify=True, ssl_verify_hostname=True, ssl_context=None):
def __init__(self, host, username='admin', password='', port=None, plaintext_login=False, use_ssl=False, ssl_verify=True, ssl_verify_hostname=True, ssl_context=None):
self.host = host
self.username = username
self.password = password

self.plaintext_login = plaintext_login

self.ssl_context = ssl_context
# Use SSL? Ignored when using a context, so we will set it for simple reference when port-switching:
if ssl_context is not None:
Expand All @@ -29,7 +31,7 @@ def __init__(self, host, username='admin', password='', port=None, use_ssl=False
self.use_ssl = use_ssl
self.ssl_verify = ssl_verify
self.ssl_verify_hostname = ssl_verify_hostname

self.port = port or self._select_default_port(self.use_ssl)

self.connected = False
Expand All @@ -46,7 +48,7 @@ def get_api(self):
self.api = RouterOsApi(communicator)
for handler in self._get_exception_handlers():
communicator.add_exception_handler(handler)
self.api.login(self.username, self.password)
self.api.login(self.username, self.password, self.plaintext_login)
self.connected = True
return self.api

Expand Down Expand Up @@ -74,16 +76,21 @@ class RouterOsApi(object):
def __init__(self, communicator):
self.communicator = communicator

def login(self, login, password):
response = self.get_binary_resource('/').call('login')
token = binascii.unhexlify(response.done_message['ret'])
hasher = hashlib.md5()
hasher.update(b'\x00')
hasher.update(password.encode())
hasher.update(token)
hashed = b'00' + hasher.hexdigest().encode('ascii')
self.get_binary_resource('/').call(
'login', {'name': login.encode(), 'response': hashed})
def login(self, login, password, plaintext_login):
response = None
if plaintext_login:
response = self.get_binary_resource('/').call('login',{ 'name': login, 'password': password })
else:
response = self.get_binary_resource('/').call('login')
if 'ret' in response.done_message:
token = binascii.unhexlify(response.done_message['ret'])
hasher = hashlib.md5()
hasher.update(b'\x00')
hasher.update(password.encode())
hasher.update(token)
hashed = b'00' + hasher.hexdigest().encode('ascii')
self.get_binary_resource('/').call(
'login', {'name': login.encode(), 'response': hashed})

def get_resource(self, path, structure=None):
structure = structure or api_structure.default_structure
Expand Down

0 comments on commit 8c49e8f

Please sign in to comment.