import httplib import hashlib import json class KeeneticAPI: def __init__(self, destination="192.168.1.1"): self._conn = httplib.HTTPConnection(destination) self._cookie = "" pass def _get(self, url): headers = {"Cookie": self._cookie} self._conn.request("GET", url, "", headers) res = self._conn.getresponse() data = res.read() return res, data def _post(self, url, body): headers = {"Content-type": "application/json", "Cookie": self._cookie} self._conn.request("POST", url, body, headers) res = self._conn.getresponse() data = res.read() return res, data def auth(self, login, passw): res, _ = self._get("/auth") self._cookie = res.getheader("Set-Cookie") if res.status == 401: md5 = login + ":" + res.getheader("X-NDM-Realm") + ":" + passw md5 = hashlib.md5(md5.encode('utf-8')) sha = res.getheader("X-NDM-Challenge") + md5.hexdigest() sha = hashlib.sha256(sha.encode('utf-8')) self._post("/auth", json.dumps({"login": login, "password": sha.hexdigest()})) def show_ip_route(self): res, data = self._get("/rci/show/ip/route") print data def main(): api = KeeneticAPI() api.auth("test", "test") api.show_ip_route() if __name__ == "__main__": main()