Giter Site home page Giter Site logo

python-ubus's Introduction

Python interface for Ubus

This package provides a pythonic interface for accessing ubus <https://wiki.openwrt.org/doc/techref/ubus> over its separately packaged JSON-RPC interface.

Library use

Ubus class is the entrypoint to access all functionalities of this package, which provides an access to the available interfaces:

ubus = Ubus('router.local')
for iface in ubus.interfaces:
    print(iface)

Besides iterator interface the interfaces can be accessed with a dict-style accessors, which also return UbusNamespace objects for accessing the underlying methods.

dhcp = ubus['dhcp']
for method in dhcp:
     print(method)

UbusMethod class implement __call__ to allow easy execution of methods:

v4leases = dhcp['ipv4leases']()

Where required, the parameters are passed as dictionaries:

ubus['session']['login'](username='user', password='1234')

Context manager support

Ubus constructor accepts username and password as parameters, which in turn allows using the class as a context-manager to simplify session handling:

with Ubus('router.local', 'user', '1234') as ubus:
    wlan0_devices = ubus['iwinfo']['assoclist'](device='wlan0')

Command-line utility

There is also a simple command-line interface (similar to ubus command-line tool) called pyubus, which enables running arbitrary commands from the console. When no parameters are given it will output the list of available namespaces (calls implicitly list command, more information in --help). All unknown commands are interpreted as namespaces names, so listing all available methods in namespace 'dhcp' can be done by simply calling:

$ pyubus dhcp

Methods can be directly executed by passing their name, and potential parameters in key=value format to the utility:

$ pyubus iwinfo assoclist device=wlan0

Configuration can be either passed as options or through creating an ini-style configuration file in ~/.config/python-ubus.ini:

[ubus]
host = router.local
username = user
password = 1234

python-ubus's People

Contributors

rytilahti avatar sasquatchrus avatar

Watchers

James Cloos avatar  avatar

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.