74 lines
2.0 KiB
Python
74 lines
2.0 KiB
Python
#!/usr/bin/env python3
|
|
"""Test UniFi Controller API connection"""
|
|
|
|
import requests
|
|
import urllib3
|
|
|
|
# Disable SSL warnings (self-signed certs)
|
|
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
|
|
|
|
# Config
|
|
CONTROLLER_URL = "https://192.168.0.39:8443"
|
|
USERNAME = "corey"
|
|
PASSWORD = "is41945549"
|
|
SITE_ID = "default"
|
|
|
|
print("Testing UniFi Controller connection...")
|
|
print(f"URL: {CONTROLLER_URL}")
|
|
|
|
# Step 1: Login
|
|
login_url = f"{CONTROLLER_URL}/api/auth/login"
|
|
login_data = {
|
|
"username": USERNAME,
|
|
"password": PASSWORD
|
|
}
|
|
|
|
try:
|
|
print("\n1. Attempting login...")
|
|
response = requests.post(
|
|
login_url,
|
|
json=login_data,
|
|
verify=False,
|
|
timeout=10
|
|
)
|
|
response.raise_for_status()
|
|
print(" ✅ Login successful!")
|
|
|
|
# Step 2: Get clients
|
|
api_url = f"{CONTROLLER_URL}/proxy/network/api/s/{SITE_ID}/stat/sta"
|
|
cookies = response.cookies
|
|
|
|
print(f"\n2. Fetching clients from site '{SITE_ID}'...")
|
|
api_response = requests.get(
|
|
api_url,
|
|
cookies=cookies,
|
|
verify=False,
|
|
timeout=10
|
|
)
|
|
api_response.raise_for_status()
|
|
|
|
data = api_response.json()
|
|
clients = data.get('data', [])
|
|
|
|
print(f" ✅ Found {len(clients)} connected clients")
|
|
|
|
print("\n3. Sample clients:")
|
|
for idx, client in enumerate(clients[:5], 1):
|
|
name = client.get('name', client.get('hostname', 'Unknown'))
|
|
ip = client.get('ip', 'N/A')
|
|
mac = client.get('mac', 'N/A')
|
|
device_type = 'wireless' if client.get('is_wired', True) is False else 'wired'
|
|
print(f" {idx}. {name} ({device_type})")
|
|
print(f" IP: {ip} | MAC: {mac}")
|
|
|
|
print("\n✅ Connection test PASSED!")
|
|
|
|
except requests.exceptions.ConnectionError as e:
|
|
print(f"\n ❌ Connection failed: {e}")
|
|
print(" Make sure the controller is running and URL is correct")
|
|
except requests.exceptions.HTTPError as e:
|
|
print(f"\n ❌ HTTP error: {e}")
|
|
print(" Check username/password")
|
|
except Exception as e:
|
|
print(f"\n ❌ Error: {e}")
|