Script to search a web page and then add to users

I am wanting to create a script that goes to a website and gets updated mac addresses from there and adds those mac addresses in hotspot users so that they can log in automatically by mac address. Is there a way of doing this I am looking for a way of doing this to make it more efficient. Right now they add the mac address to the website and then they have to call in and get this person setup so they can get a lease. We only want people with a registered mac address to connect to this network so we do not want to setup a dhcp server to automatically hand out ip addresses. So if there was a way we could set this up and if the mac address is not in the hotspot users they cannot get on the internet.

Hope this helps, adjust the three parameters in the top three lines to match your environment:

# set some parameters for fetching the list of MAC addresses to add
:local server [:resolve "server.com"];
:local url "/MAC-addresses.txt";
:local fileName "MAC-addresses.txt";

# try to fetch the file
/tool fetch mode=http address=$server src-path=$url dst-path=$fileName;

# get a hold on the file
:local filePointer [/file find name=$fileName];
# check if it exists
:if ($filePointer != "") do={
# file exists, start a counter for the entries inserted
    :local added 0;
# read the configuration file into a buffer
    :local buffer [/file get $filePointer contents];
# record the total buffer size for later use in do loop cycling through the lines
    :local bufferSize [:len $buffer];
# create variable to hold buffer offset from start to current line end
    :local lineEnd 0;
# create variable that will hold the current line
    :local line "";
# create variable that keeps track of current position in buffer
    :local position 0;
# go through buffer
    :do {
# find next line end
        :set lineEnd [:find $buffer "\n" $position];
# fetch the line
        :set line [:pick $buffer $position $lineEnd];
# advance position skipping the newlines
        :set position ($lineEnd + 1);
# parse line into array
        :local record [:toarray $line];
# get elements - hotspot user profile first, then the MAC address to create a user for
        :local profile [:pick $record 0];
        :local MAC [:pick $record 1];
# verify that both elements have a string length
        :if (([:len $MAC] > 0) and ([:len $profile] > 0)) do={
# verify that the user profile exists
            :if ([:len [/ip hotspot user profile find name="$profile"]] > 0) do={
# verify that there isn't already a user for this MAC address
                :if ([:len [/ip hotspot user find name="$MAC"]] > 0) do={
# user already exists, log that fact
                    :log warning "Cannot insert MAC user for hotspot, user account with name $MAC already exists!"
                } else={
# all is well, create the user
                    /ip hotspot user add name="$MAC" profile="$profile"
                    :set added ($added + 1);
                }
            } else={
# invalid user profile name, log that fact
                :log warning "Cannot insert MAC user for hotspot, user profile $profile does not exist!"
            }
        } else={
# invalid entry in file, log that fact
            :log warning "Cannot insert MAC user for hotspot, corrupt line in file!"
        }
    } while=($lineEnd < $bufferSize);
# delete the configuration file 
    /file remove $filePointer;
# check how many entries were inserted - if any, log for informational purposes
    :if ($added > 0) do={
        :log info "Inserted $added new MAC users."
    } else={
# if 0, check if the buffer read from the file was larger than 0. if so we should have had inserts
        :if ($bufferSize > 0) do={
            :log warning "MAC user insertion file had content, but none were inserted! Check process!"
        }
    }
} else={
# file could not be fetched. log that fact
    :log warning "Unable to fetch MAC address list!"
}

The file MAC-addresses.txt should be in this format:

profile1,aa:bb:cc:dd:ee:ff
profile1,bb:cc:dd:ee:ff:aa

One entry per line, comma separated, first column is a user profile, second column is the MAC address.

Didn’t test it too much (it’s based on a script I use for something completely different) but it seems to work.

Caveats: the file cannot be larger than 4K. It’d probably be best to generate a list of new MAC addresses every hour and run the script via scheduler 5 minutes later, or something like that.