add src path
This commit is contained in:
130
src/load_ftp_users.py
Normal file
130
src/load_ftp_users.py
Normal file
@@ -0,0 +1,130 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Script per prelevare dati da MySQL e inviare comandi SITE FTP
|
||||
"""
|
||||
|
||||
import mysql.connector
|
||||
from utils.database.connection import connetti_db
|
||||
from utils.config import users_loader as setting
|
||||
from ftplib import FTP
|
||||
import logging
|
||||
import sys
|
||||
from typing import List, Tuple
|
||||
|
||||
# Configurazione logging
|
||||
logging.basicConfig(
|
||||
level=logging.INFO,
|
||||
format='%(asctime)s - %(levelname)s - %(message)s'
|
||||
)
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
# Configurazione server FTP
|
||||
FTP_CONFIG = {
|
||||
'host': 'localhost',
|
||||
'user': 'admin',
|
||||
'password': 'batt1l0',
|
||||
'port': 2121
|
||||
}
|
||||
|
||||
def connect_ftp() -> FTP:
|
||||
"""Connessione al server FTP"""
|
||||
try:
|
||||
ftp = FTP()
|
||||
ftp.connect(FTP_CONFIG['host'], FTP_CONFIG['port'])
|
||||
ftp.login(FTP_CONFIG['user'], FTP_CONFIG['password'])
|
||||
logger.info("Connessione FTP stabilita")
|
||||
return ftp
|
||||
except Exception as e:
|
||||
logger.error(f"Errore connessione FTP: {e}")
|
||||
sys.exit(1)
|
||||
|
||||
def fetch_data_from_db(connection: mysql.connector.MySQLConnection) -> List[Tuple]:
|
||||
"""Preleva i dati dal database"""
|
||||
try:
|
||||
cursor = connection.cursor()
|
||||
|
||||
# Modifica questa query secondo le tue esigenze
|
||||
query = """
|
||||
SELECT username, password
|
||||
FROM ase_lar.ftp_accounts
|
||||
"""
|
||||
|
||||
cursor.execute(query)
|
||||
results = cursor.fetchall()
|
||||
|
||||
logger.info(f"Prelevate {len(results)} righe dal database")
|
||||
return results
|
||||
|
||||
except mysql.connector.Error as e:
|
||||
logger.error(f"Errore query database: {e}")
|
||||
return []
|
||||
finally:
|
||||
cursor.close()
|
||||
|
||||
def send_site_command(ftp: FTP, command: str) -> bool:
|
||||
"""Invia un comando SITE al server FTP"""
|
||||
try:
|
||||
# Il comando SITE viene inviato usando sendcmd
|
||||
response = ftp.sendcmd(f"SITE {command}")
|
||||
logger.info(f"Comando SITE '{command}' inviato. Risposta: {response}")
|
||||
return True
|
||||
except Exception as e:
|
||||
logger.error(f"Errore invio comando SITE '{command}': {e}")
|
||||
return False
|
||||
|
||||
def main():
|
||||
"""Funzione principale"""
|
||||
logger.info("Avvio script caricamento utenti FTP")
|
||||
cfg = setting.Config()
|
||||
|
||||
# Connessioni
|
||||
db_connection = connetti_db(cfg)
|
||||
ftp_connection = connect_ftp()
|
||||
|
||||
try:
|
||||
# Preleva dati dal database
|
||||
data = fetch_data_from_db(db_connection)
|
||||
|
||||
if not data:
|
||||
logger.warning("Nessun dato trovato nel database")
|
||||
return
|
||||
|
||||
success_count = 0
|
||||
error_count = 0
|
||||
|
||||
# Processa ogni riga
|
||||
for row in data:
|
||||
username, password = row
|
||||
|
||||
# Costruisci il comando SITE completo
|
||||
ftp_site_command = f'addu {username} {password}'
|
||||
|
||||
logger.info(f"Sending ftp command: {ftp_site_command}")
|
||||
|
||||
# Invia comando SITE
|
||||
if send_site_command(ftp_connection, ftp_site_command):
|
||||
success_count += 1
|
||||
else:
|
||||
error_count += 1
|
||||
|
||||
logger.info(f"Elaborazione completata. Successi: {success_count}, Errori: {error_count}")
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"Errore generale: {e}")
|
||||
|
||||
finally:
|
||||
# Chiudi connessioni
|
||||
try:
|
||||
ftp_connection.quit()
|
||||
logger.info("Connessione FTP chiusa")
|
||||
except Exception as e:
|
||||
logger.error(f"Errore chiusura connessione FTP: {e}")
|
||||
|
||||
try:
|
||||
db_connection.close()
|
||||
logger.info("Connessione MySQL chiusa")
|
||||
except Exception as e:
|
||||
logger.error(f"Errore chiusura connessione MySQL: {e}")
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user