From 3873543fa67cca9d41058d13bd1ab172e98bdca7 Mon Sep 17 00:00:00 2001 From: Maxim Slipenko Date: Tue, 5 Sep 2023 09:14:32 +0300 Subject: [PATCH] wip --- main.py | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 50 insertions(+), 4 deletions(-) diff --git a/main.py b/main.py index 3ecef02..b4b647a 100644 --- a/main.py +++ b/main.py @@ -1,6 +1,10 @@ import httplib import hashlib import json +import argparse +# import ipaddress +import socket +import urllib class KeeneticAPI: @@ -23,6 +27,13 @@ class KeeneticAPI: data = res.read() return res, data + def _delete(self, url): + headers = {"Cookie": self._cookie} + self._conn.request("DELETE", url, "", 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") @@ -36,15 +47,50 @@ class KeeneticAPI: 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 + res, data = self._get("/rci/ip/route") + return json.loads(data) + def set_ip_route(self, route_data): + res, data = self._post("/rci/ip/route", json.dumps(route_data)) + return res.status == 200 -def main(): + def remove_ip_route(self, route): + res, _ = self._delete("/rci/ip/route?" + urllib.urlencode(route)) + return res.status == 200 + + def get_interfaces(self): + res, data = self._get("/rci/show/interface") + return json.loads(data, encoding='utf-8') + +def interfaces(args): api = KeeneticAPI() api.auth("test", "test") - api.show_ip_route() + ifaces = api.get_interfaces() + print "{:>30} {:>30}".format("ID", "DESCRIPTION") + + for iface in ifaces: + print "{:>30} {:>30}".format(iface, (ifaces[iface].get('description') or '-')) + +def main(): + parser = argparse.ArgumentParser(description="a script to do stuff") + subparsers = parser.add_subparsers() + + parser_foo = subparsers.add_parser('interfaces') + parser_foo.set_defaults(func=interfaces) + + args = parser.parse_args() + args.func(args) + + # api = KeeneticAPI() + # api.auth("test", "test") + # routes = api.show_ip_route() + + # for r in routes: + # print r['comment'] + # hostname_data = socket.gethostbyname_ex('grafana.com') + + # print hostname_data[2] if __name__ == "__main__":