This commit is contained in:
Maxim Slipenko 2023-09-07 11:30:22 +03:00
parent 94a1683771
commit c801a0606f
3 changed files with 36 additions and 22 deletions

View File

@ -12,7 +12,7 @@
<option name="ADD_CONTENT_ROOTS" value="true" /> <option name="ADD_CONTENT_ROOTS" value="true" />
<option name="ADD_SOURCE_ROOTS" value="true" /> <option name="ADD_SOURCE_ROOTS" value="true" />
<option name="SCRIPT_NAME" value="$PROJECT_DIR$/main.py" /> <option name="SCRIPT_NAME" value="$PROJECT_DIR$/main.py" />
<option name="PARAMETERS" value="interfaces" /> <option name="PARAMETERS" value="--config-file app.cfg.dev interfaces" />
<option name="SHOW_COMMAND_LINE" value="false" /> <option name="SHOW_COMMAND_LINE" value="false" />
<option name="EMULATE_TERMINAL" value="false" /> <option name="EMULATE_TERMINAL" value="false" />
<option name="MODULE_MODE" value="false" /> <option name="MODULE_MODE" value="false" />

View File

@ -12,7 +12,7 @@
<option name="ADD_CONTENT_ROOTS" value="true" /> <option name="ADD_CONTENT_ROOTS" value="true" />
<option name="ADD_SOURCE_ROOTS" value="true" /> <option name="ADD_SOURCE_ROOTS" value="true" />
<option name="SCRIPT_NAME" value="$PROJECT_DIR$/main.py" /> <option name="SCRIPT_NAME" value="$PROJECT_DIR$/main.py" />
<option name="PARAMETERS" value="start --config-file app.cfg.dev" /> <option name="PARAMETERS" value="--config-file app.cfg.dev start" />
<option name="SHOW_COMMAND_LINE" value="false" /> <option name="SHOW_COMMAND_LINE" value="false" />
<option name="EMULATE_TERMINAL" value="false" /> <option name="EMULATE_TERMINAL" value="false" />
<option name="MODULE_MODE" value="false" /> <option name="MODULE_MODE" value="false" />

54
main.py
View File

@ -49,7 +49,7 @@ 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 get_ip_route(self):
res, data = self._get("/rci/ip/route") res, data = self._get("/rci/ip/route")
return json.loads(data) return json.loads(data)
@ -71,8 +71,13 @@ class KeeneticAPI:
def interfaces(args): def interfaces(args):
config = ConfigParser.ConfigParser()
config.read(args.config_file)
api = KeeneticAPI() api = KeeneticAPI()
api.auth("test", "test") api.auth(
config.get('auth', 'login'),
config.get('auth', 'password')
)
ifaces = api.get_interfaces() ifaces = api.get_interfaces()
max_len = 0 max_len = 0
@ -81,18 +86,30 @@ def interfaces(args):
max_len = max(len(iface), max_len) max_len = max(len(iface), max_len)
max_len = max(len(ifaces[iface].get('description') or '-'), 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) print "{:^{width}} {:^{width}}".format("ID", "DESCRIPTION", width=max_len)
for iface in ifaces: for iface in ifaces:
print "{:^{width}} {:^{width}}".format(iface, (ifaces[iface].get('description') or '-'), width=max_len) print "{:^{width}} {:^{width}}".format(iface, (ifaces[iface].get('description') or '-'), width=max_len)
def start(args): def start(args):
config = ConfigParser.ConfigParser() config = ConfigParser.ConfigParser()
config.read(args.config_file) 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_dir = abspath(config.get('app', 'dir'))
routes = list() routes = list()
@ -125,10 +142,20 @@ def start(args):
else: else:
lst.append(ln) lst.append(ln)
if len(lst) > 0:
data.append(
(lst, comment)
)
for group in data: for group in data:
hosts, description = group hosts, description = group
for host in hosts: for host in hosts:
ipnetwork = IPNetwork(host) if not host[0].isdigit():
ipnet = socket.gethostbyname(host)
else:
ipnet = host
ipnetwork = IPNetwork(ipnet)
routes.append( routes.append(
{ {
@ -147,35 +174,22 @@ def start(args):
) )
api.set_ip_routes(routes) 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")
parser.add_argument("--config-file", default="app.cfg")
subparsers = parser.add_subparsers() subparsers = parser.add_subparsers()
parser_interfaces = subparsers.add_parser('interfaces') parser_interfaces = subparsers.add_parser('interfaces')
parser_interfaces.set_defaults(func=interfaces) parser_interfaces.set_defaults(func=interfaces)
parser_start = subparsers.add_parser('start') parser_start = subparsers.add_parser('start')
parser_start.add_argument("--config-file", default="app.cfg")
parser_start.set_defaults(func=start) parser_start.set_defaults(func=start)
args = parser.parse_args() args = parser.parse_args()
args.func(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__":
main() main()