/
VinOgr
/
controller
Обзор
Документация
Войти
/
VinOgr
/
controller
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
main.py
84 строки
3 KB
denisE
26.02 2
26 фев 2026, 07:15
26 фев 2026, 07:15
3e2328f
Код
Авторство
О чём код?
# Работа с локальной базой доанных, считать фал лог. # потери связи, IP, время, количество. Найти имя контроллера по IP. # import re, sqlite3, datetime, linecache # ipaddress from smb.SMBConnection import SMBConnection # ip_address_str_v4 = '192.168.1.1' # intip = int(ipaddress.ip_address(ip_address_str_v4)) # print(intip) # strip = str(ipaddress.ip_address(intip)) # print(strip) def date_to_unix(date1, time1): datetime_string = date1 + ' ' + time1 datetime_obj = datetime.datetime.strptime(datetime_string, "%d.%m.%Y %H:%M:%S") return datetime_obj.timestamp() def get_ip_id(ipaddr): with sqlite3.connect("logdb.db") as condb: cursor = condb.cursor() # Устанавливаем курсор cursor.execute("SELECT key FROM IpAddr WHERE ip=?", (str(ipaddr),)) condb.commit() ipid = cursor.fetchone()[0] return ipid def last_record(): with sqlite3.connect("logdb.db") as condb: cursor = condb.cursor() # Устанавливаем курсор cursor.execute("SELECT * FROM CONTROLLER_LOG ORDER BY date DESC LIMIT 1") condb.commit() return (cursor.fetchone()) # Устанавливаем соединение с базой данных def add_record(utime, ip, messege): # print(unix_time) ipid = (get_ip_id(ip)) with sqlite3.connect("logdb.db") as condb: cursor = condb.cursor() # Устанавливаем курсор cursor.execute("INSERT INTO CONTROLLER_LOG (date, ip, messege) VALUES (?, ?, ?)", (utime, ipid, messege)) condb.commit() # четение логов # from dateutil.parser import parse # log_pattern = re.compile(r'\[(.*?)\] (.*?) (.*)') log_pattern = r"\d{2}.\d{2}.\d{4} \d{2}.\d{2}.\d{2} \d{1,3}.\d{1,3}.\d{1,3}.\d{1,3} Потеря" log_pattern2 = r"\d{2}.\d{2}.\d{4} \d{2}.\d{2}.\d{2} \d{1,3}.\d{1,3}.\d{1,3}.\d{1,3} Повтор" ip = re.compile(r'(\d{1,4}\.\d{1,4}\.\d{1,4}\.\d{1,4})') # find_ip = ip.search(content) #подключение по сети server_ip = "172.27.34.201" conn = ('administrator', 'njkmrjflvby', 'DENIS', 'TAI-serv1', use_ntlm_v2:=True) #conn.connect(server_ip, 139) #filelist = conn.listShares(timeout=30) # obtain a list of shares # последняя запись в базе lastrec = last_record() with open(r"\\172.27.34.201\c$\Horizont\ersocket.txt", "r", encoding='windows-1251') as file: # for _ in range(lastrec[0]): # print(lastrec[0]) while True: content = file.readline() if not content: break log_set = (content.split(maxsplit=3)) unix_time = int(date_to_unix(log_set[0], log_set[1])) print(unix_time, lastrec[2], log_set[0], log_set[1]) if unix_time > lastrec[2]: if re.match(log_pattern, content): add_record(unix_time, log_set[2], 1) if re.match(log_pattern2, content): add_record(unix_time, log_set[2], 2) file.close()