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 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
|
Path object
===========
``Path`` object represents absolute command path within routeros. e.g. ``/ip/address``.
You can traverse down in tree with ``join()`` method.
Works same as python `join() <https://docs.python.org/3/library/os.path.html#os.path.join>`_.
.. code-block:: python
# First create desired path.
interfaces = api.path('interface')
# Traverse down into /interfaces/ethernet
ethernet = interfaces.join('ethernet')
# path() and join() accepts multiple arguments
ips = api.path('ip', 'address')
Get all
-------
.. code-block:: python
# Path objects are iterable
tuple(interfaces)
# This also will work, as well as anything else you can do with iterables
for item in interfaces:
print(item)
# async version
async for item in interfaces:
print(item)
# or you can use list comprehension
items = [item async for item in interfaces]
Add
---
.. code-block:: python
# Will return newly created .id
path.add(interface='ether1', address='172.31.31.1/24')
# async version
await path.add(interface='ether1', address='172.31.31.1/24')
Remove
------
.. code-block:: python
# Pass each .id as an argument.
path.remove('*1', '*2')
# async version
await path.remove('*1', '*2')
.. note::
``.id`` change on reboot. Always read them first.
Update
------
.. code-block:: python
params = {'disabled': True, '.id' :'*7'}
path.update(**params)
# async version
await path.update(**params)
.. note::
``.id`` change on reboot. Always read them first.
Arbitrary command
-----------------
For all other commands, call ``Path`` object directly.
Remember to consume the result since it returns a generator.
As a first argument, pass command that you wish to run without absolute path.
.. code-block:: python
script = api.path('system', 'script')
# Will run /system/script/run with desired .id
tuple(script('run', **{'.id': '*1'}))
# async version
[item async for item in script('run', **{'.id': '*1'})]
|