File: connect.rst

package info (click to toggle)
librouteros 3.1.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 336 kB
  • sloc: python: 1,110; makefile: 127; sh: 8
file content (67 lines) | stat: -rw-r--r-- 1,834 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
Connect
=======

Unencrypted
-----------

.. code-block:: python

    from librouteros import connect
    api = connect(
        username='admin',
        password='abc',
        host='some.address.com',
    )

Encrypted
---------

Before connecting, ``api-ssl`` service on routeros must be enabled.
For more information on how to generate certificates see
`MikroTik wiki <https://wiki.mikrotik.com/wiki/Manual:Create_Certificates>`_.
After that, create your default `SSLContext <https://docs.python.org/library/ssl.html#ssl.create_default_context>`_
and fine tune for your needs. Code below allows connecting to API without ceritficate.

.. code-block:: python

    import ssl
    from librouteros import connect

    ctx = ssl.create_default_context()
    ctx.check_hostname = False
    ctx.set_ciphers('ADH:@SECLEVEL=0')
    api = connect(
        username='admin',
        password='abc',
        host='some.address.com',
        ssl_wrapper=ctx.wrap_socket,
        port=8729
        )

If you need to pass any other parameters like ``server_name``,
use `partial <https://docs.python.org/3/library/functools.html#functools.partial>`_.

.. code-block:: python

    from functools import partial
    ssl_wrapper=partial(ctx.wrap_socket, server_hostname='some.address.com')

Auth methods
------------

Starting from routeros ``6.43``, token auth method was replaced by plain text.
By default library will use plain text method. You can force library to use token method:

.. code-block:: python

    from librouteros.login import plain, token

    # for post 6.42 (plain text password)
    method = plain
    # for pre 6.43 (with token)
    method = token
    api = connect(username='admin', password='abc', host='some.address.com', login_method=method)

.. note::

    Library will not try different methods untill it will log in.