Advertisement
R4z3r

Unifi Delete Clients

Dec 15th, 2018
3,919
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.24 KB | None | 0 0
  1. #!/usr/bin/env python3
  2. from requests import Session
  3. from urllib.parse import urljoin
  4.  
  5. username = 'REDACTED'
  6. password = r'REDACTED'
  7.  
  8. cloud_key_ip = '192.168.1.2'
  9. controller_port = 8443
  10. site_name = 'default'
  11.  
  12. base_url = 'https://{cloud_key_ip}:{controller_port}'.format(cloud_key_ip=cloud_key_ip, controller_port=controller_port)
  13.  
  14. # How many do you have to forget?
  15. #
  16. #The API call is a **POST** to `/api/s/{site}/cmd/stamgr` with the body `{"macs":["00:1e:35:ff:ff:ff"],"cmd":"forget-sta"}` yes, it does look like you could submit them all in bulk to the API but the webUI doesn't expose that
  17. #
  18. #To fetch the list of all devices in json **GET** `/api/s/{site}/stat/alluser`
  19. #
  20. #Shouldn't be that hard to throw something together in python.
  21.  
  22. def api_login(sess, base_url):
  23.     payload = {
  24.         'username': username,
  25.         'password': password
  26.     }
  27.     url = urljoin(base_url, '/api/login')
  28.     resp = sess.post(url, json=payload, headers={'Referer': '/login'})
  29.     if resp.status_code == 200:
  30.         print('[*] successfully logged in')
  31.         return True
  32.     else:
  33.         print('[!] failed to login with provided credentials')
  34.         return False
  35.  
  36. def api_get_clients(sess, base_url, site_name):
  37.     url = urljoin(base_url, '/api/s/{site_name}/stat/alluser'.format(site_name=site_name))
  38.     resp = sess.get(url)
  39.     client_list = resp.json()['data']
  40.     return client_list
  41.  
  42. def api_del_clients(sess, base_url, site_name, macs):
  43.     payload = {
  44.         'cmd': 'forget-sta',
  45.         'macs': macs
  46.     }
  47.     url = urljoin(base_url, '/api/s/{site_name}/cmd/stamgr'.format(site_name=site_name))
  48.     resp = sess.post(url, json=payload)
  49.     client_list = resp.json()['data']
  50.     return client_list
  51.  
  52. def client_macs(client_list):
  53.     macs = []
  54.     for client in client_list:
  55.         if 'mac' in client:
  56.             macs.append(client['mac'])
  57.     return macs
  58.  
  59. if __name__ == '__main__':
  60.     sess = Session()
  61.     sess.verify = False
  62.    
  63.     success = api_login(sess=sess, base_url=base_url)
  64.     if success:
  65.         client_list = api_get_clients(sess=sess, base_url=base_url, site_name=site_name)
  66.         macs = client_macs(client_list=client_list)
  67.         api_del_clients(sess=sess, base_url=base_url, site_name=site_name, macs=macs)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement