Package: laiarturs-ros-api
RouterOS APIPython API for MikroTik RouterOS. Simple and easy to use.WARNINGfor old users:Project has changes it's structure and import signature.Features:Easy to use;Standard RouterOS API syntax;SSL;Verbose.Find this project onPyPI.org.Installationpython-mpipinstalllaiarturs-ros-apiUsage:Default configuration:Python code:importros_apirouter=ros_api.Api('192.168.88.1')r=router.talk('/system/identity/print')print(r)Output:[{'name': 'MikroTik'}]Username, password, port:Python code:importros_apirouter=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 createcertificateand assign it toapi-sslservice.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=serverMore info:MikroTik Wiki.Python code:importros_apirouter=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 modulerouteros_api.pycontains classApi.__init__()By initialising this class it creates socket, connects and logs in.Apiclass__init__()arguments:ArgumentDescriptionaddressstrof IP address or host of RouterOS router on which it can be reached.userstrof username on router,default='admin'.passwordstrof password of user on router,default=''.use_sslboolwhether to use SSL,default=False.portinton which port to connect to router,default=8728,ssl default=8729.verboseboolwhether to print conversation with router,default=False.contextssl instancefor creating ssl connection, default is created, but it can be adjusted.timeoutfloatin seconds to set timeout on socket blocking operations,default=None.Python code: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 usetalk()method ofApiclass.talk()take one argument - message:ArgumentDescriptionmessagestr,tupleorlistof strings or tuples. It is possible to send multiple commands bundled in a list.Python code: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() astuple. Otherwise you can send sentences as strings and it will be divided in words where there is
space or linebreak.Methodtalk()returnslistcontaining replies from router. In this case there are two replies, becausemessagecontained two sentences. Actions likeset,add,enableetc. usually returns empty list, however,print,monitorand others returnslistwithdictinside containing reply from router.Messages use RouterOS API syntax. More info:MikroTik Wiki.