Hello, I don’t speak English very well and I use Google Translate
I want to create a script to create a hotspot user in a telst file that can identify the word preceded by a symbol or name, for example, like the picture I put

Use a text editor,
open the file,
replace
" "
with
replace
“username=”
with
“/ip hotspot user add name=”
replace
“&password=”
with
" profile=all password=" (notice the space at the start)
Simply alter the “program” that create that lines on the txt file…
and yuou can import the file with /import command on terminal.
Thanks, but I want to script it as the picture I want the last line I specified
Is simpler that you alter the “program” that create that lines on the txt file…
Or is done by “excel”?
Thanks I want to do the work on txt I have a special reason if possible thanks again
It’s not an easy thing, you need someone to do it from scratch and to follow you step by step…
Can’t you alter the file with powershell before uploading it to routeros? (is more easy)
You download it from internet or do you drag the file there by hand?
I’m downloading it from the Internet
/tool fetch mode=https url="https://example.com/get_user_credentials.php" dst-path=user_credentials.txt
:local username [/file get user_credentials.txt contents | find "username" | :pick [ :find "]"] | :pick [1 [:len $result - 1]]
:local password [/file get user_credentials.txt contents | find "password" | :pick [ :find "]"] | :pick [1 [:len $result - 1]]
:local profile "hotspot"
/ip hotspot user add name=$username password=$password profile=$profile
/file remove user_credentials.txt
I say like and that but I don’t know if that won’t work
I do not know if you try to apply your knowledge from linux or you ask ChatGPT for novels,
but that is not RouterOS script: for example “|” do not exist as intended on that scipt.
Why is present “]”? Is a json output?
You’d better fix the php for have the correct output…
just have it write the data I wrote before instead of “username=” and “password=” strings…
Sorry, but I stop here, as I already wrote:
It’s not an easy thing, you need someone to do it from scratch and to follow you step by step…
I understand, thank you very much. I want to create a user hotspot through the Telegram bot. Is there any way? That’s right, I got that from Chat GPT4
thanks
ChatGPT response
:
:local profile "hotspot"
:local fileName "user_credentials.txt"
:local fileCnt [/file get $fileName contents]
:local fileLen [:len $fileCnt]
:local lineLen 0
:local prevLineLen 0
:local lineCnt 0
:local line ""
:local notBlank ".*\\S.*"
:local uTokenLen [:len "username= "]
:local pTokenLen [:len "passowrd= "]
:local sepLen [:len " & "]
:local usernames
:local passwords
:while ($lineLen < $fileLen) do={
:set lineLen [:find $fileCnt "\n" $prevLineLen]
:if ([:len $lineLen] = 0) do={ :set lineLen $fileLen }
:set line [:pick $fileCnt $prevLineLen $lineLen]
:set prevLineLen ($lineLen + 1)
:if ($line != "\r") do={
:local username [:pick $line $uTokenLen [:find $line " &" ]]
if ($username ~ $notBlank) do= {
:local password [:pick $line ($uTokenLen + [:len $username] + $sepLen + $pTokenLen) $lineLen]
:set ($usernames->$lineCnt) $username
:set ($passwords->$lineCnt) $password
:set lineCnt ($lineCnt + 1)
}
}
}
:for i from=0 to=([:len $usernames] - 1) do={
:local username [:pick $usernames $i]
:local password [:pick $passwords $i]
/ip/hotspot/user add name=$username password=$password profile=$profile
}
/file remove $fileName
Just a bullshit at the start…
:local notBlank ".*\\S.*"
The \s metacharacter matches whitespace character.
Whitespace characters can be:
A space character
A tab character
A carriage return character
A new line character
A vertical tab character
A form feed character
RouterOS do not support \S (neither \s)
[] > :put ("test" ~ "\\S")
false
[] > :put ("test" ~ ".*\\S.*")
false
[] > :put ("" ~ ".*\\S.*")
false
[] > :put ([:nothing] ~ ".*\\S.*")
false
[] > :put (" " ~ ".*\\S.*")
false
[] > :put ("x x" ~ ".*\\S.*")
false
[] > :put ([:find in=""] ~ ".*\\S.*")
false
Lol, rage again…
You really think that is a ChatGPT script?
I admit that I did not tested this regex with blanks on MT, but on https://regex101.com/ it is valid (and I think that is only mistake in script).
Sorry, but when I see commands invented or not supported, it’s a ChatGPT attitude…
I do not like the script, if you wait the time I write, I explain why.
I have unessery adding usernames and passwors into arrays and iteration at the end, hotspot users can be added in main parsing loop where usernames and passwors are added into arrays...
P.S.
you are wrong about regex (at least for ROS 7.8 ), just tested it:
> :put ("test" ~ "\\S")
true
> :put ("" ~ "\\S")
false
(see next post)
EDIT: is useless back edit if you do not read next post first…
Here is optimized version without additional arrays and loop.
Feel free to correct it ![]()
:local profile "hotspot"
:local fileName "user_credentials.txt"
:local fileCnt [/file get $fileName contents]
:local fileLen [:len $fileCnt]
:local lineLen 0
:local prevLineLen 0
:local lineCnt 0
:local line ""
:local notBlank ".*\\S.*"
:local uTokenEnd " &"
:local uTokenLen [:len "username= "]
:local pTokenLen [:len "passowrd= "]
:local sepLen [:len " & "]
:while ($lineLen < $fileLen) do={
:set lineLen [:find $fileCnt "\n" $prevLineLen]
:if ([:len $lineLen] = 0) do={ :set lineLen $fileLen }
:set line [:pick $fileCnt $prevLineLen $lineLen]
:set prevLineLen ($lineLen + 1)
:if ($line != "\r") do={
:local username [:pick $line $uTokenLen [:find $line $uTokenEnd]]
if ($username ~ $notBlank) do= {
:local password [:pick $line ($uTokenLen + [:len $username] + $sepLen + $pTokenLen) $lineLen]
/ip/hotspot/user add name=$username password=$password profile=$profile
}
}
}
/file remove $fileName
Still present unsupported “.\S.”, so never works.
The “new line” method used is not initially checked.
It is almost certainly consistent throughout the file, it is quite unlikely that it is mixed.
So first check if there is “\r\n” in the file.
If there is, it’s sure to be that, otherwise it’s sure to be “\n”.
It also seems pretty unlikely to me that it’s just \r,
so at that point there’s no need to recalculate it every single line.
The script doesn’t check if the line actually contains “username=”
and if indeed after the " &" there is password=, it’s just based on the length.
The " &" value is encoded in the script (like the others) instead of in variables at the start.
No error check if on the username and password are present only valid characters (like 0x20-0x7E only)
because HotSpot / RouterOS do not support UTF-8 or other 8-bit codepages… and what is, for example “д” in one codepage, is “è” on another
The user have RouterOS v6 and your syntax is valid only on v7, but I have deduced this from the image, it is not said that you knew how to interpret.
& more.
Ok
, here is from my terminal:
> :put ("" ~ ".*\\S.*")
false
> :put ("afds" ~ ".*\\S.*")
true
> :put (" " ~ ".*\\S.*")
false
Ok I did not know that, but if you are mentioning pretty unlikely for "\r" I can say that in the file will pretty unlikely be characters for user/pass outside 0x20-0x7E range ![]()
Feel free to write your own, I was trying to help the guy and I think it is pretty much helpful vs ChatGPT like scripts...
PS.
I created parsing lines like that to support both \n and \r\n line endings in file to be more robust. Trimming usernames can be added and checking character set but dont't have more time to add that.