wip
This commit is contained in:
parent
485678d857
commit
3873543fa6
54
main.py
54
main.py
@ -1,6 +1,10 @@
|
|||||||
import httplib
|
import httplib
|
||||||
import hashlib
|
import hashlib
|
||||||
import json
|
import json
|
||||||
|
import argparse
|
||||||
|
# import ipaddress
|
||||||
|
import socket
|
||||||
|
import urllib
|
||||||
|
|
||||||
|
|
||||||
class KeeneticAPI:
|
class KeeneticAPI:
|
||||||
@ -23,6 +27,13 @@ class KeeneticAPI:
|
|||||||
data = res.read()
|
data = res.read()
|
||||||
return res, data
|
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):
|
def auth(self, login, passw):
|
||||||
res, _ = self._get("/auth")
|
res, _ = self._get("/auth")
|
||||||
self._cookie = res.getheader("Set-Cookie")
|
self._cookie = res.getheader("Set-Cookie")
|
||||||
@ -36,15 +47,50 @@ class KeeneticAPI:
|
|||||||
self._post("/auth", json.dumps({"login": login, "password": sha.hexdigest()}))
|
self._post("/auth", json.dumps({"login": login, "password": sha.hexdigest()}))
|
||||||
|
|
||||||
def show_ip_route(self):
|
def show_ip_route(self):
|
||||||
res, data = self._get("/rci/show/ip/route")
|
res, data = self._get("/rci/ip/route")
|
||||||
print data
|
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 = KeeneticAPI()
|
||||||
api.auth("test", "test")
|
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__":
|
if __name__ == "__main__":
|
||||||
|
Loading…
Reference in New Issue
Block a user