From edba34193695e15c7dcb7e24a3e5ba704ebb5d49 Mon Sep 17 00:00:00 2001 From: Mikhail Grebenkin Date: Thu, 28 Dec 2017 23:52:22 +0300 Subject: [PATCH] =?UTF-8?q?-=20=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=D0=B0=20=D0=BF=D0=BE=D0=B4=D0=B4=D0=B5=D1=80=D0=B6=D0=BA?= =?UTF-8?q?=D0=B0=20=D0=BA=D0=BE=D0=BD=D1=84=D0=B8=D0=B3=D0=B0=20=D0=B2=20?= =?UTF-8?q?web=5Fleases=20-=20=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=20=D1=84=D1=83=D0=BD=D0=BA=D1=86=D0=B8=D0=BE=D0=BD=D0=B0?= =?UTF-8?q?=D0=BB=20=D0=B2=D1=81=D1=8F=D1=87=D0=B5=D1=81=D0=BA=D0=B8=D0=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .idea/workspace.xml | 334 ++++++++++++++++++++++--------------- config-sample.json | 3 +- kea_host_wacher.py | 12 +- kea_hosts_generane.py | 14 +- hosts.tpl => tpl/hosts.tpl | 22 ++- tpl/import_hosts.tpl | 26 +++ main.tpl => tpl/main.tpl | 18 +- tpl/remove_hosts.tpl | 11 ++ web-leases.py | 137 ++++++++++++--- 9 files changed, 390 insertions(+), 187 deletions(-) rename hosts.tpl => tpl/hosts.tpl (53%) create mode 100644 tpl/import_hosts.tpl rename main.tpl => tpl/main.tpl (50%) create mode 100644 tpl/remove_hosts.tpl diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 68c3180..8e85932 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -1,9 +1,16 @@ - + + + + + + + + - print read_t + print + send_data @@ -105,20 +142,24 @@ - @@ -145,6 +186,11 @@ + + + + + - + + + %for a in A: - %for i in a: - + + + + + %end %end diff --git a/tpl/import_hosts.tpl b/tpl/import_hosts.tpl new file mode 100644 index 0000000..589e47d --- /dev/null +++ b/tpl/import_hosts.tpl @@ -0,0 +1,26 @@ + + + DHCP web-viewer + + + +

Таблица импорта обновлена.

+Если устройства не перезагружены автоматически, это необходимо сделать вручную. +
Таблица аренды статических адресов
IP адрес Имя хоста Mac адресадрес арендованарендованОсвободить
адрес
Удалить из
импорта
- %if i == '-': + {{a[0]}}{{a[1]}}{{a[2]}} + %if a[3]== '-': - - %elif i == '+': + %elif a[3] == '+': + - %elif '10.' in i: - {{i}} - %else: - {{i}} %end
+ + + + + + + %for a in A: + + + + + %end + +
Добавленные хосты
IP адресMac адресИмя хоста
{{a[0]}}{{a[1]}}{{a[2]}}
+Назад + + \ No newline at end of file diff --git a/main.tpl b/tpl/main.tpl similarity index 50% rename from main.tpl rename to tpl/main.tpl index e30e46d..e98573d 100644 --- a/main.tpl +++ b/tpl/main.tpl @@ -7,6 +7,8 @@ отчет по хостам в статическом адресном пространстве

Динамические адреса



+
+ @@ -14,14 +16,24 @@ + + + + %i = 0 %for a in A: + %i = i + 1 - %for i in a: - - %end + + + + + + + %end
Таблица аренды динамических адресов
IP адрес Mac адрес Имя хостаНовое имя№ сетиИмпортировать
{{i}}{{a[0]}}{{a[1]}}{{a[2]}}{{a[3]}}
+
diff --git a/tpl/remove_hosts.tpl b/tpl/remove_hosts.tpl new file mode 100644 index 0000000..db99a5c --- /dev/null +++ b/tpl/remove_hosts.tpl @@ -0,0 +1,11 @@ + + + DHCP web-viewer + + + +

Хосты удалены.

