0% found this document useful (0 votes)
54 views1 page

MikroTik Hotspot User Automation Script

The document outlines a system scheduler script that adds users to a hotspot every 3 seconds by reading from a specified text file. It processes user data including name, email, phone, and generates a password before adding the user to the hotspot. Additionally, it logs the user information and manages file content to ensure proper operation.

Uploaded by

Jose Claudio
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
54 views1 page

MikroTik Hotspot User Automation Script

The document outlines a system scheduler script that adds users to a hotspot every 3 seconds by reading from a specified text file. It processes user data including name, email, phone, and generates a password before adding the user to the hotspot. Additionally, it logs the user information and manages file content to ensure proper operation.

Uploaded by

Jose Claudio
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

/system scheduler

add interval=3s name=ADD_USER_HOTSPOT on-event=":global userToAdd 1\r\


\n:global fileContent [/file get [find name=[Link]] contents
]\r\
\n\r\
\n:while (\$userToAdd != 0) do={\r\
\n\r\
\n:global beginString ([:find \$fileContent \"!==\"] + 3) \r\
\n:if (\$beginString = 3) do={/file set [find name=[Link]]
contents=\"\";:return \"\";} \r\
\n:global endString [:find \$fileContent \"==!\"]\r\
\n:global userToAdd [toarray [:pick \$fileContent \$beginString \$endString]]\
r\
\n:global fileContent [:pick \$fileContent (\$endString + 3) [:len \
$fileContent]]\r\
\n\r\
\n\r\
\n:global name [pick \$userToAdd 0]\r\
\n:global email [pick \$userToAdd 1]\r\
\n:global phone [pick \$userToAdd 2]\r\
\n:global password [pick \$phone ([:len \$phone] - 4) [:len \$phone]]\r\
\n\r\
\n:do {/ip hotspot user add password=\$password name=\$email comment=\"\$name -
\$phone\"} on-error={}\r\
\n\r\
\n:put \"Nome: \$name - Email: \$email - Telefone: \$phone\"\r\
\n/log warning message=\"Nome: \$name - Email: \$email - Telefone: \$phone\"\r\
\n\r\
\n\r\
\n\r\
\n\r\
\n\r\
\n\r\
\ndelay 500ms\r\
\n\r\
\n}\r\
\n\r\
\n\r\
\n\r\
\n\r\
\n" policy=ftp,reboot,read,write,policy,test,password,sniff,sensitive,romon

/system logging action


add disk-file-count=1 disk-file-name=users-to-hotspot disk-lines-per-file=50
name=hotspot target=disk
/system logging
add action=hotspot topics=hotspot,info,debug

Common questions

Powered by AI

Using ":global" for variable declaration impacts scope by making these variables accessible throughout the entire script, instead of being limited to function or block-level scope. This allows for data persistence across different parts of the script execution, facilitating complex operations that require uniform data tracking, like user addition in this case. However, it also increases the risk of unintended side-effects or data contamination if variables are not carefully managed or distinctively named.

The script ensures accurate and meaningful logging by including specific user attributes ('Nome', 'Email', 'Telefone') in both its console outputs and system logs. These logs are formatted to convey essential identity details, facilitating effective administrative review. Furthermore, the setup of dedicated logging actions with disk targets and constraints ensures that logs are systematically stored and easily retrievable, reinforcing data integrity and supporting comprehensive audits.

Processing file user data in small segments allows for efficient resource management by breaking down tasks into manageable parts, reducing memory or CPU spikes, and ensuring that each user data segment is fully processed before proceeding. This approach aids in pinpointing errors to specific data segments, simplifying debugging. It further prevents interruption of service by compartmentalizing operations, which enhances stability and fault isolation within the script execution environment.

The command ":global userToAdd 1" initializes a global variable 'userToAdd' with an initial value of 1. This variable is central to the script's loop, which continuously checks and processes users to be added from the 'users-to-hotspot.0.txt' file, as long as 'userToAdd' is not zero. This setup implies a loop condition that continues to execute until no more users need to be added, reflecting a crucial flow control mechanism within the script's operation.

The script uses a simple 'on-error' handler when adding users to the hotspot, which essentially ignores any errors by providing an empty block. While this prevents the script from crashing during errors, it offers no diagnostics or logging, which could hinder troubleshooting and error correction processes. Improvements could include logging error details for future reference or implementing conditional logic to handle different error types distinctly, which would improve maintainability and error recovery.

The script includes a command for a 'delay 500ms', likely serving to prevent overwhelming the system with rapid requests or to allow sufficient time for resources to be freed or updated between operations. While these delays can reduce the risk of collisions or resource exhaustion, they also introduce latency, which can impact overall system performance, especially if the script is running frequently or handling large volumes of data. Strategic placement and duration of delays need to be balanced against system demand and processing efficiency.

The script employs a methodical approach to extract user information from the file content. It uses specific string manipulation functions to locate and parse user data encased between the markers "!==" and "==!" in the string that represents the file's content. By identifying these precise positions, the script ensures that only the intended sections of the data are extracted, minimizing potential errors and ensuring accuracy. This strategy ensures that each user is processed independently and completely before moving on, thus maintaining the relevance of the actions performed within the loop.

Dependence on fixed delimiters ('!==', '==!') for parsing could lead to parsing failure if these markers are altered or misused in the file, or if the input data format evolves. This rigid structure reduces flexibility and increases maintenance overhead. To mitigate these challenges, the script could implement a more flexible parsing logic that allows configuration of delimiters or adopts a more robust data format, such as XML or JSON, which includes inherent structural markers. This would enhance adaptability and reduce the likelihood of parsing errors.

The script supports scalability by processing users from a file, which allows batch updates and can accommodate large sets of data as user numbers grow. Coupled with dynamic loop execution and logging mechanisms that document user information and system events, the script can scale to handle increased load efficiently. However, to further enhance scalability, considerations such as adapting to file size limitations or optimizing process intervals might prove beneficial as user numbers expand exponentially.

The script's logging mechanism enhances management and monitoring by systematically recording user information and activities related to the hotspot. It logs critical details such as 'Nome', 'Email', and 'Telefone' of each user being added, as both console output and warning messages. Additionally, the script sets up a dedicated logging action to store these logs in a disk file designed with specific properties like 'disk-file-count' and 'disk-lines-per-file', allowing for persistent record-keeping and easy review for system audits or troubleshooting.

You might also like