-
-
Save tprelog/286096e8e204bb7301027fecdc46ff4b to your computer and use it in GitHub Desktop.
Revisions
-
tprelog revised this gist
Aug 3, 2021 . 2 changed files with 33 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,7 +1,13 @@ import argparse import requests import json import urllib3 import unifi_secrets HOST = unifi_secrets.unifi_host USER = unifi_secrets.unifi_username PASS = unifi_secrets.unifi_password UNIFI_LOGIN_PATH = '/api/auth/login' @@ -42,4 +48,27 @@ def request(self, path, data={}, method='POST'): except KeyError: pass return r def reboot(self): r = self.request("/api/system/reboot", method='POST') return r.ok def shutdown(self): r = self.request("/api/system/poweroff", method='POST') return r.ok if __name__ == "__main__": udm = Unifi(HOST,USER,PASS) udm.login() parser = argparse.ArgumentParser() parser.add_argument('command', help='reboot | shutdown') args = parser.parse_args() if args.command == 'reboot': udm.reboot() elif args.command == 'shutdown': udm.shutdown() This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,3 @@ unifi_host = '' unifi_username = '' unifi_password = '' -
Joe Stump created this gist
Jul 15, 2020 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,45 @@ import requests import json import urllib3 UNIFI_LOGIN_PATH = '/api/auth/login' # Disable SSL verification warnings urllib3.disable_warnings() class Unifi(object): def __init__(self, host, username, password): self.host = host self.username = username self.password = password self.session = requests.Session() self.csrf = "" def login(self): payload = { "username": self.username, "password": self.password, } r = self.request(UNIFI_LOGIN_PATH, payload) return r.ok def request(self, path, data={}, method='POST'): uri = 'https://{}{}'.format(self.host, path) headers = { "Accept": "application/json", "Content-Type": "application/json; charset=utf-8", } if self.csrf: headers["X-CSRF-Token"] = self.csrf r = getattr(self.session, method.lower())(uri, json=data, verify=False, headers=headers) try: self.csrf = r.headers['X-CSRF-Token'] except KeyError: pass return r