This commit is contained in:
Maxim Slipenko 2023-09-06 15:13:28 +03:00
parent 3873543fa6
commit 94a1683771
8 changed files with 148 additions and 8 deletions

5
.gitignore vendored
View File

@ -286,4 +286,7 @@ poetry.toml
# LSP config files
pyrightconfig.json
# End of https://www.toptal.com/developers/gitignore/api/python,pycharm
# End of https://www.toptal.com/developers/gitignore/api/python,pycharm
app.cfg.*
vendor

View File

@ -4,7 +4,7 @@
<content url="file://$MODULE_DIR$">
<excludeFolder url="file://$MODULE_DIR$/venv" />
</content>
<orderEntry type="jdk" jdkName="Python 2.7" jdkType="Python SDK" />
<orderEntry type="jdk" jdkName="Python 2.7 (keenetic-dpr-bypass)" jdkType="Python SDK" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

View File

@ -1,4 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" project-jdk-name="Python 2.7" project-jdk-type="Python SDK" />
<component name="ProjectRootManager" version="2" project-jdk-name="Python 2.7 (keenetic-dpr-bypass)" project-jdk-type="Python SDK" />
</project>

View File

@ -0,0 +1,23 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="interfaces" type="PythonConfigurationType" factoryName="Python" folderName="commands">
<module name="keenetic-dpr-bypass" />
<option name="INTERPRETER_OPTIONS" value="" />
<option name="PARENT_ENVS" value="true" />
<envs>
<env name="PYTHONUNBUFFERED" value="1" />
</envs>
<option name="SDK_HOME" value="" />
<option name="WORKING_DIRECTORY" value="$PROJECT_DIR$" />
<option name="IS_MODULE_SDK" value="true" />
<option name="ADD_CONTENT_ROOTS" value="true" />
<option name="ADD_SOURCE_ROOTS" value="true" />
<option name="SCRIPT_NAME" value="$PROJECT_DIR$/main.py" />
<option name="PARAMETERS" value="interfaces" />
<option name="SHOW_COMMAND_LINE" value="false" />
<option name="EMULATE_TERMINAL" value="false" />
<option name="MODULE_MODE" value="false" />
<option name="REDIRECT_INPUT" value="false" />
<option name="INPUT_FILE" value="" />
<method v="2" />
</configuration>
</component>

View File

@ -0,0 +1,23 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="start" type="PythonConfigurationType" factoryName="Python" folderName="commands">
<module name="keenetic-dpr-bypass" />
<option name="INTERPRETER_OPTIONS" value="" />
<option name="PARENT_ENVS" value="true" />
<envs>
<env name="PYTHONUNBUFFERED" value="1" />
</envs>
<option name="SDK_HOME" value="" />
<option name="WORKING_DIRECTORY" value="$PROJECT_DIR$" />
<option name="IS_MODULE_SDK" value="true" />
<option name="ADD_CONTENT_ROOTS" value="true" />
<option name="ADD_SOURCE_ROOTS" value="true" />
<option name="SCRIPT_NAME" value="$PROJECT_DIR$/main.py" />
<option name="PARAMETERS" value="start --config-file app.cfg.dev" />
<option name="SHOW_COMMAND_LINE" value="false" />
<option name="EMULATE_TERMINAL" value="false" />
<option name="MODULE_MODE" value="false" />
<option name="REDIRECT_INPUT" value="false" />
<option name="INPUT_FILE" value="" />
<method v="2" />
</configuration>
</component>

6
app.cfg Normal file
View File

@ -0,0 +1,6 @@
[auth]
# login=test
# password=test
[app]
dir=routes

94
main.py
View File

@ -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)

1
requirements.txt Normal file
View File

@ -0,0 +1 @@
netaddr~=0.8.0