Bulk update controller/set-inform on unifi

My goal was to update the controller on my APs from http://weburl:8080 to http://weburl2:8080. I have over 200 Unifi Wireless Access Points that need the update. After I wrote this python script I learned I could use ansible to do this, but as it stands I know python and not Ansible.

First: Get the IP addresses from Unifi. They do not have an export option. So the easiest way I found was to copy and past all the text on the page and use a script to pull the IP addresses from it.
cat IPs.txt | python3 findip.py

import fileinput
import re

for line in fileinput.input():
    ip = re.findall( r'[0-9]+(?:\.[0-9]+){3}', line )
    if ip:
        for i in ip:
            print(i)

Now I saved the results as APs.txt

Second: use this python script to SSH into each AP and update the inform command. This script is threaded so if the IP address is not accessible it just keeps going to the next AP.

import sys, os, string, threading
import paramiko
import numpy as np

cmd = "/usr/bin/mca-cli-op set-inform http://myunificontroller.com:8080/inform"

datafromfile=np.loadtxt("APs.txt",dtype="str")

outlock = threading.Lock()

def workon(host):

    ssh = paramiko.SSHClient()
    ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    ssh.connect(host, username='admin', password='your-password')
    stdin, stdout, stderr = ssh.exec_command(cmd)
    stdin.flush()

    with outlock:
        print (f'STDOUT: {stdout.read().decode("utf8")}')

def main():
    hosts = datafromfile # etc
    threads = []
    for h in hosts:
        t = threading.Thread(target=workon, args=(h,))
        t.start()
        threads.append(t)
    for t in threads:
        t.join()

main()

There maybe another better way to get all this done. Feel free to let me know. In the meantime I will be learning ansible. Hopefully this helps someone else who is trying to do this.

Be the first to comment

Leave a Reply