Coder Social home page Coder Social logo

cykyy / pyros-api Goto Github PK

View Code? Open in Web Editor NEW
3.0 1.0 2.0 31 KB

pyros-api is a simple python API for MikroTik RouterOS.

License: MIT License

Python 100.00%
routeros routeros-api mikrotik-api mikrotik mikrotik-routeros-api mikrotik-routeros-script mikrotik-hotspot python

pyros-api's Introduction

pyros-api

A simplified routerOS api in python!

Thanks to Social WiFi for their incredible RouterOS-api

pyros-api is a simple python api for MikroTik RouterOS extended from RouterOS-api.
pyros-api on PyPi

Why Another API?

Well, I find the routeros-api by socialwifi (their api is great!) is complicated unless you are well familiar with routerOS.
Though I was somewhat familiar with routerOS I still needed to often search on google and play with winbox/cli to do a simple stuff.
And the most embarrassing part is I used to often forget what I did to achieve something let's say a day ago If I deleted or needed to implement the same logic with a bit of twist.
That's where this wrapper comes in. There's not many functions are covered so far but I will be working on this and will update with more simplified api. Any contribution is welcome.

Usage

pip install pyros-api

Connection

#!/usr/bin/python

import pyros_api

connection = pyros_api.RosCall('Mikrotik IP', username='admin', password='')
connection.login()
connection.get_ppp_secret()

Connect Options

pyros_api.RosCall(
    host,
    username='admin',
    password='',
    port=8728,
    use_ssl=False,
    ssl_verify=True,
    ssl_verify_hostname=True,
    ssl_context=None,
)

Parameters:

  • host - String - Hostname or IP of device

Optional Parameters:

  • username - String - Login username - Default 'admin'
  • password - String - Login password - Default empty string
  • port - Integer - TCP Port for API - Default 8728 or 8729 when using SSL
  • plaintext_login - Boolean - Try plaintext login (for RouterOS 6.43 onwards) - Default False
  • use_ssl - Boolean - Use SSL or not? - Default False
  • ssl_verify - Boolean - Verify the SSL certificate? - Default True
  • ssl_verify_hostname - Boolean - Verify the SSL certificate hostname matches? - Default True
  • ssl_context - Object - Pass in a custom SSL context object. Overrides other options. - Default None

Using SSL

If we want to use SSL, we can simply specify use_ssl as True:

connection = pyros_api.RosCall('<IP>', username='admin', password='', use_ssl=True)
connection.login()

This will automatically verify SSL certificate and hostname. The most flexible way to modify SSL parameters is to provide an SSL Context object using the ssl_context parameter, but for typical use-cases with self-signed certificates, the shorthand options of ssl_verify and ssl_verify_hostname are provided.

e.g. if using a self-signed certificate, you can (but probably shouldn't) use:

connection = pyros_api.RosCall(
    '<IP>',
    username='admin',
    password='',
    use_ssl=True,
    ssl_verify=False,
    ssl_verify_hostname=False,
)

Login for RouterOS v6.43 onwards

RouterOS Versions v6.43 onwards now use a different login method. The disadvantage is that it passes the password in plain text. For security we only attempt the plaintext login if requested using the plaintext_login parameter. It is highly recommended only to use this option with SSL enabled.

pyros_api.RosCall(host, username='admin', password='', plaintext_login=True)
connection.login()

Execute Commands

After successfully connecting with routerOS you can call all the available functions.

Examples

x = connection.get_ppp_secret()
print(x)  # print list of all ppp secrets from routerOS
# changes password of given ppp secret. e.g.: secret = 'abc1 & password = '1234'
x = connection.update_secret_password(secret, password)

Create New PPP Secret

secret = {
    'c_ident': '',
    'p_pw': '',
    'profile': 'default',
    'service_type': 'pppoe',
    'comment': '',
    'has_suspended': False
 }

Secret Dictionary Key-Value Pair:

  • c_ident - String - PPP secret name (e.g: abc1) - Default empty string

Optional Keys:

  • p_pw - String - PPP secret password - Default empty string
  • profile - String - PPP secret profile - Default 'default' profile
  • service_type - String - PPP secret service type (e.g: pptp/any/pppe) - Default pppoe
  • comment - String - PPP secret comment - Default empty string
  • has_suspended - Boolean - PPP secret state after creation (e.g: if True then after creating the secret the ppp secret will be disabled) - Default False
Example
secret = {
    'c_ident': 'abc5',
    'p_pw': '1234',
    'profile': 'default',
    'service_type': 'pppoe',
    'comment': 'This is a dummy comment!',
    'has_suspended': False
}
connection.add_ppp_secret(secret)  # returns True if successfully created

Close conection:

connection.disconnect()

socialWifi's routerOS-api API's

Everything from the routerOS-api by socialWifi is also available by invoking the given function.

api = connection.ros_api_raw()

Now we can access all the functions from the routerOS-api by socialWifi.

Example

api = connection.ros_api_raw()
list_ppp = api.get_resource('/ppp/secret')
print(list_ppp.get())  # prints all ppp secrets

To learn more about how to access API's from RouterOS-api by Social WiFi please visit their repository.

Any contribution is welcome! Thanks.

pyros-api's People

Contributors

cykyy avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar

Forkers

essam7389 samy440

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.