luqasz / librouteros

Python implementation of MikroTik RouterOS API
GNU General Public License v2.0
224 stars 50 forks source link

RoS 6.42.4 can't login #31

Closed Mantikor closed 6 years ago

Mantikor commented 6 years ago

Python 3.5.3 (default, Jan 19 2017, 14:11:04) [GCC 6.3.0 20170118] on linux Type "help", "copyright", "credits" or "license" for more information.

import librouteros client = librouteros.connect(host='192.168.88.1', username='hass', password='hass') Traceback (most recent call last): File "", line 1, in File "/usr/local/lib/python3.5/dist-packages/librouteros/init.py", line 58, in connect api('/login', **{'name': username, 'response': encode_password(token, password)}) File "/usr/local/lib/python3.5/dist-packages/librouteros/api.py", line 80, in call return self._readResponse() File "/usr/local/lib/python3.5/dist-packages/librouteros/api.py", line 106, in _readResponse self._trapCheck(response) File "/usr/local/lib/python3.5/dist-packages/librouteros/api.py", line 124, in _trapCheck raise TrapError(message=trap['message'], category=trap.get('category')) librouteros.exceptions.TrapError: cannot log in

librouteros version is 2.1.0 RoS log: 11:39:39 system,error,critical login failure for user hass from 192.168.88.151 via winbox

zentavr commented 6 years ago

Probably you need to downgrade to the previous release, because Mikrotik Core Team changed the API Login Flow.

Mantikor commented 6 years ago

zentavr, previous release RoS or librouteros? librouteros in 2.1.0 version support new login flow.

zentavr commented 6 years ago

@Mantikor Mikrotik's Manual:API says that in 6.43 there was a non-backward compatible change in the login flow.

So I guess:

librouteros/__init__.py#L45 has a logic to detect which login flow is being used. at the line 58 of the code seems like pre-6.43 flow was used in your case and there is something with the credentials you have passed.

luqasz commented 6 years ago

@Mantikor check with librouteros 2.0.0 and let us know.

Mantikor commented 6 years ago

@luqasz I try ver. 2.0.0 and the result is same:

root@orangepipc2:~/.homeassistant# pip3 install librouteros==2.0.0
Collecting librouteros==2.0.0
  Downloading https://files.pythonhosted.org/packages/04/de/3586a56bd2681fa246e910958fe91df7eb27384882e1511dfda6152ad7fb/librouteros-2.0.0.tar.gz
Installing collected packages: librouteros
  Found existing installation: librouteros 2.1.0
    Uninstalling librouteros-2.1.0:
      Successfully uninstalled librouteros-2.1.0
  Running setup.py install for librouteros ... done
Successfully installed librouteros-2.0.0
root@orangepipc2:~/.homeassistant# python3
Python 3.5.3 (default, Jan 19 2017, 14:11:04)
[GCC 6.3.0 20170118] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import librouteros
>>> client = librouteros.connect(host='192.168.88.1', username='hass', password='hass')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python3.5/dist-packages/librouteros/__init__.py", line 49, in connect
    api('/login', **{'name': username, 'response': encoded})
  File "/usr/local/lib/python3.5/dist-packages/librouteros/api.py", line 80, in __call__
    return self._readResponse()
  File "/usr/local/lib/python3.5/dist-packages/librouteros/api.py", line 106, in _readResponse
    self._trapCheck(response)
  File "/usr/local/lib/python3.5/dist-packages/librouteros/api.py", line 124, in _trapCheck
    raise TrapError(message=trap['message'], category=trap.get('category'))
librouteros.exceptions.TrapError: cannot log in
zentavr commented 6 years ago

@Mantikor I have RouterOS 6.42.3 on site, did a quick test with Python2 (seems like you have Python3). The script is:

#!/usr/bin/env python

import argparse
import ssl
import pprint
from librouteros import connect

def main():
    parser = argparse.ArgumentParser(description='Linet Law #133/2017 Filler')
    parser.add_argument('-d', '--domains', default='/tmp/domains.txt', dest='domains',
                        help='File where to seek for the domains.')
    parser.add_argument('-H', '--hostname', default='', dest='hostname', help='API Hostname.')
    parser.add_argument('-u', '--user', default='admin', dest='username', help='API User.')
    parser.add_argument('-p', '--password', default='', dest='password', help='API Password.')

    parser.add_argument('-s', '--ssl', dest='use_ssl', action='store_true', help='Use SSL.')
    parser.add_argument('-v', '--verbose', dest='verbose', action='store_true', help='Be verbose.')
    args = parser.parse_args()

    # Connection Arguments
    connect_args = {
        'port': 8728
    }

    with open(args.domains, 'r') as domains:
        if args.use_ssl:
            print "SSL will be used"
            ssl_ctx = ssl.create_default_context()
            ssl_ctx.check_hostname = False
            ssl_ctx.verify_mode = ssl.CERT_NONE

            connect_args = {
                'port': 8729,
                'ssl_wrapper': ssl_ctx.wrap_socket
            }

        # Connect to Mikrotik
        api = connect(
            username=args.username,
            password=args.password,
            host=args.hostname,
            **connect_args
        )

        pp = pprint.PrettyPrinter(indent=4)
        # Fetch Something
        stuff = api(cmd='/interface/print')
        pp.pprint(stuff)

        api.close()

if __name__ == '__main__':
    main()

I cannot reproduce your error. the Library is 2.1.0

Mantikor commented 6 years ago

@luqasz I upgrade my Mikrotik with 6.43rc32 and all works fine, seems the RoS fw 6.42.4 with api login bug.