init
This commit is contained in:
commit
f4a511ca44
12 files changed
+159
No files matched your search
@@ -0,0 +1,92 @@
|
||||
import fnmatch
|
||||
import logging
|
||||
import os
|
||||
import sqlite3
|
||||
from datetime import date
|
||||
|
||||
import paramiko
|
||||
from dotenv import load_dotenv
|
||||
|
||||
load_dotenv()
|
||||
|
||||
|
||||
class RegisteredUser:
|
||||
tid: str
|
||||
username: str
|
||||
password: str
|
||||
|
||||
class AnalyticData:
|
||||
memory: str
|
||||
containers: list
|
||||
|
||||
logging.basicConfig(filename="app.log",
|
||||
level=logging.INFO,
|
||||
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s",
|
||||
encoding="utf-8")
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
def find_latest_file(
|
||||
sftp,
|
||||
remote_dir: str,
|
||||
pattern: str = "*",
|
||||
) -> str | None:
|
||||
try:
|
||||
entries = sftp.listdir_attr(remote_dir)
|
||||
except FileNotFoundError:
|
||||
return None
|
||||
|
||||
candidates = [
|
||||
e for e in entries
|
||||
if fnmatch.fnmatch(e.filename, pattern)
|
||||
and not e.filename.startswith(".")
|
||||
]
|
||||
if not candidates:
|
||||
return None
|
||||
|
||||
# Самый свежий по времени модификации
|
||||
latest = max(candidates, key=lambda e: e.st_mtime)
|
||||
return f"{remote_dir.rstrip('/')}/{latest.filename}"
|
||||
|
||||
|
||||
def get_user_by_tid(tid: str) -> RegisteredUser:
|
||||
try:
|
||||
db = sqlite3.connect("app.db")
|
||||
cursor = db.cursor()
|
||||
cursor.execute(f"SELECT * FROM telegram_users WHERE tid = '{tid}'")
|
||||
user: list[RegisteredUser] = cursor.fetchall()
|
||||
if user.count == 0:
|
||||
return "(Пользователь не найден)"
|
||||
return user[0]
|
||||
finally:
|
||||
db.close()
|
||||
|
||||
def execute_server_audit(user: str, pwd: str) -> None:
|
||||
try:
|
||||
client = paramiko.SSHClient()
|
||||
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
|
||||
client.connect(hostname=os.getenv('HOST_SERVER'), username=user, password=pwd, timeout=60, banner_timeout=30)
|
||||
client.exec_command("cd /usr/audit && sudo ./server_audit.sh")
|
||||
finally:
|
||||
client.close()
|
||||
|
||||
def remove_audit_file(user: str, pwd: str):
|
||||
try:
|
||||
client = paramiko.SSHClient()
|
||||
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
|
||||
client.connect(hostname=os.getenv('HOST_SERVER'), username=user, password=pwd, timeout=60, banner_timeout=30)
|
||||
client.exec_command("cd /usr/audit && sudo rm -- *.txt")
|
||||
finally:
|
||||
client.close()
|
||||
|
||||
def create_download_link(user: str, pwd: str) -> None:
|
||||
try:
|
||||
with paramiko.SSHClient() as client:
|
||||
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
|
||||
client.connect(hostname=os.getenv('HOST_SERVER'), username=user, password=pwd, timeout=60, banner_timeout=30)
|
||||
sftp = client.open_sftp()
|
||||
now_utc = date.today()
|
||||
sftp.get(find_latest_file(sftp=sftp, remote_dir="/usr/audit", pattern="server_report_*.txt"), f"audit_{now_utc}.txt")
|
||||
finally:
|
||||
client.close()
|
||||
sftp.close()
|
||||
Reference in new issue
Block a user