Last active
February 7, 2018 10:01
-
-
Save tom-henderson/e24a2b4bae8821880b09fe88be64f1aa to your computer and use it in GitHub Desktop.
Get a list of connected devices from a Unifi controller
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
import json | |
import requests | |
def get_clients(): | |
PORTAL_URL = 'https://0.0.0.0:8443/' | |
PORTAL_LOGIN_PARAMS = { | |
'username': '', | |
'password': '', | |
} | |
login_url = '{}api/login'.format(PORTAL_URL) | |
logout_url = '{}logout'.format(PORTAL_URL) | |
clients_url = '{}api/s/default/stat/sta'.format(PORTAL_URL) | |
session = requests.Session() | |
# Log in | |
login_response = session.post( | |
login_url, | |
data=json.dumps(PORTAL_LOGIN_PARAMS), | |
verify=False, | |
timeout=5, | |
) | |
if login_response.status_code != 200: | |
print "Login failed with code: {}".format(login_response.status_code) | |
sys.exit(1) | |
# Get client data | |
client_response = session.get(clients_url, verify=False, timeout=5) | |
client_data = client_response.json()['data'] | |
# Log out | |
session.get(logout_url, timeout=5) | |
return client_data | |
clients = get_clients() | |
for client in clients: | |
print "{} [{}] ({})".format( | |
client['hostname'], | |
client['mac'], | |
client['oui'], | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment