wip
This commit is contained in:
parent
3873543fa6
commit
94a1683771
5
.gitignore
vendored
5
.gitignore
vendored
@ -286,4 +286,7 @@ poetry.toml
|
|||||||
# LSP config files
|
# LSP config files
|
||||||
pyrightconfig.json
|
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
|
@ -4,7 +4,7 @@
|
|||||||
<content url="file://$MODULE_DIR$">
|
<content url="file://$MODULE_DIR$">
|
||||||
<excludeFolder url="file://$MODULE_DIR$/venv" />
|
<excludeFolder url="file://$MODULE_DIR$/venv" />
|
||||||
</content>
|
</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" />
|
<orderEntry type="sourceFolder" forTests="false" />
|
||||||
</component>
|
</component>
|
||||||
</module>
|
</module>
|
@ -1,4 +1,4 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
<project version="4">
|
<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>
|
</project>
|
23
.idea/runConfigurations/interfaces.xml
Normal file
23
.idea/runConfigurations/interfaces.xml
Normal 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>
|
23
.idea/runConfigurations/start.xml
Normal file
23
.idea/runConfigurations/start.xml
Normal 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>
|
94
main.py
94
main.py
@ -2,9 +2,12 @@ import httplib
|
|||||||
import hashlib
|
import hashlib
|
||||||
import json
|
import json
|
||||||
import argparse
|
import argparse
|
||||||
# import ipaddress
|
import os
|
||||||
import socket
|
import socket
|
||||||
import urllib
|
import urllib
|
||||||
|
import ConfigParser
|
||||||
|
from os.path import abspath
|
||||||
|
from netaddr import IPAddress, IPNetwork
|
||||||
|
|
||||||
|
|
||||||
class KeeneticAPI:
|
class KeeneticAPI:
|
||||||
@ -62,22 +65,103 @@ class KeeneticAPI:
|
|||||||
res, data = self._get("/rci/show/interface")
|
res, data = self._get("/rci/show/interface")
|
||||||
return json.loads(data, encoding='utf-8')
|
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):
|
def interfaces(args):
|
||||||
api = KeeneticAPI()
|
api = KeeneticAPI()
|
||||||
api.auth("test", "test")
|
api.auth("test", "test")
|
||||||
ifaces = api.get_interfaces()
|
ifaces = api.get_interfaces()
|
||||||
|
|
||||||
print "{:>30} {:>30}".format("ID", "DESCRIPTION")
|
max_len = 0
|
||||||
|
|
||||||
for iface in ifaces:
|
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():
|
def main():
|
||||||
parser = argparse.ArgumentParser(description="a script to do stuff")
|
parser = argparse.ArgumentParser(description="a script to do stuff")
|
||||||
subparsers = parser.add_subparsers()
|
subparsers = parser.add_subparsers()
|
||||||
|
|
||||||
parser_foo = subparsers.add_parser('interfaces')
|
parser_interfaces = subparsers.add_parser('interfaces')
|
||||||
parser_foo.set_defaults(func=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 = parser.parse_args()
|
||||||
args.func(args)
|
args.func(args)
|
||||||
|
1
requirements.txt
Normal file
1
requirements.txt
Normal file
@ -0,0 +1 @@
|
|||||||
|
netaddr~=0.8.0
|
Loading…
Reference in New Issue
Block a user