98 lines
2.8 KiB
Python
98 lines
2.8 KiB
Python
import httplib
|
|
import hashlib
|
|
import json
|
|
import argparse
|
|
# import ipaddress
|
|
import socket
|
|
import urllib
|
|
|
|
|
|
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 _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")
|
|
|
|
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/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 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")
|
|
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__":
|
|
main()
|