laiarturs-ros-api


Namelaiarturs-ros-api JSON
Version 1.1.0 PyPI version JSON
download
home_page
SummaryConnect to and use API interface of MikroTik RouterOS
upload_time2023-03-23 21:29:42
maintainer
docs_urlNone
authorArturs Laizans
requires_python>=3.4
licenseMIT License Copyright (c) [year] [fullname] Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
keywords mikrotik routeros router api
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # RouterOS API

Python API for MikroTik RouterOS. Simple and easy to use.

> **WARNING** for old users: 
> 
> Project has changes it's structure and import signature.

#### Features:
* Easy to use;
* Standard RouterOS API syntax;
* SSL;
* Verbose.

Find this project on [PyPI.org](https://pypi.org/project/laiarturs-ros-api/).

## Installation

```sh
python -m pip install laiarturs-ros-api
```

## Usage:

#### Default configuration:

*Python code:*
```python
import ros_api

router = ros_api.Api('192.168.88.1')
r = router.talk('/system/identity/print')
print(r)
```

*Output:*
```
[{'name': 'MikroTik'}]
```

#### Username, password, port:

*Python code:*
```python
import ros_api

router = ros_api.Api('10.21.0.100', user='Bob', password='St4ong0nE', port=15811)
r = router.talk('/ip/address/print')
print(r)
```

*Output:*
```
[{'.id': '*5', 'address': '10.21.0.100/24', 'network': '10.21.0.0','interface': 'ether1',
'actual-interface': 'ether1', 'invalid': 'false', 'dynamic': 'false', 'disabled': 'false'}]

```

#### SSL and verbose:

On RouterOS router create **certificate** and assign it to **api-ssl** service.

*RouterOS:*
```
/certificate
add name=ca-template common-name=myCa key-usage=key-cert-sign,crl-sign
add name=server-template common-name=server                           
sign ca-template ca-crl-host=10.21.0.100 name=myCa                         
sign server-template ca=myCa name=server

/ip service
set [find name=api-ssl] certificate=server
```
More info: [MikroTik Wiki](https://wiki.mikrotik.com/wiki/Manual:Create_Certificates).

*Python code:*
```python
import ros_api

router = ros_api.Api('10.21.0.100', user='SysAdmin', password='Meeseeks', verbose=True, use_ssl=True)
r = router.talk('/interface/wireless/enable\n=numbers=0')
print(r)
```

*Output:*
```
>>>  /login
>>>  =name=SysAdmin
>>>  =password=Meeseeks

<<<  !done

>>>  /interface/wireless/enable
>>>  =numbers=0

<<<  !done

[]
```

## How it works:
Python3 module *routeros_api.py* contains class *Api*. 
#### \_\_init__()
By initialising this class it creates socket, connects and logs in.
*Api* class *\_\_init__()* arguments:

Argument  | Description
----------|------------
`address` | `str` of IP address or host of RouterOS router on which it can be reached.
`user`    | `str` of username on router, *default='admin'*.
`password`| `str` of password of user on router, *default=''*.
`use_ssl` | `bool` whether to use SSL, *default=False*.
`port`    | `int` on which port to connect to router, *default=8728*, *ssl default=8729*.
`verbose` | `bool` whether to print conversation with router, *default=False*.
`context` | `ssl instance` for creating ssl connection, default is created, but it can be adjusted.
`timeout` | `float` in seconds to set timeout on socket blocking operations, *default=None*.

*Python code:*
```python
router = Api(address='192.168.10.1', user='Juri', password='L0vE$aun@', 
             use_ssl=True, port=8730, verbose=False, context=ctx, timeout=1)
```

#### talk()

To send commands to router use *talk()* method of *Api* class. *talk()* take one argument - message:

Argument  | Description
----------|------------
`message` | `str`, `tuple` or `list` of strings or tuples. It is possible to send multiple commands bundled in a list.

*Python code:*
```python
message = [('/system/note/set', '=note=Multi line\nnote for the Router!'), '/system/note/print']
r = router.talk(message)
print(r)
```
*Output:*
```
[[], [{'show-at-login': 'true', 'note': 'Multi line\nnote for the Router!'}]]
```

If property values you want to send to router contains spaces or linebreaks, sentence must be divided in words and then
passed to talk() as `tuple`. Otherwise you can send sentences as strings and it will be divided in words where there is 
space or linebreak.

Method *talk()* returns `list` containing replies from router. In this case there are two replies, because *message* 
contained two sentences. Actions like *set*, *add*, *enable* etc. usually returns empty list, however, *print*, *monitor*
and others returns `list` with `dict` inside containing reply from router.

Messages use RouterOS API syntax. More info: [MikroTik Wiki](https://wiki.mikrotik.com/wiki/Manual:API).

            

Raw data

            {
    "_id": null,
    "home_page": "",
    "name": "laiarturs-ros-api",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3.4",
    "maintainer_email": "",
    "keywords": "MikroTik,RouterOS,router,API",
    "author": "Arturs Laizans",
    "author_email": "",
    "download_url": "https://files.pythonhosted.org/packages/bc/3b/7622f44a7189dc3ec2131d2f09d807875ec596737f01910e3ee1b94c7b6b/laiarturs-ros_api-1.1.0.tar.gz",
    "platform": null,
    "description": "# RouterOS API\n\nPython API for MikroTik RouterOS. Simple and easy to use.\n\n> **WARNING** for old users: \n> \n> Project has changes it's structure and import signature.\n\n#### Features:\n* Easy to use;\n* Standard RouterOS API syntax;\n* SSL;\n* Verbose.\n\nFind this project on [PyPI.org](https://pypi.org/project/laiarturs-ros-api/).\n\n## Installation\n\n```sh\npython -m pip install laiarturs-ros-api\n```\n\n## Usage:\n\n#### Default configuration:\n\n*Python code:*\n```python\nimport ros_api\n\nrouter = ros_api.Api('192.168.88.1')\nr = router.talk('/system/identity/print')\nprint(r)\n```\n\n*Output:*\n```\n[{'name': 'MikroTik'}]\n```\n\n#### Username, password, port:\n\n*Python code:*\n```python\nimport ros_api\n\nrouter = ros_api.Api('10.21.0.100', user='Bob', password='St4ong0nE', port=15811)\nr = router.talk('/ip/address/print')\nprint(r)\n```\n\n*Output:*\n```\n[{'.id': '*5', 'address': '10.21.0.100/24', 'network': '10.21.0.0','interface': 'ether1',\n'actual-interface': 'ether1', 'invalid': 'false', 'dynamic': 'false', 'disabled': 'false'}]\n\n```\n\n#### SSL and verbose:\n\nOn RouterOS router create **certificate** and assign it to **api-ssl** service.\n\n*RouterOS:*\n```\n/certificate\nadd name=ca-template common-name=myCa key-usage=key-cert-sign,crl-sign\nadd name=server-template common-name=server                           \nsign ca-template ca-crl-host=10.21.0.100 name=myCa                         \nsign server-template ca=myCa name=server\n\n/ip service\nset [find name=api-ssl] certificate=server\n```\nMore info: [MikroTik Wiki](https://wiki.mikrotik.com/wiki/Manual:Create_Certificates).\n\n*Python code:*\n```python\nimport ros_api\n\nrouter = ros_api.Api('10.21.0.100', user='SysAdmin', password='Meeseeks', verbose=True, use_ssl=True)\nr = router.talk('/interface/wireless/enable\\n=numbers=0')\nprint(r)\n```\n\n*Output:*\n```\n>>>  /login\n>>>  =name=SysAdmin\n>>>  =password=Meeseeks\n\n<<<  !done\n\n>>>  /interface/wireless/enable\n>>>  =numbers=0\n\n<<<  !done\n\n[]\n```\n\n## How it works:\nPython3 module *routeros_api.py* contains class *Api*. \n#### \\_\\_init__()\nBy initialising this class it creates socket, connects and logs in.\n*Api* class *\\_\\_init__()* arguments:\n\nArgument  | Description\n----------|------------\n`address` | `str` of IP address or host of RouterOS router on which it can be reached.\n`user`    | `str` of username on router, *default='admin'*.\n`password`| `str` of password of user on router, *default=''*.\n`use_ssl` | `bool` whether to use SSL, *default=False*.\n`port`    | `int` on which port to connect to router, *default=8728*, *ssl default=8729*.\n`verbose` | `bool` whether to print conversation with router, *default=False*.\n`context` | `ssl instance` for creating ssl connection, default is created, but it can be adjusted.\n`timeout` | `float` in seconds to set timeout on socket blocking operations, *default=None*.\n\n*Python code:*\n```python\nrouter = Api(address='192.168.10.1', user='Juri', password='L0vE$aun@', \n             use_ssl=True, port=8730, verbose=False, context=ctx, timeout=1)\n```\n\n#### talk()\n\nTo send commands to router use *talk()* method of *Api* class. *talk()* take one argument - message:\n\nArgument  | Description\n----------|------------\n`message` | `str`, `tuple` or `list` of strings or tuples. It is possible to send multiple commands bundled in a list.\n\n*Python code:*\n```python\nmessage = [('/system/note/set', '=note=Multi line\\nnote for the Router!'), '/system/note/print']\nr = router.talk(message)\nprint(r)\n```\n*Output:*\n```\n[[], [{'show-at-login': 'true', 'note': 'Multi line\\nnote for the Router!'}]]\n```\n\nIf property values you want to send to router contains spaces or linebreaks, sentence must be divided in words and then\npassed to talk() as `tuple`. Otherwise you can send sentences as strings and it will be divided in words where there is \nspace or linebreak.\n\nMethod *talk()* returns `list` containing replies from router. In this case there are two replies, because *message* \ncontained two sentences. Actions like *set*, *add*, *enable* etc. usually returns empty list, however, *print*, *monitor*\nand others returns `list` with `dict` inside containing reply from router.\n\nMessages use RouterOS API syntax. More info: [MikroTik Wiki](https://wiki.mikrotik.com/wiki/Manual:API).\n",
    "bugtrack_url": null,
    "license": "MIT License  Copyright (c) [year] [fullname]  Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the \"Software\"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:  The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.  THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ",
    "summary": "Connect to and use API interface of MikroTik RouterOS",
    "version": "1.1.0",
    "split_keywords": [
        "mikrotik",
        "routeros",
        "router",
        "api"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "c69fb33e70b42d5ec212de25e7ee89e642ef1ef1f70c3a9467992a5f8b3efa7e",
                "md5": "ec218eee93d429a826c0367503bfec9f",
                "sha256": "1be16c22011e2c8ee4c279b5571ab597e0179778c342db5771da62cca1719f7b"
            },
            "downloads": -1,
            "filename": "laiarturs_ros_api-1.1.0-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "ec218eee93d429a826c0367503bfec9f",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.4",
            "size": 9186,
            "upload_time": "2023-03-23T21:29:39",
            "upload_time_iso_8601": "2023-03-23T21:29:39.558591Z",
            "url": "https://files.pythonhosted.org/packages/c6/9f/b33e70b42d5ec212de25e7ee89e642ef1ef1f70c3a9467992a5f8b3efa7e/laiarturs_ros_api-1.1.0-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "bc3b7622f44a7189dc3ec2131d2f09d807875ec596737f01910e3ee1b94c7b6b",
                "md5": "ed6ebfdbb8449ccdce33a1dfdafbbba7",
                "sha256": "f2439e21eaca37dc9b05a4bedbab7ff2034f8a74f20a3b4f0a7448b53d44fec7"
            },
            "downloads": -1,
            "filename": "laiarturs-ros_api-1.1.0.tar.gz",
            "has_sig": false,
            "md5_digest": "ed6ebfdbb8449ccdce33a1dfdafbbba7",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.4",
            "size": 8051,
            "upload_time": "2023-03-23T21:29:42",
            "upload_time_iso_8601": "2023-03-23T21:29:42.467462Z",
            "url": "https://files.pythonhosted.org/packages/bc/3b/7622f44a7189dc3ec2131d2f09d807875ec596737f01910e3ee1b94c7b6b/laiarturs-ros_api-1.1.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-03-23 21:29:42",
    "github": false,
    "gitlab": false,
    "bitbucket": false,
    "lcname": "laiarturs-ros-api"
}
        
Elapsed time: 0.05836s