Last active
April 8, 2022 17:46
-
-
Save dmrub/61e6ba9d05a2571e479be1b66d9ca9e2 to your computer and use it in GitHub Desktop.
List all Unifi clients
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
#!/usr/bin/env python | |
# | |
# This code is based on | |
# https://github.com/finish06/pyunifi/blob/master/unifi-ls-clients | |
# | |
# 1. Install pyunifi | |
# pip install --user pyunifi | |
# 2. Run this script | |
# unifi-ls-all-clients -c UNIFI_HOST -b UNIFI_PORT -u USER -p PASSWORD -V | |
import argparse | |
from pyunifi.controller import Controller | |
def print_table(table): | |
longest_cols = [(max([len(str(row[i])) for row in table]) + 1) for i in range(len(table[0]))] | |
row_format = " ".join(["{:<" + str(longest_col) + "}" for longest_col in longest_cols]) | |
for row in table: | |
print(row_format.format(*row)) | |
if __name__ == '__main__': | |
parser = argparse.ArgumentParser() | |
parser.add_argument('-c', '--controller', default='unifi', help='the controller address (default "unifi")') | |
parser.add_argument('-u', '--username', default='admin', help='the controller username (default("admin")') | |
parser.add_argument('-p', '--password', default='', help='the controller password') | |
parser.add_argument('-b', '--port', default='8443', help='the controller port (default "8443")') | |
parser.add_argument('-v', '--version', default='v5', help='the controller base version (default "v5")') | |
parser.add_argument('-s', '--siteid', default='default', help='the site ID, UniFi >=3.x only (default "default")') | |
parser.add_argument('-S', '--sitename', default=None, help='the site name, UniFi >=3.x only (default none)') | |
parser.add_argument('-V', '--no-ssl-verify', default=False, action='store_true', | |
help='Don\'t verify ssl certificates') | |
parser.add_argument('-C', '--certificate', default='', help='verify with ssl certificate pem file') | |
args = parser.parse_args() | |
ssl_verify = (not args.no_ssl_verify) | |
if ssl_verify and len(args.certificate) > 0: | |
ssl_verify = args.certificate | |
c = Controller(args.controller, args.username, args.password, args.port, args.version, args.siteid, | |
ssl_verify=ssl_verify) | |
site_id_to_name = {} | |
all_sites = c.get_sites() | |
for site in all_sites: | |
site_id = site.get('_id') | |
if site_id: | |
site_id_to_name[site_id] = site.get('desc', site_id) | |
if args.sitename: | |
sites = [args.sitename] | |
else: | |
sites = [site.get('desc') for site in all_sites if site.get('desc')] | |
out_table = [ | |
('SITE', 'NAME', 'HOSTNAME', 'IP ADDRESS', 'MAC', 'MANUFACTURER') | |
] | |
for site_name in sites: | |
c.switch_site(site_name) | |
for client in c.get_clients(): | |
def val(name, default='-'): | |
return client.get(name, default) | |
out_table.append( | |
(site_id_to_name.get(val('site_id'), ''), val('name'), val('hostname'), val('ip'), val('mac'), | |
val('oui')) | |
) | |
print_table(out_table) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment