Giter Club home page Giter Club logo

unifiapi's Introduction

Unifi API library and examples

ALPHA API tool. This is still very perliminary and much documentation has yet to be written. This is written for python 3 but I will make every attempt to keep it working under python 2.

At the heart of this are some very simple RESTful wrappers. The UnifiClient base implements get, put, post, and delete calls while adding whatever prefix they were started with. The controller has no prefix and the site has api/s/{sitename}. The return values from the API are wrapped to preseve fidelety of the data and meta. The response object is a UserList so it acts like a list most of the time but has additional properties. It can be indexed with strings and it will try to do the right thing ( see sites['default'] in examples ). There is a meta property which has the full meta returned by the server.

import unifiapi
c = unifiapi.controller(endpoint='https://unifi:8443', usernane='ubnt', password='ubnt', verify=False)
s = c.sites['default']()
s.devices()

For simple access please define a unifiapi.yaml file in the cwd or ~/.unifiapi_yaml with the following syntax

default:
  endpoint: 'https://unifi.mydomain.com'
  username: 'myusername'
  password: '12345'
  verify: False
other:
  endpoint: 'https://10.11.12.13:8443'
  username: 'bob'
  password: '42'
  verify: True

Examples

Toggle syn cookies.

>>> from unifiapi import controller
>>> # Get controller object
>>> c = controller(endpoint='https://192.168.111.20:8443', verify=False)
Please enter credentials for Unifi https://192.168.111.20:8443
Username (CR=erice): foo
foo Password :
>>> s = c.sites[0]()
>>> settings = s.settings()
>>> settings['dpi']
{'_id': '5ad52945be0777002184bc49', 'enabled': True, 'key': 'dpi', 'site_id': '5ad52944be0777002184bc41'}
>>> settings['usg']
{'_id': '5ad52945be0777002184bc4a', 'broadcast_ping': False, 'echo_server': 'ping.ubnt.com', 'ftp_module': True, 'gre_module': True, 'h323_module': True, 'key': 'usg', 'lldp_enable_all': True, 'mdns_enabled': True, 'mss_clamp': 'auto', 'mss_clamp_mss': 1452, 'offload_accounting': True, 'offload_l2_blocking': True, 'offload_sch': True, 'pptp_module': True, 'receive_redirects': False, 'send_redirects': True, 'sip_module': False, 'site_id': '5ad52944be0777002184bc41', 'syn_cookies': True, 'tftp_module': True, 'upnp_enabled': True, 'upnp_nat_pmp_enabled': True, 'upnp_secure_mode': True, 'upnp_wan_interface': 'wan'}
>>> settings['usg']['syn_cookies'] = False
>>> settings['usg'].update()
[{'_id': '5ad52945be0777002184bc4a', 'broadcast_ping': False, 'echo_server': 'ping.ubnt.com', 'ftp_module': True, 'gre_module': True, 'h323_module': True, 'key': 'usg', 'lldp_enable_all': True, 'mdns_enabled': True, 'mss_clamp': 'auto', 'mss_clamp_mss': 1452, 'offload_accounting': True, 'offload_l2_blocking': True, 'offload_sch': True, 'pptp_module': True, 'receive_redirects': False, 'send_redirects': True, 'sip_module': False, 'site_id': '5ad52944be0777002184bc41', 'syn_cookies': False, 'tftp_module': True, 'upnp_enabled': True, 'upnp_nat_pmp_enabled': True, 'upnp_secure_mode': True, 'upnp_wan_interface': 'wan'}]
>>> settings['usg']['syn_cookies'] = True
>>> settings['usg'].update()
[{'_id': '5ad52945be0777002184bc4a', 'broadcast_ping': False, 'echo_server': 'ping.ubnt.com', 'ftp_module': True, 'gre_module': True, 'h323_module': True, 'key': 'usg', 'lldp_enable_all': True, 'mdns_enabled': True, 'mss_clamp': 'auto', 'mss_clamp_mss': 1452, 'offload_accounting': True, 'offload_l2_blocking': True, 'offload_sch': True, 'pptp_module': True, 'receive_redirects': False, 'send_redirects': True, 'sip_module': False, 'site_id': '5ad52944be0777002184bc41', 'syn_cookies': True, 'tftp_module': True, 'upnp_enabled': True, 'upnp_nat_pmp_enabled': True, 'upnp_secure_mode': True, 'upnp_wan_interface': 'wan'}]

List backups

>>> backups = s.c_backups()
>>> for item in backups:
...     print("{datetime} {filename} {size}".format(**item))
...
2018-05-06T00:00:00Z autobackup_5.7.23_20180506_0000_1525564800026.unf 13915648
2018-05-13T00:00:00Z autobackup_5.7.23_20180513_0000_1526169600008.unf 22216336
2018-05-19T00:00:00Z autobackup_5.7.23_20180519_0000_1526688000016.unf 24923824
2018-05-20T00:00:00Z autobackup_5.7.23_20180520_0000_1526774400013.unf 25360096
2018-05-21T00:00:00Z autobackup_5.7.23_20180521_0000_1526860800007.unf 25751888
2018-05-22T00:00:00Z autobackup_5.7.23_20180522_0000_1526947200012.unf 26076656
2018-05-23T00:00:00Z autobackup_5.7.23_20180523_0000_1527033600013.unf 26448416
2018-05-24T00:00:00Z autobackup_5.7.23_20180524_0000_1527120000007.unf 26862720
2018-05-25T00:00:00Z autobackup_5.7.23_20180525_0000_1527206400013.unf 27250960
2018-05-26T00:00:00Z autobackup_5.7.23_20180526_0000_1527292800011.unf 27546816
2018-05-27T00:00:00Z autobackup_5.7.23_20180527_0000_1527379200013.unf 28005568

Show the temperatures of all units with sensors

>>> devs = s.devices()
>>> for item in devs.filter_by('has_temperature', True):
...     print('{ip} - {general_temperature}C'.format(**item))
...
10.11.10.6 - 58C

unifiapi's People

Contributors

brontide avatar

Watchers

James Cloos avatar Cazzar 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.