diff --git a/.idea/runConfigurations/interfaces.xml b/.idea/runConfigurations/interfaces.xml
index d85606f..18e2662 100644
--- a/.idea/runConfigurations/interfaces.xml
+++ b/.idea/runConfigurations/interfaces.xml
@@ -12,7 +12,7 @@
-
+
diff --git a/.idea/runConfigurations/start.xml b/.idea/runConfigurations/start.xml
index d1f8447..6003451 100644
--- a/.idea/runConfigurations/start.xml
+++ b/.idea/runConfigurations/start.xml
@@ -12,7 +12,7 @@
-
+
diff --git a/main.py b/main.py
index ef60238..2b72f3c 100644
--- a/main.py
+++ b/main.py
@@ -49,7 +49,7 @@ class KeeneticAPI:
self._post("/auth", json.dumps({"login": login, "password": sha.hexdigest()}))
- def show_ip_route(self):
+ def get_ip_route(self):
res, data = self._get("/rci/ip/route")
return json.loads(data)
@@ -71,8 +71,13 @@ class KeeneticAPI:
def interfaces(args):
+ config = ConfigParser.ConfigParser()
+ config.read(args.config_file)
api = KeeneticAPI()
- api.auth("test", "test")
+ api.auth(
+ config.get('auth', 'login'),
+ config.get('auth', 'password')
+ )
ifaces = api.get_interfaces()
max_len = 0
@@ -81,18 +86,30 @@ def interfaces(args):
max_len = max(len(iface), max_len)
max_len = max(len(ifaces[iface].get('description') or '-'), max_len)
- print max_len
-
print "{:^{width}} {:^{width}}".format("ID", "DESCRIPTION", width=max_len)
for iface in ifaces:
print "{:^{width}} {:^{width}}".format(iface, (ifaces[iface].get('description') or '-'), width=max_len)
-
def start(args):
config = ConfigParser.ConfigParser()
config.read(args.config_file)
+ api = KeeneticAPI()
+ api.auth(
+ config.get('auth', 'login'),
+ config.get('auth', 'password')
+ )
+ old_routes = api.get_ip_route()
+
+ def filter_old_routes(item):
+ return item['comment'].startswith('[A]')
+
+ old_routes = list(filter(filter_old_routes, old_routes))
+
+ for route in old_routes:
+ api.remove_ip_route(route)
+
routes_dir = abspath(config.get('app', 'dir'))
routes = list()
@@ -125,10 +142,20 @@ def start(args):
else:
lst.append(ln)
+ if len(lst) > 0:
+ data.append(
+ (lst, comment)
+ )
+
for group in data:
hosts, description = group
for host in hosts:
- ipnetwork = IPNetwork(host)
+ if not host[0].isdigit():
+ ipnet = socket.gethostbyname(host)
+ else:
+ ipnet = host
+
+ ipnetwork = IPNetwork(ipnet)
routes.append(
{
@@ -147,35 +174,22 @@ def start(args):
)
api.set_ip_routes(routes)
- # for route in routes:
- # print route
-
def main():
parser = argparse.ArgumentParser(description="a script to do stuff")
+ parser.add_argument("--config-file", default="app.cfg")
subparsers = parser.add_subparsers()
parser_interfaces = subparsers.add_parser('interfaces')
parser_interfaces.set_defaults(func=interfaces)
parser_start = subparsers.add_parser('start')
- parser_start.add_argument("--config-file", default="app.cfg")
parser_start.set_defaults(func=start)
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()