send_ips_report.py

Envia os IP´s das interfaces para um email.
This commit is contained in:
walter 2025-06-07 16:23:11 +00:00
commit 1b1dfcc29b

46
send_ips_report.py Normal file
View File

@ -0,0 +1,46 @@
#!/usr/bin/env python3
import smtplib
from email.mime.text import MIMEText
from datetime import datetime
import subprocess
# Configurações
smtp_server = "smtp.gmail.com"
smtp_port = 587
email_from = "walter@h4t.eti.br"
email_to = "walter@h4t.eti.br"
app_password = "pzqw mwgc qctj imqs"
subject = f"Relatório de IPs - {datetime.now().strftime('%Y-%m-%d %H:%M')}"
# Interfaces com nome amigável
interfaces = {
"eth8": "Algar1",
"eth6": "Algar2",
"eth7": "Claro"
}
body = f"Relatório de IPs Públicos - {datetime.now()}\n"
body += "----------------------------------------\n"
for iface, name in interfaces.items():
try:
ip = subprocess.check_output(["curl", "-s", "--interface", iface, "https://ifconfig.me"], timeout=10).decode().strip()
except:
ip = "IP inválido ou não detectado"
body += f"{name} ({iface}) => IP: {ip}\n"
body += "----------------------------------------"
# Construir email
msg = MIMEText(body)
msg["Subject"] = subject
msg["From"] = email_from
msg["To"] = email_to
# Enviar email
try:
with smtplib.SMTP(smtp_server, smtp_port) as server:
server.starttls()
server.login(email_from, app_password)
server.send_message(msg)
print("Email enviado com sucesso.")
except Exception as e:
print(f"Falha ao enviar email: {e}")