Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,68 +3,63 @@ import requests
|
|
| 3 |
import time
|
| 4 |
import json
|
| 5 |
|
| 6 |
-
def
|
| 7 |
try:
|
| 8 |
-
# 1. Primero intentamos login para obtener cookie/token
|
| 9 |
session = requests.Session()
|
| 10 |
|
| 11 |
-
#
|
| 12 |
-
login_url = f"http://{ip}/api/login"
|
| 13 |
-
|
| 14 |
-
# Datos de login
|
| 15 |
-
login_data = {
|
| 16 |
-
"username": usuario,
|
| 17 |
-
"password": password
|
| 18 |
-
}
|
| 19 |
-
|
| 20 |
-
# Headers comunes
|
| 21 |
headers = {
|
| 22 |
-
"
|
| 23 |
-
"
|
|
|
|
| 24 |
}
|
| 25 |
|
| 26 |
-
# Intentar
|
| 27 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
|
| 29 |
-
|
| 30 |
-
# Login exitoso, ahora obtener métricas
|
| 31 |
-
status_url = f"http://{ip}/api/v1/status"
|
| 32 |
-
status_response = session.get(status_url, timeout=10)
|
| 33 |
-
|
| 34 |
-
if status_response.status_code == 200:
|
| 35 |
-
metricas = status_response.json()
|
| 36 |
-
resultado = f"# ✅ CONEXIÓN EXITOSA - {ip}\n\n"
|
| 37 |
-
resultado += f"**Estado:** Conectado via API Web\n"
|
| 38 |
-
resultado += f"**Hora:** {time.strftime('%H:%M:%S')}\n\n"
|
| 39 |
-
resultado += "## MÉTRICAS OBTENIDAS:\n"
|
| 40 |
-
resultado += f"```json\n{json.dumps(metricas, indent=2)[:1000]}\n```"
|
| 41 |
-
return resultado
|
| 42 |
-
else:
|
| 43 |
-
return f"# ⚠️ CONECTADO PERO NO MÉTRICAS\n\nError al obtener status: {status_response.status_code}"
|
| 44 |
-
else:
|
| 45 |
-
return f"# ❌ ERROR LOGIN\n\nNo se pudo autenticar: {response.status_code}"
|
| 46 |
-
|
| 47 |
-
except Exception as e:
|
| 48 |
-
return f"# ❌ ERROR DE CONEXIÓN\n\n**IP:** {ip}\n**Error:** {str(e)}\n**Hora:** {time.strftime('%H:%M:%S')}"
|
| 49 |
-
|
| 50 |
-
# INTERFAZ GRADIO
|
| 51 |
-
with gr.Blocks(title="Test Ubiquiti Web API") as demo:
|
| 52 |
-
gr.Markdown("# 📡 Test Antena Ubiquiti (Web API)")
|
| 53 |
-
|
| 54 |
-
with gr.Row():
|
| 55 |
-
with gr.Column():
|
| 56 |
-
ip_input = gr.Textbox(label="IP del Equipo", placeholder="192.168.1.24", value="192.168.1.24")
|
| 57 |
-
usuario_input = gr.Textbox(label="Usuario", value="admin")
|
| 58 |
-
password_input = gr.Textbox(label="Contraseña", type="password")
|
| 59 |
-
test_btn = gr.Button("🚀 Ejecutar Test Web", variant="primary")
|
| 60 |
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 69 |
|
| 70 |
-
|
|
|
|
|
|
|
|
|
| 3 |
import time
|
| 4 |
import json
|
| 5 |
|
| 6 |
+
def test_antena_vpn(ip, usuario, password):
|
| 7 |
try:
|
|
|
|
| 8 |
session = requests.Session()
|
| 9 |
|
| 10 |
+
# Headers para evitar bloqueos
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
headers = {
|
| 12 |
+
"User-Agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36",
|
| 13 |
+
"Accept": "application/json",
|
| 14 |
+
"Content-Type": "application/json"
|
| 15 |
}
|
| 16 |
|
| 17 |
+
# Intentar endpoints comunes de Ubiquiti
|
| 18 |
+
endpoints_try = [
|
| 19 |
+
f"http://{ip}/api/login",
|
| 20 |
+
f"http://{ip}/api/v1/login",
|
| 21 |
+
f"http://{ip}/api/v2/login",
|
| 22 |
+
f"http://{ip}/login",
|
| 23 |
+
f"https://{ip}/api/login"
|
| 24 |
+
]
|
| 25 |
|
| 26 |
+
login_data = {"username": usuario, "password": password}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
+
for login_url in endpoints_try:
|
| 29 |
+
try:
|
| 30 |
+
print(f"Probando: {login_url}")
|
| 31 |
+
response = session.post(
|
| 32 |
+
login_url,
|
| 33 |
+
json=login_data,
|
| 34 |
+
headers=headers,
|
| 35 |
+
timeout=10,
|
| 36 |
+
verify=False # Ignorar SSL si es necesario
|
| 37 |
+
)
|
| 38 |
+
|
| 39 |
+
if response.status_code in [200, 201]:
|
| 40 |
+
print(f"✅ Login exitoso en: {login_url}")
|
| 41 |
+
|
| 42 |
+
# Probar endpoints de status
|
| 43 |
+
status_endpoints = [
|
| 44 |
+
f"http://{ip}/api/v1/status",
|
| 45 |
+
f"http://{ip}/api/status",
|
| 46 |
+
f"http://{ip}/status",
|
| 47 |
+
f"http://{ip}/api/v1/device",
|
| 48 |
+
f"http://{ip}/api/v2/devices"
|
| 49 |
+
]
|
| 50 |
+
|
| 51 |
+
for status_url in status_endpoints:
|
| 52 |
+
try:
|
| 53 |
+
status_response = session.get(status_url, timeout=10, verify=False)
|
| 54 |
+
if status_response.status_code == 200:
|
| 55 |
+
metricas = status_response.json()
|
| 56 |
+
return f"""
|
| 57 |
+
# ✅ CONEXIÓN EXITOSA - {ip}
|
| 58 |
+
|
| 59 |
+
**Método:** API Web Ubiquiti
|
| 60 |
+
**Endpoint:** {status_url}
|
| 61 |
+
**Hora:** {time.strftime('%H:%M:%S')}
|
| 62 |
|
| 63 |
+
## DATOS OBTENIDOS:
|
| 64 |
+
```json
|
| 65 |
+
{json.dumps(metricas, indent=2)[:1500]}
|