+Если устройства в сети, их нужно перезагрузить вручную. +Назад + + \ No newline at end of file diff --git a/web-leases.py b/web-leases.py index 697e694..9fb0935 100644 --- a/web-leases.py +++ b/web-leases.py @@ -1,10 +1,15 @@ #!/usr/bin/python3 # coding=utf-8 -from bottle import route, run, static_file, template +from bottle import route, run, static_file, template, post, request +import json import MySQLdb -HOST = 'localhost' + +def read_conf(): + with open('config.json') as f: + conf = json.load(f) + return conf @route('/static/') @@ -14,17 +19,18 @@ def server_static(filepath): @route('/') def server_homepage(): - dbhost = '10.5.1.248' - dbuser = 'kea' - dbpass = 'kea1234' - dbbase = 'dhcp4' + conf = read_conf() + dbhost = conf['dbhost'] + dbuser = conf['dbuser'] + dbpass = conf['dbpass'] + dbbase = conf['dbbase'] sql = "SELECT " \ " expire, " \ " INET_NTOA(address) as ip4, " \ " HEX(hwaddr)as hw_addr, " \ " hostname " \ " FROM lease4 " \ - "where INET_NTOA(address) like '10.5.0%'" \ + "where INET_NTOA(address) like '%.%.0.%'" \ " ORDER BY expire DESC;" try: conn = MySQLdb.connect(host=dbhost, user=dbuser, @@ -41,21 +47,62 @@ def server_homepage(): conn.close() data_list = [] for row in data: - temp = [] - temp.append(row['expire']) - temp.append(row['ip4']) - temp.append(row['hw_addr']) - temp.append(row['hostname']) + temp = [row['expire'], row['ip4'], row['hw_addr'], row['hostname']] data_list.append(temp) - return template('main.tpl', A=data_list) + return template('tpl/main.tpl', A=data_list) + + +@post('/') +def adding_hosts(): + conf = read_conf() + dbhost = conf['dbhost'] + dbuser = conf['dbuser'] + dbpass = conf['dbpass'] + dbbase = conf['dbbase'] + s_select = "SELECT " \ + " expire, " \ + " INET_NTOA(address) as ip4, " \ + " HEX(hwaddr)as hw_addr, " \ + " hostname " \ + " FROM lease4 " \ + "where INET_NTOA(address) like '10.100.0%'" \ + " ORDER BY expire DESC;" + s_insert = "insert into 1c_import (serial, net_num, hw_addr, imported) values ('%s', %s, '%s', 0);" + + i = 1 + data = [] + while request.POST.get("hostname_%s" % i, default=False): + hostname = request.POST.getall('hostname_%s' % i) + print(request.POST.getall('hostname_%s' % i)) + if len(hostname) == 3: + data.append(request.POST.getall('hostname_%s' % i)) + try: + conn = MySQLdb.connect(host=dbhost, user=dbuser, + passwd=dbpass, db=dbbase) + except MySQLdb.Error as err: + print("Connection error: {}".format(err)) + conn.close() + try: + cur = conn.cursor(MySQLdb.cursors.Cursor) + print(s_insert % tuple(hostname)) + cur.execute(s_insert % tuple(hostname)) + conn.commit() + except MySQLdb.Error as err: + print("Query error: {}".format(err)) + conn.close() + + i += 1 + + return template('tpl/import_hosts.tpl', A=data) @route('/hosts/') def server_hosts_repo(): - dbhost = '10.5.1.248' - dbuser = 'kea' - dbpass = 'kea1234' - dbbase = 'dhcp4' + conf = read_conf() + dbhost = conf['dbhost'] + dbuser = conf['dbuser'] + dbpass = conf['dbpass'] + dbbase = conf['dbbase'] sql = "SELECT " \ "INET_NTOA(hosts.ipv4_address) AS ip4," \ " hosts.hostname," \ @@ -67,9 +114,7 @@ def server_hosts_repo(): " lease4" \ " on" \ " dhcp_identifier = hwaddr " \ - "ORDER BY ip4;" \ -# "where " \ -# " INET_NTOA(hosts.ipv4_address) LIKE '10.5.1%';" + "ORDER BY ip4;" try: conn = MySQLdb.connect(host=dbhost, user=dbuser, passwd=dbpass, db=dbbase) @@ -91,8 +136,56 @@ def server_hosts_repo(): temp.append(row['hw_addr']) temp.append(row['leased']) data_list.append(temp) - print(data_list) - return template('hosts.tpl', A=data_list) + return template('tpl/hosts.tpl', A=data_list) + + +@post('/hosts/') +def remove_host(): + conf = read_conf() + dbhost = conf['dbhost'] + dbuser = conf['dbuser'] + dbpass = conf['dbpass'] + dbbase = conf['dbbase'] + s_remove1c = "delete from 1c_import where replace(hw_addr, ':', '') = '%s';" + s_removehost = "delete from hosts where dhcp_identifier = unhex('%s');" + hosts_del = request.forms.getall('remove_host') + del_1c = request.forms.getall('remove_import') + try: + conn = MySQLdb.connect(host=dbhost, user=dbuser, + passwd=dbpass, db=dbbase) + except MySQLdb.Error as err: + print("Connection error: {}".format(err)) + conn.close() + try: + cur = conn.cursor(MySQLdb.cursors.Cursor) + for host in hosts_del: + try: + cur.execute(s_removehost % host) + conn.commit() + except MySQLdb.Error as err: + print("Query error: {}".format(err)) + print(s_removehost % host) + for host in del_1c: + try: + cur.execute(s_removehost % host) + conn.commit() + except MySQLdb.Error as err: + print("Query error: {}".format(err)) + try: + cur.execute(s_remove1c % host) + conn.commit() + except MySQLdb.Error as err: + print("Query error: {}".format(err)) + + print(s_remove1c % host) + conn.commit() + conn.close() + except MySQLdb.Error as err: + print("Query error: {}".format(err)) + conn.close() + print(request.forms.getall('remove_host')) + print(request.forms.getall('remove_import')) + return template('tpl/remove_hosts.tpl') run(host='0.0.0.0', port=8080, debug=True)