diff --git a/.gitignore b/.gitignore
index 4167464..a4964b6 100644
--- a/.gitignore
+++ b/.gitignore
@@ -286,4 +286,7 @@ poetry.toml
# LSP config files
pyrightconfig.json
-# End of https://www.toptal.com/developers/gitignore/api/python,pycharm
\ No newline at end of file
+# End of https://www.toptal.com/developers/gitignore/api/python,pycharm
+
+app.cfg.*
+vendor
\ No newline at end of file
diff --git a/.idea/keenetic-dpr-bypass.iml b/.idea/keenetic-dpr-bypass.iml
index 9455a52..fc10057 100644
--- a/.idea/keenetic-dpr-bypass.iml
+++ b/.idea/keenetic-dpr-bypass.iml
@@ -4,7 +4,7 @@
-
+
\ No newline at end of file
diff --git a/.idea/misc.xml b/.idea/misc.xml
index 7ba73c2..5a12aa4 100644
--- a/.idea/misc.xml
+++ b/.idea/misc.xml
@@ -1,4 +1,4 @@
-
+
\ No newline at end of file
diff --git a/.idea/runConfigurations/interfaces.xml b/.idea/runConfigurations/interfaces.xml
new file mode 100644
index 0000000..d85606f
--- /dev/null
+++ b/.idea/runConfigurations/interfaces.xml
@@ -0,0 +1,23 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/.idea/runConfigurations/start.xml b/.idea/runConfigurations/start.xml
new file mode 100644
index 0000000..d1f8447
--- /dev/null
+++ b/.idea/runConfigurations/start.xml
@@ -0,0 +1,23 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/app.cfg b/app.cfg
new file mode 100644
index 0000000..0219523
--- /dev/null
+++ b/app.cfg
@@ -0,0 +1,6 @@
+[auth]
+# login=test
+# password=test
+
+[app]
+dir=routes
\ No newline at end of file
diff --git a/main.py b/main.py
index b4b647a..ef60238 100644
--- a/main.py
+++ b/main.py
@@ -2,9 +2,12 @@ import httplib
import hashlib
import json
import argparse
-# import ipaddress
+import os
import socket
import urllib
+import ConfigParser
+from os.path import abspath
+from netaddr import IPAddress, IPNetwork
class KeeneticAPI:
@@ -62,22 +65,103 @@ class KeeneticAPI:
res, data = self._get("/rci/show/interface")
return json.loads(data, encoding='utf-8')
+ def set_ip_routes(self, routes):
+ res, _ = self._post("/rci/ip/route", json.dumps(routes))
+ return res.status == 200
+
+
def interfaces(args):
api = KeeneticAPI()
api.auth("test", "test")
ifaces = api.get_interfaces()
- print "{:>30} {:>30}".format("ID", "DESCRIPTION")
+ max_len = 0
for iface in ifaces:
- print "{:>30} {:>30}".format(iface, (ifaces[iface].get('description') or '-'))
+ 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)
+
+ routes_dir = abspath(config.get('app', 'dir'))
+
+ routes = list()
+
+ for filename in os.listdir(routes_dir):
+ fp = os.path.join(routes_dir, filename)
+
+ if not os.path.isfile(fp):
+ continue
+
+ interface_name = os.path.splitext(filename)[0]
+
+ data = list()
+
+ with open(fp) as f:
+ comment = ''
+ lst = list()
+ for line in f:
+ ln = line.strip()
+ if ln == '':
+ continue
+ if ln.startswith('#'):
+ if len(lst) > 0:
+ data.append(
+ (lst, comment)
+ )
+
+ comment = ln.lstrip('# ')
+ lst = list()
+ else:
+ lst.append(ln)
+
+ for group in data:
+ hosts, description = group
+ for host in hosts:
+ ipnetwork = IPNetwork(host)
+
+ routes.append(
+ {
+ 'network': str(ipnetwork.network),
+ 'mask': str(ipnetwork.netmask),
+ 'interface': interface_name,
+ 'auto': True,
+ 'comment': '[A] {}'.format(description)
+ }
+ )
+
+ api = KeeneticAPI()
+ api.auth(
+ config.get('auth', 'login'),
+ config.get('auth', 'password')
+ )
+ api.set_ip_routes(routes)
+
+ # for route in routes:
+ # print route
+
+
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)
+ 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)
diff --git a/requirements.txt b/requirements.txt
new file mode 100644
index 0000000..19f5ece
--- /dev/null
+++ b/requirements.txt
@@ -0,0 +1 @@
+netaddr~=0.8.0
\ No newline at end of file