Last active
February 28, 2026 08:03
-
-
Save ancientGlider/e72cdaa2daf0af5f8d80f53fea4666be to your computer and use it in GitHub Desktop.
Authentication for Keenetic routers for work with CLI via REST API (Python)
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 characters
| # -*- coding: utf-8 -*- | |
| """ | |
| Данные с адресом, логином, паролем хранятся в конфигурационном файле следующего вида: | |
| [Router] | |
| ip_addr = 192.168.1.1:8080 | |
| login = admin | |
| passw = anyPassword | |
| """ | |
| CONFIG_FILE_NAME = "keenetic.conf" # имя конфигурационного файла | |
| import configparser | |
| import requests | |
| import hashlib | |
| cookies_current = None | |
| session = requests.session() # заводим сессию глобально чтобы отрабатывались куки | |
| def keen_auth(login, passw): # авторизация на роутере | |
| response = keen_request(ip_addr, "auth") | |
| if response.status_code == 401: | |
| md5 = login + ":" + response.headers["X-NDM-Realm"] + ":" + passw | |
| md5 = hashlib.md5(md5.encode('utf-8')) | |
| sha = response.headers["X-NDM-Challenge"] + md5.hexdigest() | |
| sha = hashlib.sha256(sha.encode('utf-8')) | |
| response = keen_request(ip_addr, "auth", {"login": login, "password": sha.hexdigest()}) | |
| if response.status_code == 200: | |
| return True | |
| elif response.status_code == 200: | |
| return True | |
| else: | |
| return False | |
| def keen_request(ip_addr, query, post = None): # отправка запросов на роутер | |
| global session | |
| # конструируем url | |
| url = "http://" + ip_addr + "/" + query | |
| # если есть данные для запроса POST, делаем POST, иначе GET | |
| if post: | |
| return session.post(url, json=post) | |
| else: | |
| return session.get(url) | |
| config = configparser.ConfigParser() # создаём объекта парсера | |
| config.read(CONFIG_FILE_NAME) # читаем конфиг | |
| ip_addr = config["Router"]["ip_addr"] | |
| login = config["Router"]["login"] | |
| passw = config["Router"]["passw"] | |
| # тестируем | |
| if keen_auth(login, passw): | |
| response = keen_request(ip_addr, 'rci/show/interface/WifiMaster0'); | |
| print(response.text) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Ох уже столько времени прошло, уже не пользуюсь. Но у меня всё делалось через Raspberri PI и Home Assistant приложение умного дома. Там можно приконектить яндекс алису и алиса коллбечила как то в Home Assistant и можно было всё что угодно делать дальше уже по триггерам.